Welcome to mirror list, hosted at ThFree Co, Russian Federation.

product.rb « factory « qa « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17fe908eaa2fd886ae9939b72643bcf50467e6b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'capybara/dsl'

module QA
  module Factory
    class Product
      include Capybara::DSL

      NoValueError = Class.new(RuntimeError)

      attr_reader :factory, :web_url

      Attribute = Struct.new(:name, :block)

      def initialize(factory, web_url)
        @factory = factory
        @web_url = web_url

        populate_attributes!
      end

      def visit!
        visit(web_url)
      end

      def self.populate!(factory, web_url)
        new(factory, web_url)
      end

      private

      def populate_attributes!
        factory.class.attributes.each do |attribute|
          instance_exec(factory, attribute.block) do |factory, block|
            value = attribute_value(attribute, block)

            raise NoValueError, "No value was computed for product #{attribute.name} of factory #{factory.class.name}." unless value

            define_singleton_method(attribute.name) { value }
          end
        end
      end

      def attribute_value(attribute, block)
        factory.api_resource&.dig(attribute.name) ||
          (block && block.call(factory)) ||
          (factory.respond_to?(attribute.name) && factory.public_send(attribute.name))
      end
    end
  end
end