Permalink
Browse files

Proxy => Strategy

  • Loading branch information...
1 parent 9826658 commit b79a525c09d18fa329b7c9e84f0778e4a9d82774 @joshuaclayton joshuaclayton committed Feb 8, 2012
View
@@ -165,7 +165,7 @@ end
Dependent Attributes
--------------------
-Attributes can be based on the values of other attributes using the proxy that is yielded to lazy attribute blocks:
+Attributes can be based on the values of other attributes using the evaluator that is yielded to lazy attribute blocks:
```ruby
factory :user do
@@ -193,8 +193,8 @@ factory :user do
name { "John Doe#{" - Rockstar" if rockstar}" }
email { "#{name.downcase}@example.com" }
- after_create do |user, proxy|
- user.name.upcase! if proxy.upcased
+ after_create do |user, evaluator|
+ user.name.upcase! if evaluator.upcased
end
end
@@ -207,8 +207,8 @@ within attributes\_for and won't be set on the model, even if the attribute
exists or you attempt to override it.
Within Factory Girl's dynamic attributes, you can access ignored attributes as
-you would expect. If you need to access the proxy in a Factory Girl callback,
-you'll need to declare a second block argument (for the proxy) and access
+you would expect. If you need to access the evaluator in a Factory Girl callback,
+you'll need to declare a second block argument (for the evaluator) and access
ignored attributes from there.
Associations
View
@@ -1,7 +1,7 @@
require "active_support/core_ext/module/delegation"
require 'factory_girl/errors'
-require 'factory_girl/proxy'
+require 'factory_girl/strategy'
require 'factory_girl/registry'
require 'factory_girl/null_factory'
require 'factory_girl/null_object'
@@ -22,7 +22,7 @@ def initialize(definition, ignore = false)
# instance is generated. Lazy attribute blocks will not be called if that
# attribute is overridden for a specific instance.
#
- # When defining lazy attributes, an instance of FactoryGirl::Proxy will
+ # When defining lazy attributes, an instance of FactoryGirl::Strategy will
# be yielded, allowing associations to be built using the correct build
# strategy.
#
@@ -36,16 +36,16 @@ def default_strategy #:nodoc:
@default_strategy || parent.default_strategy
end
- def run(proxy_class, overrides, &block) #:nodoc:
+ def run(strategy_class, overrides, &block) #:nodoc:
block ||= lambda {|result| result }
compile
- proxy = proxy_class.new
+ strategy = strategy_class.new
- evaluator = evaluator_class.new(proxy, overrides.symbolize_keys)
+ evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
attribute_assigner = AttributeAssigner.new(evaluator, &instance_builder)
- proxy.result(attribute_assigner, to_create).tap(&block)
+ strategy.result(attribute_assigner, to_create).tap(&block)
end
def human_names
@@ -133,7 +133,7 @@ def assert_valid_options(options)
options.assert_valid_keys(:class, :parent, :default_strategy, :aliases, :traits)
if options[:default_strategy]
- Proxy.ensure_strategy_exists!(options[:default_strategy])
+ Strategy.ensure_strategy_exists!(options[:default_strategy])
$stderr.puts "DEPRECATION WARNING: default_strategy is deprecated."
$stderr.puts "Override to_create if you need to prevent a call to #save!."
end
@@ -1,12 +1,12 @@
require "active_support/core_ext/hash/except"
-require "factory_girl/proxy/build"
-require "factory_girl/proxy/create"
-require "factory_girl/proxy/attributes_for"
-require "factory_girl/proxy/stub"
+require "factory_girl/strategy/build"
+require "factory_girl/strategy/create"
+require "factory_girl/strategy/attributes_for"
+require "factory_girl/strategy/stub"
require "observer"
module FactoryGirl
- class Proxy #:nodoc:
+ class Strategy #:nodoc:
include Observable
# Generates an association using the current build strategy.
@@ -49,7 +49,7 @@ def result(attribute_assigner, to_create)
end
def self.ensure_strategy_exists!(strategy)
- unless Proxy.const_defined? strategy.to_s.camelize
+ unless Strategy.const_defined? strategy.to_s.camelize
raise ArgumentError, "Unknown strategy: #{strategy}"
end
end
@@ -1,6 +1,6 @@
module FactoryGirl
- class Proxy #:nodoc:
- class AttributesFor < Proxy #:nodoc:
+ class Strategy #:nodoc:
+ class AttributesFor < Strategy #:nodoc:
def result(attribute_assigner, to_create)
attribute_assigner.hash
end
@@ -1,6 +1,6 @@
module FactoryGirl
- class Proxy #:nodoc:
- class Build < Proxy #:nodoc:
+ class Strategy #:nodoc:
+ class Build < Strategy #:nodoc:
def association(factory_name, overrides = {})
factory = FactoryGirl.factory_by_name(factory_name)
factory.run(get_method(overrides[:method]), overrides.except(:method))
@@ -16,9 +16,9 @@ def result(attribute_assigner, to_create)
def get_method(method)
case method
- when :build then Proxy::Build
- when :create then Proxy::Create
- when nil then Proxy::Create
+ when :build then Strategy::Build
+ when :create then Strategy::Create
+ when nil then Strategy::Create
else raise "unrecognized method #{method}"
end
end
@@ -1,6 +1,6 @@
module FactoryGirl
- class Proxy #:nodoc:
- class Create < Proxy #:nodoc:
+ class Strategy #:nodoc:
+ class Create < Strategy #:nodoc:
def association(factory_name, overrides = {})
factory = FactoryGirl.factory_by_name(factory_name)
factory.run(get_method(overrides[:method]), overrides.except(:method))
@@ -18,9 +18,9 @@ def result(attribute_assigner, to_create)
def get_method(method)
case method
- when :build then Proxy::Build
- when :create then Proxy::Create
- when nil then Proxy::Create
+ when :build then Strategy::Build
+ when :create then Strategy::Create
+ when nil then Strategy::Create
else raise "unrecognized method #{method}"
end
end
@@ -1,11 +1,11 @@
module FactoryGirl
- class Proxy
- class Stub < Proxy #:nodoc:
+ class Strategy
+ class Stub < Strategy #:nodoc:
@@next_id = 1000
def association(factory_name, overrides = {})
factory = FactoryGirl.factory_by_name(factory_name)
- factory.run(Proxy::Stub, overrides.except(:method))
+ factory.run(Strategy::Stub, overrides.except(:method))
end
def result(attribute_assigner, to_create)
@@ -43,22 +43,22 @@ module ClassMethods #:nodoc:
def generate(overrides = {}, &block)
factory = FactoryGirl.factory_by_name(name.underscore)
- instance = factory.run(Proxy::Build, overrides)
+ instance = factory.run(Strategy::Build, overrides)
instance.save
yield(instance) if block_given?
instance
end
def generate!(overrides = {}, &block)
factory = FactoryGirl.factory_by_name(name.underscore)
- instance = factory.run(Proxy::Create, overrides)
+ instance = factory.run(Strategy::Create, overrides)
yield(instance) if block_given?
instance
end
def spawn(overrides = {}, &block)
factory = FactoryGirl.factory_by_name(name.underscore)
- instance = factory.run(Proxy::Build, overrides)
+ instance = factory.run(Strategy::Build, overrides)
yield(instance) if block_given?
instance
end
@@ -28,11 +28,11 @@ def self.included(base) # :nodoc:
module ClassMethods #:nodoc:
def make(overrides = {})
- FactoryGirl.factory_by_name(name.underscore).run(Proxy::Build, overrides)
+ FactoryGirl.factory_by_name(name.underscore).run(Strategy::Build, overrides)
end
def make!(overrides = {})
- FactoryGirl.factory_by_name(name.underscore).run(Proxy::Create, overrides)
+ FactoryGirl.factory_by_name(name.underscore).run(Strategy::Create, overrides)
end
end
@@ -18,7 +18,7 @@ module Methods
# A set of attributes that can be used to build an instance of the class
# this factory generates.
def attributes_for(name, *traits_and_overrides, &block)
- run_factory_girl_proxy(name, traits_and_overrides, Proxy::AttributesFor, &block)
+ run_factory_girl_strategy(name, traits_and_overrides, Strategy::AttributesFor, &block)
end
# Generates and returns an instance from this factory. Attributes can be
@@ -37,7 +37,7 @@ def attributes_for(name, *traits_and_overrides, &block)
# An instance of the class this factory generates, with generated attributes
# assigned.
def build(name, *traits_and_overrides, &block)
- run_factory_girl_proxy(name, traits_and_overrides, Proxy::Build, &block)
+ run_factory_girl_strategy(name, traits_and_overrides, Strategy::Build, &block)
end
# Generates, saves, and returns an instance from this factory. Attributes can
@@ -60,7 +60,7 @@ def build(name, *traits_and_overrides, &block)
# A saved instance of the class this factory generates, with generated
# attributes assigned.
def create(name, *traits_and_overrides, &block)
- run_factory_girl_proxy(name, traits_and_overrides, Proxy::Create, &block)
+ run_factory_girl_strategy(name, traits_and_overrides, Strategy::Create, &block)
end
# Generates and returns an object with all attributes from this factory
@@ -79,7 +79,7 @@ def create(name, *traits_and_overrides, &block)
# Returns: +Object+
# An object with generated attributes stubbed out.
def build_stubbed(name, *traits_and_overrides, &block)
- run_factory_girl_proxy(name, traits_and_overrides, Proxy::Stub, &block)
+ run_factory_girl_strategy(name, traits_and_overrides, Strategy::Stub, &block)
end
# Builds and returns multiple instances from this factory as an array. Attributes can be
@@ -134,7 +134,7 @@ def generate(name)
private
- def run_factory_girl_proxy(name, traits_and_overrides, proxy, &block)
+ def run_factory_girl_strategy(name, traits_and_overrides, strategy, &block)
overrides = if traits_and_overrides.last.respond_to?(:has_key?)
traits_and_overrides.pop
else
@@ -147,7 +147,7 @@ def run_factory_girl_proxy(name, traits_and_overrides, proxy, &block)
factory = factory.with_traits(traits_and_overrides)
end
- factory.run(proxy, overrides, &block)
+ factory.run(strategy, overrides, &block)
end
end
end
@@ -93,40 +93,40 @@
FactoryGirl.register_factory(@factory)
end
- it "uses Proxy::AttributesFor for Factory.attributes_for" do
+ it "uses Strategy::AttributesFor for Factory.attributes_for" do
@factory.stubs(:run => "result")
Factory.attributes_for(@name, :attr => 'value').should == 'result'
- @factory.should have_received(:run).with(FactoryGirl::Proxy::AttributesFor, :attr => 'value')
+ @factory.should have_received(:run).with(FactoryGirl::Strategy::AttributesFor, :attr => 'value')
end
- it "uses Proxy::Build for Factory.build" do
+ it "uses Strategy::Build for Factory.build" do
@factory.stubs(:run => "result")
Factory.build(@name, :attr => 'value').should == 'result'
- @factory.should have_received(:run).with(FactoryGirl::Proxy::Build, :attr => 'value')
+ @factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :attr => 'value')
end
- it "uses Proxy::Create for Factory.create" do
+ it "uses Strategy::Create for Factory.create" do
@factory.stubs(:run => "result")
Factory.create(@name, :attr => 'value').should == 'result'
- @factory.should have_received(:run).with(FactoryGirl::Proxy::Create, :attr => 'value')
+ @factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :attr => 'value')
end
- it "uses Proxy::Stub for Factory.stub" do
+ it "uses Strategy::Stub for Factory.stub" do
@factory.stubs(:run => "result")
Factory.stub(@name, :attr => 'value').should == 'result'
- @factory.should have_received(:run).with(FactoryGirl::Proxy::Stub, :attr => 'value')
+ @factory.should have_received(:run).with(FactoryGirl::Strategy::Stub, :attr => 'value')
end
it "uses default strategy option as Factory.default_strategy" do
@factory.stubs(:default_strategy => :create, :run => "result")
Factory.default_strategy(@name, :attr => 'value').should == 'result'
- @factory.should have_received(:run).with(FactoryGirl::Proxy::Create, :attr => 'value')
+ @factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :attr => 'value')
end
it "uses the default strategy for the global Factory method" do
@factory.stubs(:default_strategy => :create, :run => "result")
Factory(@name, :attr => 'value').should == 'result'
- @factory.should have_received(:run).with(FactoryGirl::Proxy::Create, :attr => 'value')
+ @factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :attr => 'value')
end
[:build, :create, :attributes_for, :stub].each do |method|
@@ -210,7 +210,7 @@
end
end
-describe "traits added via proxy" do
+describe "traits added via strategy" do
before do
define_model("User", :name => :string, :admin => :boolean)
@@ -17,8 +17,8 @@
name { "#{FactoryGirl.generate(:name)}#{" - Rockstar" if rockstar}" }
email { "#{name.downcase}#{four}@example.com" }
- after_create do |user, proxy|
- user.name.upcase! if proxy.upcased
+ after_create do |user, evaluator|
+ user.name.upcase! if evaluator.upcased
end
end
end
@@ -5,7 +5,6 @@
let(:factory) { :user }
let(:overrides) { { :first_name => "John" } }
let(:association) { stub("association") }
- let(:proxy) { stub("proxy", :association => association) }
subject { FactoryGirl::Attribute::Association.new(name, factory, overrides) }
before { subject.stubs(:association => association) }
@@ -2,7 +2,6 @@
describe FactoryGirl::Attribute do
let(:name) { "user" }
- let(:proxy) { stub("proxy") }
subject { FactoryGirl::Attribute.new(name, false) }
its(:name) { should == name.to_sym }
@@ -2,7 +2,6 @@
describe FactoryGirl::Declaration::Implicit do
let(:name) { :author }
- let(:proxy) { stub("proxy") }
let(:declaration) { FactoryGirl::Declaration::Implicit.new(name) }
subject { declaration.to_attributes.first }
Oops, something went wrong.

0 comments on commit b79a525

Please sign in to comment.