Skip to content

Commit

Permalink
Remove #set_ignored altogether
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Dec 1, 2011
1 parent 3282eea commit d3a7b7e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 79 deletions.
16 changes: 6 additions & 10 deletions lib/factory_girl/anonymous_evaluator.rb
Expand Up @@ -6,13 +6,9 @@ def initialize
@attributes = []
end

def set(attribute, value)
define_attribute(attribute, value)
@attributes << attribute
end

def set_ignored(attribute, value)
define_attribute(attribute, value)
def set(attribute)
define_attribute(attribute.name, attribute.to_proc)
@attributes << attribute.name unless attribute.ignored
end

def evaluator
Expand All @@ -25,9 +21,9 @@ def initialize

private

def define_attribute(attribute, value)
evaluator.send(:define_method, attribute) {
@cached_attributes[attribute] ||= instance_exec(&value)
def define_attribute(attribute_name, attribute_proc)
evaluator.send(:define_method, attribute_name) {
@cached_attributes[attribute_name] ||= instance_exec(&attribute_proc)
}
end
end
Expand Down
12 changes: 2 additions & 10 deletions lib/factory_girl/factory.rb
Expand Up @@ -191,19 +191,11 @@ def handle_attribute_with_overrides(attribute)
end

def add_static_attribute(attr, val, ignored = false)
set_attribute_on_proxy(Attribute::Static.new(attr, val, ignored))
proxy.set(Attribute::Static.new(attr, val, ignored))
end

def handle_attribute_without_overrides(attribute)
set_attribute_on_proxy(attribute)
end

def set_attribute_on_proxy(attribute)
if attribute.ignored
proxy.set_ignored(attribute)
else
proxy.set(attribute)
end
proxy.set(attribute)
end

def proxy
Expand Down
12 changes: 3 additions & 9 deletions lib/factory_girl/proxy.rb
Expand Up @@ -11,6 +11,8 @@ def initialize(klass, callbacks = [])
@proxy = ObjectWrapper.new(klass, self)
end

delegate :set, :to => :@proxy

def run_callbacks(name)
if @callbacks[name]
@callbacks[name].each do |callback|
Expand All @@ -19,14 +21,6 @@ def run_callbacks(name)
end
end

def set_ignored(attribute)
@proxy.set_ignored(attribute.name, attribute.to_proc)
end

def set(attribute)
@proxy.set(attribute.name, attribute.to_proc)
end

# Generates an association using the current build strategy.
#
# Arguments:
Expand Down Expand Up @@ -102,7 +96,7 @@ def initialize(klass, proxy)
}
end

delegate :set, :set_ignored, :attributes, :to => :@evaluator
delegate :set, :attributes, :to => :@evaluator

def to_hash
attributes.inject({}) do |result, attribute|
Expand Down
66 changes: 27 additions & 39 deletions spec/factory_girl/anonymous_evaluator_spec.rb
@@ -1,65 +1,53 @@
require "spec_helper"

describe FactoryGirl::AnonymousEvaluator do
its(:attributes) { should == [] }
end

describe FactoryGirl::AnonymousEvaluator, "#set" do
let(:attribute) { :one }
let(:value) { lambda { @result ||= 0; @result += 1 } }

def set_attribute
subject.set(attribute, value)
end

shared_examples "#set on an AnonymousEvaluator" do
it "adds the method to the evaluator" do
set_attribute
subject.set(attribute)
subject.evaluator.new.one.should == 1
end

it "tracks the attribute" do
set_attribute
subject.attributes.should == [attribute]
end

it "caches the result" do
set_attribute
subject.set(attribute)
subject.evaluator.new.tap do |obj|
obj.one.should == 1
obj.one.should == 1
end
end

it "evaluates the block in the context of the evaluator" do
set_attribute
subject.set(:two, lambda { one + 1 })
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
end
end

describe FactoryGirl::AnonymousEvaluator, "#set_ignored" do
let(:attribute) { :one }
let(:value) { lambda { @result ||= 0; @result += 1 } }
describe FactoryGirl::AnonymousEvaluator do
its(:attributes) { should == [] }
end

describe FactoryGirl::AnonymousEvaluator, "#set" do
let(:value) { lambda { @result ||= 0; @result += 1 } }

def set_attribute
subject.set_ignored(attribute, value)
end
context "setting an ignored attribute" do
let(:attribute) { stub("attribute", :name => :one, :to_proc => value, :ignored => true) }

it "adds the method to the evaluator" do
set_attribute
subject.evaluator.new.one.should == 1
end
it_behaves_like "#set on an AnonymousEvaluator"

it "does not track the attribute" do
set_attribute
subject.attributes.should == []
it "does not track the attribute" do
subject.set(attribute)
subject.attributes.should be_empty
end
end

it "caches the result" do
set_attribute
subject.evaluator.new.tap do |obj|
obj.one.should == 1
obj.one.should == 1
context "setting an attribute" do
let(:attribute) { stub("attribute", :name => :one, :to_proc => value, :ignored => false) }

it_behaves_like "#set on an AnonymousEvaluator"

it "tracks the attribute" do
subject.set(attribute)
subject.attributes.should == [:one]
end
end
end
2 changes: 1 addition & 1 deletion spec/factory_girl/proxy/attributes_for_spec.rb
Expand Up @@ -18,7 +18,7 @@
end

describe "after setting an attribute" do
let(:attribute) { stub("attribute", :name => :attribute, :to_proc => lambda { "value" }) }
let(:attribute) { stub("attribute", :name => :attribute, :to_proc => lambda { "value" }, :ignored => false) }

before { subject.set(attribute) }

Expand Down
11 changes: 1 addition & 10 deletions spec/support/shared_examples/proxy.rb
Expand Up @@ -61,7 +61,7 @@
end

shared_examples_for "proxy with standard getters and setters" do |attribute, value|
let(:attribute_instance) { stub("attribute #{attribute}", :name => attribute, :to_proc => lambda { value }) }
let(:attribute_instance) { stub("attribute #{attribute}", :name => attribute, :to_proc => lambda { value }, :ignored => false) }

before do
instance.stubs(:"#{attribute}=" => value, :"#{attribute}" => value)
Expand All @@ -75,15 +75,6 @@

it { instance.should have_received(:"#{attribute}=").with(value) }
end

describe "when setting an ignored attribute" do
before do
subject.set_ignored(attribute_instance)
subject.result(lambda {|instance| instance })
end

it { instance.should have_received(:"#{attribute}=").with(value).never }
end
end

shared_examples_for "proxy with callbacks" do |callback_name|
Expand Down

0 comments on commit d3a7b7e

Please sign in to comment.