1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

AnonymousEvaluator => EvaluatorClassDefiner

This commit is contained in:
Joshua Clayton 2011-12-02 13:09:35 -05:00
parent 3dbbf4ed7a
commit b72c916903
5 changed files with 35 additions and 23 deletions

View file

@ -4,7 +4,8 @@ require 'factory_girl/proxy'
require 'factory_girl/registry'
require 'factory_girl/null_factory'
require 'factory_girl/factory'
require 'factory_girl/anonymous_evaluator'
require 'factory_girl/evaluator'
require 'factory_girl/evaluator_class_definer'
require 'factory_girl/attribute'
require 'factory_girl/callback'
require 'factory_girl/declaration_list'

View file

@ -0,0 +1,15 @@
module FactoryGirl
class Evaluator
def initialize(overrides = {})
@cached_attributes = overrides
end
def method_missing(method_name, *args, &block)
if @cached_attributes.key?(method_name)
@cached_attributes[method_name]
else
super
end
end
end
end

View file

@ -1,28 +1,24 @@
module FactoryGirl
class AnonymousEvaluator
class EvaluatorClassDefiner
attr_reader :attributes
def initialize
@attributes = []
end
def evaluator_class
@evaluator ||= Class.new(FactoryGirl::Evaluator)
end
def set(attribute)
define_attribute(attribute.name, attribute.to_proc)
@attributes << attribute.name unless attribute.ignored
end
def evaluator
@evaluator ||= Class.new do
def initialize
@cached_attributes = {}
end
end
end
private
def define_attribute(attribute_name, attribute_proc)
evaluator.send(:define_method, attribute_name) {
evaluator_class.send(:define_method, attribute_name) {
@cached_attributes[attribute_name] ||= instance_exec(&attribute_proc)
}
end

View file

@ -90,13 +90,13 @@ module FactoryGirl
@proxy = proxy
@assigned_attributes = []
@evaluator = AnonymousEvaluator.new
@evaluator.evaluator.send(:define_method, :association) { |*args|
@evaluator_class_definer = EvaluatorClassDefiner.new
@evaluator_class_definer.evaluator_class.send(:define_method, :association) { |*args|
proxy.association(*args)
}
end
delegate :set, :attributes, :to => :@evaluator
delegate :set, :attributes, :to => :@evaluator_class_definer
def to_hash
attributes.inject({}) do |result, attribute|
@ -112,7 +112,7 @@ module FactoryGirl
end
def anonymous_instance
@anonymous_instance ||= @evaluator.evaluator.new
@anonymous_instance ||= @evaluator_class_definer.evaluator_class.new
end
private

View file

@ -1,14 +1,14 @@
require "spec_helper"
shared_examples "#set on an AnonymousEvaluator" do
shared_examples "#set on an EvaluatorClassDefiner" do
it "adds the method to the evaluator" do
subject.set(attribute)
subject.evaluator.new.one.should == 1
subject.evaluator_class.new.one.should == 1
end
it "caches the result" do
subject.set(attribute)
subject.evaluator.new.tap do |obj|
subject.evaluator_class.new.tap do |obj|
obj.one.should == 1
obj.one.should == 1
end
@ -18,21 +18,21 @@ shared_examples "#set on an AnonymousEvaluator" do
subject.set(attribute)
second_attribute = stub("attribute", :name => :two, :to_proc => lambda { one + 1 }, :ignored => false)
subject.set(second_attribute)
subject.evaluator.new.two.should == 2
subject.evaluator_class.new.two.should == 2
end
end
describe FactoryGirl::AnonymousEvaluator do
describe FactoryGirl::EvaluatorClassDefiner do
its(:attributes) { should == [] }
end
describe FactoryGirl::AnonymousEvaluator, "#set" do
describe FactoryGirl::EvaluatorClassDefiner, "#set" do
let(:value) { lambda { @result ||= 0; @result += 1 } }
context "setting an ignored attribute" do
let(:attribute) { stub("attribute", :name => :one, :to_proc => value, :ignored => true) }
it_behaves_like "#set on an AnonymousEvaluator"
it_behaves_like "#set on an EvaluatorClassDefiner"
it "does not track the attribute" do
subject.set(attribute)
@ -43,7 +43,7 @@ describe FactoryGirl::AnonymousEvaluator, "#set" do
context "setting an attribute" do
let(:attribute) { stub("attribute", :name => :one, :to_proc => value, :ignored => false) }
it_behaves_like "#set on an AnonymousEvaluator"
it_behaves_like "#set on an EvaluatorClassDefiner"
it "tracks the attribute" do
subject.set(attribute)