Add a DSL evaluator to QA base factory class

This commit is contained in:
Grzegorz Bizon 2018-01-11 10:47:49 +01:00
parent 2de8fb7bab
commit 1011ca4949

View file

@ -1,6 +1,12 @@
require 'forwardable'
module QA
module Factory
class Base
extend SingleForwardable
def_delegators :evaluator, :dependency, :dependencies
def fabricate!(*_args)
raise NotImplementedError
end
@ -17,13 +23,21 @@ module QA
end
end
def self.dependencies
@dependencies ||= {}
def self.evaluator
@evaluator ||= Factory::Base::DSL.new(self)
end
def self.dependency(factory, as:, &block)
class DSL
attr_reader :dependencies
def initialize(base)
@base = base
@dependencies = {}
end
def dependency(factory, as:, &block)
as.tap do |name|
class_eval { attr_accessor name }
@base.class_eval { attr_accessor name }
Dependency::Signature.new(factory, block).tap do |signature|
dependencies.store(name, signature)
@ -32,4 +46,5 @@ module QA
end
end
end
end
end