mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Move all block execution for dynamic attributes to the anonymous_instance
This commit is contained in:
parent
56a6f679a7
commit
b339c8f109
5 changed files with 32 additions and 34 deletions
|
@ -10,7 +10,7 @@ module FactoryGirl
|
|||
block = @block
|
||||
|
||||
lambda {
|
||||
value = block.arity == 1 ? block.call(proxy) : proxy.instance_exec(&block)
|
||||
value = block.arity == 1 ? block.call(self) : instance_exec(&block)
|
||||
raise SequenceAbuseError if FactoryGirl::Sequence === value
|
||||
value
|
||||
}
|
||||
|
|
|
@ -11,12 +11,12 @@ module FactoryGirl
|
|||
@proxy = ObjectWrapper.new(klass)
|
||||
end
|
||||
|
||||
delegate :get, :set, :set_ignored, :to => :@proxy
|
||||
delegate :get, :set, :set_ignored, :anonymous_instance, :to => :@proxy
|
||||
|
||||
def run_callbacks(name)
|
||||
if @callbacks[name]
|
||||
@callbacks[name].each do |callback|
|
||||
callback.run(result_instance, self)
|
||||
callback.run(result_instance, anonymous_instance)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -68,10 +68,6 @@ module FactoryGirl
|
|||
|
||||
private
|
||||
|
||||
def method_missing(method, *args, &block)
|
||||
get(method)
|
||||
end
|
||||
|
||||
def process_callbacks(callbacks)
|
||||
callbacks.inject({}) do |result, callback|
|
||||
result[callback.name] ||= []
|
||||
|
@ -92,7 +88,7 @@ module FactoryGirl
|
|||
def initialize(klass)
|
||||
@klass = klass
|
||||
@attributes = []
|
||||
@cached_attribute_values = {}
|
||||
@assigned_attributes = []
|
||||
end
|
||||
|
||||
def to_hash
|
||||
|
@ -118,28 +114,35 @@ module FactoryGirl
|
|||
end
|
||||
|
||||
def get(attribute)
|
||||
@cached_attribute_values[attribute] ||= anonymous_instance.send(attribute)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def define_attribute(attribute, value)
|
||||
anonymous_class.send(:define_method, attribute.name, value)
|
||||
end
|
||||
|
||||
def assign_object_attributes
|
||||
(@attributes - @cached_attribute_values.keys).each do |attribute|
|
||||
@object.send("#{attribute}=", get(attribute))
|
||||
end
|
||||
end
|
||||
|
||||
def anonymous_class
|
||||
@anonymous_class ||= Class.new
|
||||
anonymous_instance.send(attribute)
|
||||
end
|
||||
|
||||
def anonymous_instance
|
||||
@anonymous_instance ||= anonymous_class.new
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def define_attribute(attribute, value)
|
||||
anonymous_class.send(:define_method, attribute.name) {
|
||||
@cached_attributes[attribute.name] ||= instance_exec(&value)
|
||||
}
|
||||
end
|
||||
|
||||
def assign_object_attributes
|
||||
(@attributes - @assigned_attributes).each do |attribute|
|
||||
@assigned_attributes << attribute
|
||||
@object.send("#{attribute}=", get(attribute))
|
||||
end
|
||||
end
|
||||
|
||||
def anonymous_class
|
||||
@anonymous_class ||= Class.new do
|
||||
def initialize
|
||||
@cached_attributes = {}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -20,8 +20,8 @@ describe FactoryGirl::Attribute::Dynamic do
|
|||
context "with a block returning its block-level variable" do
|
||||
let(:block) { lambda {|thing| thing } }
|
||||
|
||||
it "returns the proxy when executing the proc" do
|
||||
subject.to_proc(proxy).call.should == proxy
|
||||
it "returns self when executing the proc" do
|
||||
subject.to_proc(proxy).call.should == subject
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -30,7 +30,7 @@ describe FactoryGirl::Attribute::Dynamic do
|
|||
let(:result) { "other attribute value" }
|
||||
|
||||
before do
|
||||
proxy.stubs(:attribute_defined_on_proxy => result)
|
||||
subject.stubs(:attribute_defined_on_proxy => result)
|
||||
end
|
||||
|
||||
it "evaluates the attribute from the proxy" do
|
||||
|
|
|
@ -10,11 +10,6 @@ describe FactoryGirl::Proxy do
|
|||
expect { subject.set(name_attribute, lambda { "a name" }) }.to_not raise_error
|
||||
end
|
||||
|
||||
it "calls get for a missing method" do
|
||||
subject.stubs(:get).with(:name).returns("it's a name")
|
||||
subject.name.should == "it's a name"
|
||||
end
|
||||
|
||||
it "raises an error when asking for the result" do
|
||||
expect { subject.result(nil) }.to raise_error(NotImplementedError)
|
||||
end
|
||||
|
|
|
@ -73,7 +73,7 @@ shared_examples_for "proxy with standard getters and setters" do |attribute, val
|
|||
subject.result(lambda {|instance| instance })
|
||||
end
|
||||
|
||||
its(attribute) { should == value }
|
||||
its(:"anonymous_instance.#{attribute}") { should == value }
|
||||
it { instance.should have_received(:"#{attribute}=").with(value) }
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue