2008-05-28 18:20:25 -04:00
|
|
|
class Factory
|
2008-07-22 19:46:01 -04:00
|
|
|
|
|
|
|
class AttributeDefinitionError < RuntimeError
|
|
|
|
end
|
|
|
|
|
2008-06-01 13:46:50 -04:00
|
|
|
cattr_accessor :factories, :sequences #:nodoc:
|
2008-05-28 22:09:30 -04:00
|
|
|
self.factories = {}
|
2008-06-01 13:46:50 -04:00
|
|
|
self.sequences = {}
|
2008-05-28 18:54:54 -04:00
|
|
|
|
2008-06-23 18:17:01 -04:00
|
|
|
attr_reader :factory_name
|
2008-05-28 18:54:54 -04:00
|
|
|
|
2008-05-31 18:16:59 -04:00
|
|
|
# Defines a new factory that can be used by the build strategies (create and
|
|
|
|
# build) to build new objects.
|
2008-05-28 18:54:54 -04:00
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# name: (Symbol)
|
|
|
|
# A unique name used to identify this factory.
|
2008-05-28 21:07:51 -04:00
|
|
|
# options: (Hash)
|
|
|
|
# class: the class that will be used when generating instances for this
|
2008-05-29 01:11:33 -04:00
|
|
|
# factory. If not specified, the class will be guessed from the
|
|
|
|
# factory name.
|
2008-05-28 18:54:54 -04:00
|
|
|
#
|
|
|
|
# Yields:
|
|
|
|
# The newly created factory (Factory)
|
2008-05-28 21:07:51 -04:00
|
|
|
def self.define (name, options = {})
|
|
|
|
instance = Factory.new(name, options)
|
2008-05-28 18:54:54 -04:00
|
|
|
yield(instance)
|
2008-07-29 14:22:18 -04:00
|
|
|
self.factories[instance.factory_name] = instance
|
2008-05-28 18:54:54 -04:00
|
|
|
end
|
|
|
|
|
2008-06-01 13:46:50 -04:00
|
|
|
# Defines a new sequence that can be used to generate unique values in a specific format.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# name: (Symbol)
|
|
|
|
# A unique name for this sequence. This name will be referenced when
|
|
|
|
# calling next to generate new values from this sequence.
|
|
|
|
# block: (Proc)
|
|
|
|
# The code to generate each value in the sequence. This block will be
|
|
|
|
# called with a unique number each time a value in the sequence is to be
|
|
|
|
# generated. The block should return the generated value for the
|
|
|
|
# sequence.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
|
|
|
|
def self.sequence (name, &block)
|
|
|
|
self.sequences[name] = Sequence.new(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Generates and returns the next value in a sequence.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# name: (Symbol)
|
|
|
|
# The name of the sequence that a value should be generated for.
|
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# The next value in the sequence. (Object)
|
|
|
|
def self.next (sequence)
|
|
|
|
unless self.sequences.key?(sequence)
|
|
|
|
raise "No such sequence: #{sequence}"
|
|
|
|
end
|
|
|
|
|
|
|
|
self.sequences[sequence].next
|
|
|
|
end
|
|
|
|
|
2008-05-29 01:11:33 -04:00
|
|
|
def build_class #:nodoc:
|
2008-07-29 16:20:44 -04:00
|
|
|
@build_class ||= class_for(@options[:class] || factory_name)
|
2008-05-28 21:07:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize (name, options = {}) #:nodoc:
|
|
|
|
options.assert_valid_keys(:class)
|
2008-07-29 16:20:44 -04:00
|
|
|
@factory_name = factory_name_for(name)
|
2008-06-23 18:17:01 -04:00
|
|
|
@options = options
|
2008-05-28 21:07:51 -04:00
|
|
|
|
2008-05-31 20:12:33 -04:00
|
|
|
@static_attributes = {}
|
|
|
|
@lazy_attribute_blocks = {}
|
|
|
|
@lazy_attribute_names = []
|
2008-05-28 20:00:46 -04:00
|
|
|
end
|
|
|
|
|
2008-05-31 18:16:59 -04:00
|
|
|
# Adds an attribute that should be assigned on generated instances for this
|
|
|
|
# factory.
|
2008-05-28 20:00:46 -04:00
|
|
|
#
|
|
|
|
# This method should be called with either a value or block, but not both. If
|
|
|
|
# called with a block, the attribute will be generated "lazily," whenever an
|
|
|
|
# instance is generated. Lazy attribute blocks will not be called if that
|
|
|
|
# attribute is overriden for a specific instance.
|
|
|
|
#
|
2008-05-31 18:16:59 -04:00
|
|
|
# When defining lazy attributes, an instance of Factory::AttributeProxy will
|
|
|
|
# be yielded, allowing associations to be built using the correct build
|
|
|
|
# strategy.
|
|
|
|
#
|
2008-05-28 20:00:46 -04:00
|
|
|
# Arguments:
|
|
|
|
# name: (Symbol)
|
|
|
|
# The name of this attribute. This will be assigned using :"#{name}=" for
|
2008-05-29 01:11:33 -04:00
|
|
|
# generated instances.
|
2008-05-28 20:00:46 -04:00
|
|
|
# value: (Object)
|
|
|
|
# If no block is given, this value will be used for this attribute.
|
|
|
|
def add_attribute (name, value = nil, &block)
|
2008-07-29 14:35:17 -04:00
|
|
|
name = name.to_sym
|
2008-07-29 16:45:39 -04:00
|
|
|
|
2008-07-22 19:46:01 -04:00
|
|
|
if name.to_s =~ /=$/
|
2008-07-29 16:45:39 -04:00
|
|
|
raise AttributeDefinitionError,
|
|
|
|
"factory_girl uses 'f.#{name.to_s.chop} #{value}' syntax " +
|
|
|
|
"rather than 'f.#{name} #{value}'"
|
2008-07-22 19:46:01 -04:00
|
|
|
end
|
|
|
|
|
2008-05-28 20:00:46 -04:00
|
|
|
if block_given?
|
|
|
|
unless value.nil?
|
|
|
|
raise ArgumentError, "Both value and block given"
|
|
|
|
end
|
2008-05-31 20:12:33 -04:00
|
|
|
@lazy_attribute_blocks[name] = block
|
|
|
|
@lazy_attribute_names << name
|
2008-05-28 20:00:46 -04:00
|
|
|
else
|
|
|
|
@static_attributes[name] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-28 21:14:57 -04:00
|
|
|
# Calls add_attribute using the missing method name as the name of the
|
|
|
|
# attribute, so that:
|
|
|
|
#
|
|
|
|
# Factory.define :user do |f|
|
|
|
|
# f.name 'Billy Idol'
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# and:
|
|
|
|
#
|
|
|
|
# Factory.define :user do |f|
|
2008-06-10 15:38:18 -04:00
|
|
|
# f.add_attribute :name, 'Billy Idol'
|
2008-05-28 21:14:57 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# are equivilent.
|
|
|
|
def method_missing (name, *args, &block)
|
|
|
|
add_attribute(name, *args, &block)
|
|
|
|
end
|
|
|
|
|
2008-07-29 15:53:47 -04:00
|
|
|
# Adds an attribute that builds an association. The associated instance will
|
|
|
|
# be built using the same build strategy as the parent instance.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# Factory.define :user do |f|
|
|
|
|
# f.name 'Joey'
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Factory.define :post do |f|
|
|
|
|
# f.association :author, :factory => :user
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# name: (Symbol)
|
|
|
|
# The name of this attribute.
|
|
|
|
# options: (Hash)
|
|
|
|
# factory: (Symbol)
|
|
|
|
# The name of the factory to use when building the associated instance.
|
|
|
|
# If no name is given, the name of the attribute is assumed to be the
|
|
|
|
# name of the factory. For example, a "user" association will by
|
|
|
|
# default use the "user" factory.
|
|
|
|
def association (name, options = {})
|
|
|
|
name = name.to_sym
|
|
|
|
options = options.symbolize_keys
|
|
|
|
association_factory = options[:factory] || name
|
|
|
|
|
|
|
|
add_attribute(name) {|a| a.association(association_factory) }
|
|
|
|
end
|
|
|
|
|
2008-05-29 01:11:33 -04:00
|
|
|
def attributes_for (attrs = {}) #:nodoc:
|
2008-05-31 18:16:59 -04:00
|
|
|
build_attributes_hash(attrs, :attributes_for)
|
2008-05-28 18:54:54 -04:00
|
|
|
end
|
|
|
|
|
2008-05-29 01:11:33 -04:00
|
|
|
def build (attrs = {}) #:nodoc:
|
2008-05-31 18:16:59 -04:00
|
|
|
build_instance(attrs, :build)
|
2008-05-28 21:07:51 -04:00
|
|
|
end
|
|
|
|
|
2008-05-29 01:11:33 -04:00
|
|
|
def create (attrs = {}) #:nodoc:
|
2008-05-31 18:16:59 -04:00
|
|
|
instance = build_instance(attrs, :create)
|
2008-05-28 21:07:51 -04:00
|
|
|
instance.save!
|
|
|
|
instance
|
|
|
|
end
|
|
|
|
|
2008-05-28 22:09:30 -04:00
|
|
|
class << self
|
|
|
|
|
2008-05-29 01:11:33 -04:00
|
|
|
# Generates and returns a Hash of attributes from this factory. Attributes
|
|
|
|
# can be individually overridden by passing in a Hash of attribute => value
|
|
|
|
# pairs.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# attrs: (Hash)
|
|
|
|
# Attributes to overwrite for this set.
|
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# A set of attributes that can be used to build an instance of the class
|
|
|
|
# this factory generates. (Hash)
|
2008-05-28 22:22:48 -04:00
|
|
|
def attributes_for (name, attrs = {})
|
|
|
|
factory_by_name(name).attributes_for(attrs)
|
2008-05-28 22:09:30 -04:00
|
|
|
end
|
|
|
|
|
2008-05-29 01:11:33 -04:00
|
|
|
# Generates and returns an instance from this factory. Attributes can be
|
|
|
|
# individually overridden by passing in a Hash of attribute => value pairs.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# attrs: (Hash)
|
|
|
|
# See attributes_for
|
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# An instance of the class this factory generates, with generated
|
|
|
|
# attributes assigned.
|
2008-05-28 22:09:30 -04:00
|
|
|
def build (name, attrs = {})
|
|
|
|
factory_by_name(name).build(attrs)
|
|
|
|
end
|
|
|
|
|
2008-05-29 01:11:33 -04:00
|
|
|
# Generates, saves, and returns an instance from this factory. Attributes can
|
|
|
|
# be individually overridden by passing in a Hash of attribute => value
|
|
|
|
# pairs.
|
|
|
|
#
|
|
|
|
# If the instance is not valid, an ActiveRecord::Invalid exception will be
|
|
|
|
# raised.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# attrs: (Hash)
|
|
|
|
# See attributes_for
|
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# A saved instance of the class this factory generates, with generated
|
|
|
|
# attributes assigned.
|
2008-05-28 22:09:30 -04:00
|
|
|
def create (name, attrs = {})
|
|
|
|
factory_by_name(name).create(attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def factory_by_name (name)
|
2008-07-29 14:22:18 -04:00
|
|
|
factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}")
|
2008-05-28 22:09:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2008-05-31 18:16:59 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def build_attributes_hash (override, strategy)
|
2008-07-29 14:35:17 -04:00
|
|
|
override = override.symbolize_keys
|
2008-05-31 20:12:33 -04:00
|
|
|
result = @static_attributes.merge(override)
|
|
|
|
@lazy_attribute_names.each do |name|
|
|
|
|
proxy = AttributeProxy.new(self, name, strategy, result)
|
|
|
|
result[name] = @lazy_attribute_blocks[name].call(proxy) unless override.key?(name)
|
2008-05-31 18:16:59 -04:00
|
|
|
end
|
2008-05-31 20:12:33 -04:00
|
|
|
result
|
2008-05-31 18:16:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_instance (override, strategy)
|
|
|
|
instance = build_class.new
|
|
|
|
attrs = build_attributes_hash(override, strategy)
|
|
|
|
attrs.each do |attr, value|
|
|
|
|
instance.send(:"#{attr}=", value)
|
|
|
|
end
|
|
|
|
instance
|
|
|
|
end
|
|
|
|
|
2008-07-29 16:20:44 -04:00
|
|
|
def class_for (class_or_to_s)
|
|
|
|
if class_or_to_s.respond_to?(:to_sym)
|
|
|
|
class_or_to_s.to_s.classify.constantize
|
|
|
|
else
|
|
|
|
class_or_to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def factory_name_for (class_or_to_s)
|
|
|
|
if class_or_to_s.respond_to?(:to_sym)
|
|
|
|
class_or_to_s.to_sym
|
|
|
|
else
|
|
|
|
class_or_to_s.to_s.underscore.to_sym
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-28 18:20:25 -04:00
|
|
|
end
|