From 1011ca49495eb1f48d43694bfbb2a96951fd45ba Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 11 Jan 2018 10:47:49 +0100 Subject: [PATCH] Add a DSL evaluator to QA base factory class --- qa/qa/factory/base.rb | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/qa/qa/factory/base.rb b/qa/qa/factory/base.rb index 00851a7bece..3e5227ea426 100644 --- a/qa/qa/factory/base.rb +++ b/qa/qa/factory/base.rb @@ -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,16 +23,25 @@ module QA end end - def self.dependencies - @dependencies ||= {} + def self.evaluator + @evaluator ||= Factory::Base::DSL.new(self) end - def self.dependency(factory, as:, &block) - as.tap do |name| - class_eval { attr_accessor name } + class DSL + attr_reader :dependencies - Dependency::Signature.new(factory, block).tap do |signature| - dependencies.store(name, signature) + def initialize(base) + @base = base + @dependencies = {} + end + + def dependency(factory, as:, &block) + as.tap do |name| + @base.class_eval { attr_accessor name } + + Dependency::Signature.new(factory, block).tap do |signature| + dependencies.store(name, signature) + end end end end