summaryrefslogtreecommitdiff
path: root/qa/qa/factory/dependency.rb
blob: d0e85a68237ea3135079caa6c769dfe631d892ac (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
module QA
  module Factory
    class Dependency
      Signature = Struct.new(:factory, :block)

      def initialize(name, factory, signature)
        @name = name
        @factory = factory
        @signature = signature
      end

      def overridden?
        !!@factory.public_send(@name)
      end

      def build!
        return if overridden?

        Builder.new(@signature).fabricate!.tap do |product|
          @factory.public_send("#{@name}=", product)
        end
      end

      class Builder
        def initialize(signature)
          @factory = signature.factory
          @block = signature.block
        end

        def fabricate!
          @factory.fabricate! do |factory|
            @block&.call(factory)
          end
        end
      end
    end
  end
end