def define_region_accessor(region_name, within: nil, each: nil, collection_class: nil, region_class: nil, &block)
      include(Module.new do
        define_method(region_name) do
          # FIXME: ['MakeItSizeOne'] is required when current class is anonymous (inline region)
          class_path = self.class.name ? self.class.name.split('::') : ['MakeItSizeOne']
          namespace = if class_path.size > 1
                        class_path.pop
                        Object.const_get(class_path.join('::'))
                      elsif class_path.size == 1
                        self.class
                      else
                        raise "Cannot understand namespace from #{class_path}"
                      end

          # This copy is required since `region_class` is declared outside of this defined function,
          # and the function could change it
          region_single_class = region_class

          unless region_single_class
            if block_given?
              region_single_class = Class.new
              region_single_class.class_eval { include(Watirsome) }
              region_single_class.class_eval(&block)
            else
              singular_klass = region_name.to_s.split('_').map(&:capitalize).join
              if each
                collection_class_name = "#{singular_klass}Region"
                singular_klass = singular_klass.sub(/s\z/, '')
              end
              singular_klass << 'Region'
              region_single_class = namespace.const_get(singular_klass)
            end
          end

          scope = case within
                  when Proc
                    instance_exec(&within)
                  when Hash
                    region_element.element(within)
                  else
                    region_element
                  end

          if each
            elements = (scope.exists? ? scope.elements(each) : [])

            if collection_class_name && namespace.const_defined?(collection_class_name)
              region_collection_class = namespace.const_get(collection_class_name)
            elsif collection_class
              region_collection_class = collection_class
            else
              return elements.map { |element| region_single_class.new(@browser, element, self) }
            end

            region_collection_class.class_eval do
              include Enumerable

              attr_reader :region_collection

              define_method(:initialize) do |browser, region_element, region_elements|
                super(browser, region_element, self)
                @region_collection = if region_elements.all? { |element| element.is_a?(Watir::Element) }
                                       region_elements.map { |element| region_single_class.new(browser, element, self) }
                                     else
                                       region_elements
                                     end
              end

              def each(&block)
                region_collection.each(&block)
              end
            end

            region_collection_class.new(@browser, scope, elements)
          else
            region_single_class.new(@browser, scope, self)
          end
        end
      end)
    end
def define_region_accessor(region_name, within: nil, each: nil, collection_class: nil, region_class: nil, &block)
      include(Module.new do
        define_method(region_name) do
          # FIXME: ['MakeItSizeOne'] is required when current class is anonymous (inline region)
          class_path = self.class.name ? self.class.name.split('::') : ['MakeItSizeOne']
          namespace = if class_path.size > 1
                        class_path.pop
                        Object.const_get(class_path.join('::'))
                      elsif class_path.size == 1
                        self.class
                      else
                        raise "Cannot understand namespace from #{class_path}"
                      end

          # This copy is required since `region_class` is declared outside of this defined function,
          # and the function could change it
          region_single_class = region_class

          unless region_single_class
            if block_given?
              region_single_class = Class.new
              region_single_class.class_eval { include(Watirsome) }
              region_single_class.class_eval(&block)
            else
              singular_klass = region_name.to_s.split('_').map(&:capitalize).join
              if each
                collection_class_name = "#{singular_klass}Region"
                singular_klass = singular_klass.sub(/s\z/, '')
              end
              singular_klass << 'Region'
              region_single_class = namespace.const_get(singular_klass)
            end
          end

          scope = case within
                  when Proc
                    instance_exec(&within)
                  when Hash
                    region_element.element(within)
                  else
                    region_element
                  end

          if each
            elements = (scope.exists? ? scope.elements(each) : [])

            if collection_class_name && namespace.const_defined?(collection_class_name)
              region_collection_class = namespace.const_get(collection_class_name)
            elsif collection_class
              region_collection_class = collection_class
            else
              return elements.map { |element| region_single_class.new(@browser, element, self) }
            end

            region_collection_class.class_eval do
              include Enumerable

              attr_reader :region_collection

              define_method(:initialize) do |browser, region_element, region_elements|
                super(browser, region_element, self)
                @region_collection = if region_elements.all? { |element| element.is_a?(Watir::Element) }
                                       region_elements.map { |element| region_single_class.new(browser, element, self) }
                                     else
                                       region_elements
                                     end
              end

              def each(&block)
                region_collection.each(&block)
              end
            end

            region_collection_class.new(@browser, scope, elements)
          else
            region_single_class.new(@browser, scope, self)
          end
        end
      end)
    end
FIXME: the generated method is far too complex, and deserves a proper refactoring
 rubocop:disable all