Permalink
Browse files

Remove #set_ignored altogether

  • Loading branch information...
1 parent 3282eea commit d3a7b7e065b246c7a86865791dae4c418d0a3b3c @joshuaclayton joshuaclayton committed Dec 1, 2011
@@ -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
@@ -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
@@ -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
View
@@ -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|
@@ -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:
@@ -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|
@@ -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
@@ -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) }
@@ -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)
@@ -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|

0 comments on commit d3a7b7e

Please sign in to comment.