summaryrefslogtreecommitdiff
path: root/qa/qa/factory/dependency.rb
blob: fc5dc82ce2994bcaf8b7c84312e3fc798dbbfe12 (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
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, @factory).fabricate!.tap do |product|
          @factory.public_send("#{@name}=", product)
        end
      end

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

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