-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move class creation/handling to an anonymous evaluator
This allows for Attribute#to_proc to not require a proxy to be passed to return a Proc. It also allows for removal of Attribute#add_to
- Loading branch information
1 parent
b339c8f
commit 3282eea
Showing
19 changed files
with
165 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module FactoryGirl | ||
class AnonymousEvaluator | ||
attr_reader :attributes | ||
|
||
def initialize | ||
@attributes = [] | ||
end | ||
|
||
def set(attribute, value) | ||
define_attribute(attribute, value) | ||
@attributes << attribute | ||
end | ||
|
||
def set_ignored(attribute, value) | ||
define_attribute(attribute, value) | ||
end | ||
|
||
def evaluator | ||
@evaluator ||= Class.new do | ||
def initialize | ||
@cached_attributes = {} | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def define_attribute(attribute, value) | ||
evaluator.send(:define_method, attribute) { | ||
@cached_attributes[attribute] ||= instance_exec(&value) | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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 | ||
|
||
it "adds the method to the evaluator" do | ||
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.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.evaluator.new.two.should == 2 | ||
end | ||
end | ||
|
||
describe FactoryGirl::AnonymousEvaluator, "#set_ignored" do | ||
let(:attribute) { :one } | ||
let(:value) { lambda { @result ||= 0; @result += 1 } } | ||
|
||
def set_attribute | ||
subject.set_ignored(attribute, value) | ||
end | ||
|
||
it "adds the method to the evaluator" do | ||
set_attribute | ||
subject.evaluator.new.one.should == 1 | ||
end | ||
|
||
it "does not track the attribute" do | ||
set_attribute | ||
subject.attributes.should == [] | ||
end | ||
|
||
it "caches the result" do | ||
set_attribute | ||
subject.evaluator.new.tap do |obj| | ||
obj.one.should == 1 | ||
obj.one.should == 1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.