Permalink
Browse files

wip: Use registry for factories

  • Loading branch information...
1 parent 73d4f56 commit d901e00656ab8e54dcb6f1c4ae7191e84e979156 @jferris jferris committed Jan 27, 2011
@@ -1,29 +1,14 @@
module FactoryGirl
- class << self
- attr_accessor :factories #:nodoc:
+ def self.factories
+ puts "WARNING: FactoryGirl.factories is deprecated."
+ puts "Use FactoryGirl.registry instead."
+ registry
end
- self.factories = {}
-
def self.factory_by_name(name)
- factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}")
- end
-
- def self.register_factory(factory, options = {})
- if options[:as]
- name = options[:as]
- else
- name = factory.name
- factory.aliases.each do |alias_name|
- register_factory(factory, :as => alias_name)
- end
- end
-
- if self.factories[name]
- raise DuplicateDefinitionError, "Factory already defined: #{name}"
- end
-
- self.factories[name] = factory
+ puts "WARNING: FactoryGirl.factory_by_name is deprecated."
+ puts "Use FactoryGirl.find instead."
+ registry.find(name)
end
# Raised when a factory is defined that attempts to instantiate itself.
@@ -14,12 +14,12 @@ def set(attribute, value)
end
def associate(name, factory_name, overrides)
- factory = FactoryGirl.factory_by_name(factory_name)
+ factory = FactoryGirl.find(factory_name)
set(name, factory.run(Proxy::Create, overrides))
end
def association(factory_name, overrides = {})
- factory = FactoryGirl.factory_by_name(factory_name)
+ factory = FactoryGirl.find(factory_name)
factory.run(Proxy::Create, overrides)
end
@@ -46,12 +46,12 @@ def set(attribute, value)
end
def associate(name, factory_name, overrides)
- factory = FactoryGirl.factory_by_name(factory_name)
+ factory = FactoryGirl.find(factory_name)
set(name, factory.run(Proxy::Stub, overrides))
end
def association(factory_name, overrides = {})
- factory = FactoryGirl.factory_by_name(factory_name)
+ factory = FactoryGirl.find(factory_name)
factory.run(Proxy::Stub, overrides)
end
@@ -13,7 +13,7 @@ def add(item)
end
def find(name)
- @items[name.to_sym]
+ @items[name.to_sym] or raise ArgumentError.new("Not registered: #{name.to_s}")
end
def each(&block)
@@ -39,7 +39,7 @@ def add_as(name, item)
end
end
- def self.add(item)
+ def self.register(item)
registry.add(item)
end
@@ -29,15 +29,9 @@ def aliases
end
- class << self
- attr_writer :sequences
- end
-
def self.sequences
puts "WARNING: FactoryGirl.sequences is deprecated."
puts "Use FactoryGirl.registry instead."
- @sequences
+ registry
end
-
- self.sequences = {}
end
@@ -3,7 +3,7 @@ module FactoryGirlStepHelpers
def convert_association_string_to_instance(factory_name, assignment)
attribute, value = assignment.split(':', 2)
return if value.blank?
- factory = FactoryGirl.factory_by_name(factory_name)
+ factory = FactoryGirl.find(factory_name)
attributes = convert_human_hash_to_attribute_hash({attribute => value.strip}, factory.associations)
attributes_find = {}
attributes.each do |k, v|
@@ -28,7 +28,7 @@ def convert_human_hash_to_attribute_hash(human_hash, associations = [])
World(FactoryGirlStepHelpers)
-FactoryGirl.factories.values.each do |factory|
+FactoryGirl.registry.each do |name, factory|
Given /^the following (?:#{factory.human_name}|#{factory.human_name.pluralize}) exists?:$/ do |table|
table.hashes.each do |human_hash|
attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
@@ -29,7 +29,7 @@ def blueprint(&block)
instance = Factory.new(name.underscore, :class => self)
proxy = FactoryGirl::DefinitionProxy.new(instance)
proxy.instance_eval(&block)
- FactoryGirl.register_factory(instance)
+ FactoryGirl.register(instance)
end
end
@@ -15,13 +15,13 @@ def factory(name, options = {}, &block)
proxy = FactoryGirl::DefinitionProxy.new(factory)
proxy.instance_eval(&block)
if parent = options.delete(:parent)
- factory.inherit_from(FactoryGirl.factory_by_name(parent))
+ factory.inherit_from(FactoryGirl.find(parent))
end
- FactoryGirl.register_factory(factory)
+ FactoryGirl.register(factory)
end
def sequence(name, start_value = 1, &block)
- FactoryGirl.add(Sequence.new(name, start_value, &block))
+ FactoryGirl.register(Sequence.new(name, start_value, &block))
end
end
end
@@ -42,22 +42,22 @@ def self.included(base) # :nodoc:
module ClassMethods #:nodoc:
def generate(overrides = {}, &block)
- factory = FactoryGirl.factory_by_name(name.underscore)
+ factory = FactoryGirl.find(name.underscore)
instance = factory.run(Proxy::Build, overrides)
instance.save
yield(instance) if block_given?
instance
end
def generate!(overrides = {}, &block)
- factory = FactoryGirl.factory_by_name(name.underscore)
+ factory = FactoryGirl.find(name.underscore)
instance = factory.run(Proxy::Create, overrides)
yield(instance) if block_given?
instance
end
def spawn(overrides = {}, &block)
- factory = FactoryGirl.factory_by_name(name.underscore)
+ factory = FactoryGirl.find(name.underscore)
instance = factory.run(Proxy::Build, overrides)
yield(instance) if block_given?
instance
@@ -28,7 +28,7 @@ def self.included(base) # :nodoc:
module ClassMethods #:nodoc:
def make(overrides = {})
- FactoryGirl.factory_by_name(name.underscore).run(Proxy::Create, overrides)
+ FactoryGirl.find(name.underscore).run(Proxy::Create, overrides)
end
end
@@ -27,7 +27,7 @@ module Sham #:nodoc:
def self.method_missing(name, *args, &block)
if block_given?
start_value = args.first
- FactoryGirl.add(Sequence.new(name, start_value || 1, &block))
+ FactoryGirl.register(Sequence.new(name, start_value || 1, &block))
else
FactoryGirl.find(name).next
end
@@ -28,9 +28,9 @@ def self.define(name, options = {})
proxy = FactoryGirl::DefinitionProxy.new(factory)
yield(proxy)
if parent = options.delete(:parent)
- factory.inherit_from(FactoryGirl.factory_by_name(parent))
+ factory.inherit_from(FactoryGirl.find(parent))
end
- FactoryGirl.register_factory(factory)
+ FactoryGirl.register(factory)
end
# Generates and returns a Hash of attributes from this factory. Attributes
@@ -47,7 +47,7 @@ def self.define(name, options = {})
# A set of attributes that can be used to build an instance of the class
# this factory generates.
def self.attributes_for(name, overrides = {})
- FactoryGirl.factory_by_name(name).run(Proxy::AttributesFor, overrides)
+ FactoryGirl.find(name).run(Proxy::AttributesFor, overrides)
end
# Generates and returns an instance from this factory. Attributes can be
@@ -63,7 +63,7 @@ def self.attributes_for(name, overrides = {})
# An instance of the class this factory generates, with generated attributes
# assigned.
def self.build(name, overrides = {})
- FactoryGirl.factory_by_name(name).run(Proxy::Build, overrides)
+ FactoryGirl.find(name).run(Proxy::Build, overrides)
end
# Generates, saves, and returns an instance from this factory. Attributes can
@@ -83,7 +83,7 @@ def self.build(name, overrides = {})
# A saved instance of the class this factory generates, with generated
# attributes assigned.
def self.create(name, overrides = {})
- FactoryGirl.factory_by_name(name).run(Proxy::Create, overrides)
+ FactoryGirl.find(name).run(Proxy::Create, overrides)
end
# Generates and returns an object with all attributes from this factory
@@ -99,7 +99,7 @@ def self.create(name, overrides = {})
# Returns: +Object+
# An object with generated attributes stubbed out.
def self.stub(name, overrides = {})
- FactoryGirl.factory_by_name(name).run(Proxy::Stub, overrides)
+ FactoryGirl.find(name).run(Proxy::Stub, overrides)
end
# Executes the default strategy for the given factory. This is usually create,
@@ -114,7 +114,7 @@ def self.stub(name, overrides = {})
# Returns: +Object+
# The result of the default strategy.
def self.default_strategy(name, overrides = {})
- self.send(FactoryGirl.factory_by_name(name).default_strategy, name, overrides)
+ self.send(FactoryGirl.find(name).default_strategy, name, overrides)
end
# Defines a new sequence that can be used to generate unique values in a specific format.
@@ -133,7 +133,7 @@ def self.default_strategy(name, overrides = {})
#
# Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
def self.sequence(name, start_value = 1, &block)
- FactoryGirl.add(Sequence.new(name, start_value, &block))
+ FactoryGirl.register(Sequence.new(name, start_value, &block))
end
# Generates and returns the next value in a sequence.
@@ -12,14 +12,14 @@
Sham.name { "Name" }
Sham.email { "somebody#{rand(5)}@example.com" }
- Sham.user("FOO") { |c| "User-#{c}" }
+ Sham.username("FOO") { |c| "User-#{c}" }
FactoryGirl.define do
factory :user do
first_name { Sham.name }
last_name { Sham.name }
email { Sham.email }
- username { Sham.user }
+ username { Sham.username }
end
end
end
@@ -51,7 +51,7 @@
before do
@parent = FactoryGirl::Factory.new(:object)
@parent.define_attribute(FactoryGirl::Attribute::Static.new(:name, 'value'))
- FactoryGirl.register_factory(@parent)
+ FactoryGirl.register(@parent)
end
it "should raise an ArgumentError when trying to use a non-existent factory as parent" do
@@ -88,21 +88,16 @@
it "should add the factory to the list of factories" do
Factory.define(@name) {|f| }
- @factory.should == FactoryGirl.factories[@name]
- end
-
- it "should allow a factory to be found by name" do
- Factory.define(@name) {|f| }
- FactoryGirl.factory_by_name(@name).should == @factory
+ @factory.should == FactoryGirl.find(@name)
end
end
describe "after defining a factory" do
before do
@name = :user
- @factory = "factory"
+ @factory = FactoryGirl::Factory.new(@name)
- FactoryGirl.factories[@name] = @factory
+ FactoryGirl.register(@factory)
end
it "should use Proxy::AttributesFor for Factory.attributes_for" do
@@ -113,6 +113,7 @@
it "adds an association when passed an undefined method without arguments or a block" do
name = :user
+ FactoryGirl.register(FactoryGirl::Factory.new(name))
attr = 'attribute'
stub(attr).name { name }
mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
@@ -123,7 +124,7 @@
it "adds a sequence when passed an undefined method without arguments or a block" do
name = :airport
proxy = 'proxy'
- FactoryGirl.add(FactoryGirl::Sequence.new(name) { |value| "expected" })
+ FactoryGirl.register(FactoryGirl::Sequence.new(name) { |value| "expected" })
subject.send(name)
stub(proxy).set
factory.attributes.last.add_to(proxy)
@@ -1,33 +1,5 @@
require 'spec_helper'
-describe FactoryGirl::Factory, "registering a factory" do
- before do
- @name = :user
- @factory = "factory"
- @aliases = [:one, :two]
- stub(@factory).name { @name }
- stub(@factory).aliases { @aliases }
- end
-
- it "should add the factory to the list of factories" do
- FactoryGirl.register_factory(@factory)
- FactoryGirl.factory_by_name(@name).should == @factory
- end
-
- it "should not allow a duplicate factory definition" do
- lambda {
- 2.times { FactoryGirl.register_factory(@factory) }
- }.should raise_error(FactoryGirl::DuplicateDefinitionError)
- end
-
- it "registers aliases" do
- FactoryGirl.register_factory(@factory)
- @aliases.each do |name|
- FactoryGirl.factory_by_name(name).should == @factory
- end
- end
-end
-
describe FactoryGirl::Factory do
include DefinesConstants
@@ -313,32 +285,6 @@
end
end
-describe FactoryGirl::Factory, "registered with a string name" do
- before do
- @name = :string
- @factory = FactoryGirl::Factory.new(@name)
- FactoryGirl.register_factory(@factory)
- end
-
- it "should store the factory using a symbol" do
- FactoryGirl.factories[@name].should == @factory
- end
-end
-
-describe FactoryGirl::Factory, "registered with a custom name" do
- before do
- @actual_name = :string
- @custom_name = :words
- @factory = FactoryGirl::Factory.new(@actual_name)
-
- FactoryGirl.register_factory(@factory, :as => @custom_name)
- end
-
- it "finds the factory using the custom name" do
- FactoryGirl.factory_by_name(@custom_name).should == @factory
- end
-end
-
describe FactoryGirl::Factory, "for namespaced class" do
include DefinesConstants
Oops, something went wrong.

0 comments on commit d901e00

Please sign in to comment.