Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replace AssociationRunner with more generic FactoryRunner

  • Loading branch information...
commit 4145a6e7448cbbe2de6e6986520bdf2a1074f6ce 1 parent 09e1cea
@joshuaclayton joshuaclayton authored
View
3  lib/factory_girl.rb
@@ -1,7 +1,8 @@
require "active_support/core_ext/module/delegation"
require 'factory_girl/errors'
-require 'factory_girl/association_runner'
+require 'factory_girl/factory_runner'
+require 'factory_girl/strategy_calculator'
require 'factory_girl/strategy'
require 'factory_girl/registry'
require 'factory_girl/null_factory'
View
50 lib/factory_girl/association_runner.rb
@@ -1,50 +0,0 @@
-module FactoryGirl
- class AssociationRunner
- def initialize(factory_name, strategy_name_or_object, overrides)
- @factory_name = factory_name
- @strategy_name_or_object = strategy_name_or_object
- @overrides = overrides
- end
-
- def run(strategy_override = nil)
- strategy_name_or_object = strategy_override || @strategy_name_or_object
- strategy = StrategyCalculator.new(strategy_name_or_object).strategy
- factory.run(strategy, @overrides)
- end
-
- private
-
- def factory
- FactoryGirl.factory_by_name(@factory_name)
- end
-
- class StrategyCalculator
- def initialize(name_or_object)
- @name_or_object = name_or_object
- end
-
- def strategy
- if strategy_is_object?
- @name_or_object
- else
- strategy_name_to_object
- end
- end
-
- private
-
- def strategy_is_object?
- @name_or_object.is_a?(Class) && @name_or_object.ancestors.include?(::FactoryGirl::Strategy)
- end
-
- def strategy_name_to_object
- case @name_or_object
- when :build then Strategy::Build
- when :create then Strategy::Create
- when nil then Strategy::Create
- else raise "unrecognized method #{@name_or_object}"
- end
- end
- end
- end
-end
View
5 lib/factory_girl/evaluator.rb
@@ -36,9 +36,12 @@ def association(factory_name, overrides = {})
overrides[:method]
elsif overrides.has_key?(:strategy)
overrides[:strategy]
+ else
+ Strategy::Create
end
- runner = AssociationRunner.new(factory_name, build_strategy, overrides.except(:method, :strategy))
+ build_strategy = StrategyCalculator.new(build_strategy).strategy
+ runner = FactoryRunner.new(factory_name, build_strategy, [overrides.except(:method, :strategy)])
@build_strategy.association(runner)
end
View
26 lib/factory_girl/factory_runner.rb
@@ -0,0 +1,26 @@
+module FactoryGirl
+ class FactoryRunner
+ def initialize(name, strategy, traits_and_overrides)
+ @name = name
+ @strategy = strategy
+
+ @overrides = if traits_and_overrides.last.respond_to?(:has_key?)
+ traits_and_overrides.pop
+ else
+ {}
+ end
+ @traits = traits_and_overrides
+ end
+
+ def run(strategy_override = nil, &block)
+ strategy_override ||= @strategy
+ factory = FactoryGirl.factory_by_name(@name)
+
+ if @traits.any?
+ factory = factory.with_traits(@traits)
+ end
+
+ factory.run(strategy_override, @overrides, &block)
+ end
+ end
+end
View
29 lib/factory_girl/strategy_calculator.rb
@@ -0,0 +1,29 @@
+module FactoryGirl
+ class StrategyCalculator
+ def initialize(name_or_object)
+ @name_or_object = name_or_object
+ end
+
+ def strategy
+ if strategy_is_object?
+ @name_or_object
+ else
+ strategy_name_to_object
+ end
+ end
+
+ private
+
+ def strategy_is_object?
+ @name_or_object.is_a?(Class) && @name_or_object.ancestors.include?(::FactoryGirl::Strategy)
+ end
+
+ def strategy_name_to_object
+ case @name_or_object
+ when :build then Strategy::Build
+ when :create then Strategy::Create
+ else raise "unrecognized method #{@name_or_object}"
+ end
+ end
+ end
+end
View
9 lib/factory_girl/syntax/generate.rb
@@ -42,23 +42,20 @@ def self.included(base) # :nodoc:
module ClassMethods #:nodoc:
def generate(overrides = {}, &block)
- factory = FactoryGirl.factory_by_name(name.underscore)
- instance = factory.run(Strategy::Build, overrides)
+ instance = FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
instance.save
yield(instance) if block_given?
instance
end
def generate!(overrides = {}, &block)
- factory = FactoryGirl.factory_by_name(name.underscore)
- instance = factory.run(Strategy::Create, overrides)
+ instance = FactoryRunner.new(name.underscore, Strategy::Create, [overrides]).run
yield(instance) if block_given?
instance
end
def spawn(overrides = {}, &block)
- factory = FactoryGirl.factory_by_name(name.underscore)
- instance = factory.run(Strategy::Build, overrides)
+ instance = FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
yield(instance) if block_given?
instance
end
View
4 lib/factory_girl/syntax/make.rb
@@ -28,11 +28,11 @@ def self.included(base) # :nodoc:
module ClassMethods #:nodoc:
def make(overrides = {})
- FactoryGirl.factory_by_name(name.underscore).run(Strategy::Build, overrides)
+ FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
end
def make!(overrides = {})
- FactoryGirl.factory_by_name(name.underscore).run(Strategy::Create, overrides)
+ FactoryRunner.new(name.underscore, Strategy::Create, [overrides]).run
end
end
View
26 lib/factory_girl/syntax/methods.rb
@@ -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_strategy(name, Strategy::AttributesFor, traits_and_overrides, &block)
+ FactoryRunner.new(name, Strategy::AttributesFor, traits_and_overrides).run(&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_strategy(name, Strategy::Build, traits_and_overrides, &block)
+ FactoryRunner.new(name, Strategy::Build, traits_and_overrides).run(&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_strategy(name, Strategy::Create, traits_and_overrides, &block)
+ FactoryRunner.new(name, Strategy::Create, traits_and_overrides).run(&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_strategy(name, Strategy::Stub, traits_and_overrides, &block)
+ FactoryRunner.new(name, Strategy::Stub, traits_and_overrides).run(&block)
end
# Builds and returns multiple instances from this factory as an array. Attributes can be
@@ -131,24 +131,6 @@ def create_list(name, amount, *traits_and_overrides)
def generate(name)
FactoryGirl.sequence_by_name(name).next
end
-
- private
-
- def run_factory_girl_strategy(name, strategy, traits_and_overrides, &block)
- overrides = if traits_and_overrides.last.respond_to?(:has_key?)
- traits_and_overrides.pop
- else
- {}
- end
-
- factory = FactoryGirl.factory_by_name(name)
-
- if traits_and_overrides.any?
- factory = factory.with_traits(traits_and_overrides)
- end
-
- factory.run(strategy, overrides, &block)
- end
end
end
end
View
31 spec/factory_girl/association_runner_spec.rb
@@ -1,31 +0,0 @@
-require "spec_helper"
-
-describe FactoryGirl::AssociationRunner do
- let(:factory) { stub("factory", :run => instance) }
- let(:instance) { stub("instance") }
-
- before do
- FactoryGirl.stubs(:factory_by_name => factory)
- end
-
- it "passes all overrides to the factory" do
- FactoryGirl::AssociationRunner.new(:user, FactoryGirl::Strategy::Build, :strategy => :build, :name => "John").run
- factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :strategy => :build, :name => "John")
- end
-
- it "runs a strategy inferred by name based on a factory name" do
- FactoryGirl::AssociationRunner.new(:user, :build, :strategy => :build, :name => "John").run
- factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :strategy => :build, :name => "John")
- end
-
- it "allows overriding strategy" do
- FactoryGirl::AssociationRunner.new(:user, :build, :strategy => :build, :name => "John").run(FactoryGirl::Strategy::Create)
- factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :strategy => :build, :name => "John")
- end
-
- it "raises if the strategy cannot be inferred" do
- expect do
- FactoryGirl::AssociationRunner.new(:user, :bogus_strategy, :strategy => :build, :name => "John").run
- end.to raise_error("unrecognized method bogus_strategy")
- end
-end
View
33 spec/factory_girl/strategy_calculator_spec.rb
@@ -0,0 +1,33 @@
+require "spec_helper"
+
+describe FactoryGirl::StrategyCalculator, "with a FactoryGirl::Strategy object" do
+ let(:strategy) { FactoryGirl::Strategy::Build }
+
+ it "returns the strategy object" do
+ FactoryGirl::StrategyCalculator.new(strategy).strategy.should == strategy
+ end
+end
+
+describe FactoryGirl::StrategyCalculator, "with a non-FactoryGirl::Strategy object" do
+ before { define_class "MyAwesomeStrategy" }
+
+ let(:strategy) { MyAwesomeStrategy }
+
+ it "returns the strategy object" do
+ expect { FactoryGirl::StrategyCalculator.new(strategy).strategy }.to raise_error "unrecognized method MyAwesomeStrategy"
+ end
+end
+
+describe FactoryGirl::StrategyCalculator do
+ it "returns the correct strategy object for :build" do
+ FactoryGirl::StrategyCalculator.new(:build).strategy.should == FactoryGirl::Strategy::Build
+ end
+
+ it "returns the correct strategy object for :create" do
+ FactoryGirl::StrategyCalculator.new(:create).strategy.should == FactoryGirl::Strategy::Create
+ end
+
+ it "raises when passing a bogus strategy" do
+ expect { FactoryGirl::StrategyCalculator.new(:bogus_strategy).strategy }.to raise_error "unrecognized method bogus_strategy"
+ end
+end
View
8 spec/support/shared_examples/strategy.rb
@@ -2,7 +2,7 @@
let(:attribute) { FactoryGirl::Attribute::Association.new(:user, :user, {}) }
def association_named(name, overrides)
- runner = FactoryGirl::AssociationRunner.new(name, :build, overrides)
+ runner = FactoryGirl::FactoryRunner.new(name, FactoryGirl::Strategy::Build, [overrides])
subject.association(runner)
end
@@ -21,7 +21,7 @@ def association_named(name, overrides)
let(:factory) { stub("associate_factory") }
def association_named(name, strategy, overrides)
- runner = FactoryGirl::AssociationRunner.new(name, strategy, overrides)
+ runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides])
subject.association(runner)
end
@@ -45,8 +45,8 @@ def association_named(name, strategy, overrides)
let(:factory) { stub("associate_factory") }
def association_named(name, overrides)
- strategy = overrides[:strategy] || overrides[:method]
- runner = FactoryGirl::AssociationRunner.new(name, strategy, overrides.except(:strategy, :method))
+ strategy = FactoryGirl::StrategyCalculator.new(overrides[:strategy] || overrides[:method]).strategy
+ runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides.except(:strategy, :method)])
subject.association(runner)
end
Please sign in to comment.
Something went wrong with that request. Please try again.