2008-05-28 18:20:25 -04:00
|
|
|
class Factory
|
2009-02-17 16:38:15 -05:00
|
|
|
# Raised when a factory is defined that attempts to instantiate itself.
|
2009-01-06 15:57:37 -05:00
|
|
|
class AssociationDefinitionError < RuntimeError
|
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2009-10-10 00:46:19 -04:00
|
|
|
# Raised when a callback is defined that has an invalid name
|
|
|
|
class InvalidCallbackNameError < RuntimeError
|
|
|
|
end
|
2009-10-30 13:25:47 -04:00
|
|
|
|
|
|
|
# Raised when a factory is defined with the same name as a previously-defined factory.
|
|
|
|
class DuplicateDefinitionError < RuntimeError
|
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2008-12-11 15:54:33 -05:00
|
|
|
class << self
|
|
|
|
attr_accessor :factories #:nodoc:
|
|
|
|
|
|
|
|
# An Array of strings specifying locations that should be searched for
|
|
|
|
# factory definitions. By default, factory_girl will attempt to require
|
|
|
|
# "factories," "test/factories," and "spec/factories." Only the first
|
|
|
|
# existing file will be loaded.
|
|
|
|
attr_accessor :definition_file_paths
|
|
|
|
end
|
2008-05-28 18:54:54 -04:00
|
|
|
|
2008-12-11 15:54:33 -05:00
|
|
|
self.factories = {}
|
2008-08-20 10:20:41 -04:00
|
|
|
self.definition_file_paths = %w(factories test/factories spec/factories)
|
|
|
|
|
2009-02-17 16:38:15 -05:00
|
|
|
attr_reader :factory_name #:nodoc:
|
2009-01-02 15:45:34 -05:00
|
|
|
attr_reader :attributes #:nodoc:
|
2008-05-28 18:54:54 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
def self.register_factory(factory)
|
|
|
|
name = factory.factory_name
|
|
|
|
if self.factories[name]
|
2009-10-30 13:25:47 -04:00
|
|
|
raise DuplicateDefinitionError, "Factory already defined: #{name}"
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
self.factories[name] = factory
|
2008-05-28 18:54:54 -04:00
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2009-02-17 16:38:15 -05:00
|
|
|
def class_name #:nodoc:
|
2009-01-07 11:48:29 -05:00
|
|
|
@options[:class] || factory_name
|
|
|
|
end
|
2008-05-28 18:54:54 -04:00
|
|
|
|
2008-05-29 01:11:33 -04:00
|
|
|
def build_class #:nodoc:
|
2009-01-07 11:48:29 -05:00
|
|
|
@build_class ||= class_for(class_name)
|
2008-05-28 21:07:51 -04:00
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2009-01-08 11:43:07 -05:00
|
|
|
def default_strategy #:nodoc:
|
|
|
|
@options[:default_strategy] || :create
|
|
|
|
end
|
2008-05-28 21:07:51 -04:00
|
|
|
|
|
|
|
def initialize (name, options = {}) #:nodoc:
|
2008-12-11 15:54:33 -05:00
|
|
|
assert_valid_options(options)
|
2008-07-29 16:20:44 -04:00
|
|
|
@factory_name = factory_name_for(name)
|
2010-06-07 15:51:18 -04:00
|
|
|
@options = options
|
2008-07-30 13:46:06 -04:00
|
|
|
@attributes = []
|
2008-05-28 20:00:46 -04:00
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2009-01-08 12:55:18 -05:00
|
|
|
def inherit_from(parent) #:nodoc:
|
2010-01-08 00:07:41 -05:00
|
|
|
@options[:class] ||= parent.class_name
|
|
|
|
@options[:default_strategy] ||= parent.default_strategy
|
2009-01-07 11:48:29 -05:00
|
|
|
parent.attributes.each do |attribute|
|
|
|
|
unless attribute_defined?(attribute.name)
|
|
|
|
@attributes << attribute.clone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-05-28 20:00:46 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
def define_attribute(attribute)
|
|
|
|
name = attribute.name
|
|
|
|
# TODO: move these checks into Attribute
|
|
|
|
if attribute_defined?(name)
|
2008-07-30 14:04:43 -04:00
|
|
|
raise AttributeDefinitionError, "Attribute already defined: #{name}"
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
if attribute.respond_to?(:factory) && attribute.factory == self.factory_name
|
2009-01-06 15:57:37 -05:00
|
|
|
raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.factory_name}'"
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
@attributes << attribute
|
2009-10-10 00:46:19 -04:00
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
def add_callback(name, &block)
|
2009-10-10 00:46:19 -04:00
|
|
|
unless [:after_build, :after_create, :after_stub].include?(name.to_sym)
|
|
|
|
raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are :after_build, :after_create, and :after_stub"
|
|
|
|
end
|
|
|
|
@attributes << Attribute::Callback.new(name.to_sym, block)
|
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2008-12-30 16:36:26 -05:00
|
|
|
def self.find_definitions #:nodoc:
|
|
|
|
definition_file_paths.each do |path|
|
|
|
|
require("#{path}.rb") if File.exists?("#{path}.rb")
|
2008-10-24 12:14:13 -04:00
|
|
|
|
2008-12-30 16:36:26 -05:00
|
|
|
if File.directory? path
|
|
|
|
Dir[File.join(path, '*.rb')].each do |file|
|
|
|
|
require file
|
2008-08-20 10:20:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-05-28 22:09:30 -04:00
|
|
|
end
|
|
|
|
|
2009-01-02 16:39:24 -05:00
|
|
|
def run (proxy_class, overrides) #:nodoc:
|
|
|
|
proxy = proxy_class.new(build_class)
|
2008-12-30 16:31:26 -05:00
|
|
|
overrides = symbolize_keys(overrides)
|
2009-01-02 16:39:24 -05:00
|
|
|
overrides.each {|attr, val| proxy.set(attr, val) }
|
2008-12-30 16:31:26 -05:00
|
|
|
passed_keys = overrides.keys.collect {|k| Factory.aliases_for(k) }.flatten
|
2008-07-30 13:46:06 -04:00
|
|
|
@attributes.each do |attribute|
|
2008-07-30 15:47:12 -04:00
|
|
|
unless passed_keys.include?(attribute.name)
|
2009-01-02 17:33:00 -05:00
|
|
|
attribute.add_to(proxy)
|
2008-07-30 13:46:06 -04:00
|
|
|
end
|
2008-05-31 18:16:59 -04:00
|
|
|
end
|
2009-01-02 16:39:24 -05:00
|
|
|
proxy.result
|
2008-05-31 18:16:59 -04:00
|
|
|
end
|
|
|
|
|
2009-01-02 13:40:57 -05:00
|
|
|
def self.factory_by_name (name)
|
|
|
|
factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}")
|
|
|
|
end
|
2009-09-15 15:47:47 -04:00
|
|
|
|
|
|
|
def human_name(*args, &block)
|
|
|
|
if args.size == 0 && block.nil?
|
|
|
|
factory_name.to_s.gsub('_', ' ')
|
|
|
|
else
|
|
|
|
add_attribute(:human_name, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-15 16:56:20 -04:00
|
|
|
def associations
|
|
|
|
attributes.select {|attribute| attribute.is_a?(Attribute::Association) }
|
|
|
|
end
|
|
|
|
|
2009-09-15 15:47:47 -04:00
|
|
|
private
|
|
|
|
|
2008-07-29 16:20:44 -04:00
|
|
|
def class_for (class_or_to_s)
|
|
|
|
if class_or_to_s.respond_to?(:to_sym)
|
2010-03-17 18:49:35 -04:00
|
|
|
class_name = variable_name_to_class_name(class_or_to_s)
|
|
|
|
class_name.split('::').inject(Object) do |object, string|
|
|
|
|
object.const_get(string)
|
|
|
|
end
|
2008-07-29 16:20:44 -04:00
|
|
|
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
|
2008-12-11 15:54:33 -05:00
|
|
|
class_name_to_variable_name(class_or_to_s).to_sym
|
2008-07-29 16:20:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-30 14:04:43 -04:00
|
|
|
def attribute_defined? (name)
|
2009-10-10 01:23:57 -04:00
|
|
|
!@attributes.detect {|attr| attr.name == name && !attr.is_a?(Factory::Attribute::Callback) }.nil?
|
2008-07-30 14:04:43 -04:00
|
|
|
end
|
|
|
|
|
2008-12-11 15:54:33 -05:00
|
|
|
def assert_valid_options(options)
|
2010-06-07 15:51:18 -04:00
|
|
|
invalid_keys = options.keys - [:class, :parent, :default_strategy]
|
2008-12-11 15:54:33 -05:00
|
|
|
unless invalid_keys == []
|
|
|
|
raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
|
|
|
|
end
|
2009-01-08 11:43:07 -05:00
|
|
|
assert_valid_strategy(options[:default_strategy]) if options[:default_strategy]
|
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2009-01-08 11:43:07 -05:00
|
|
|
def assert_valid_strategy(strategy)
|
|
|
|
unless Factory::Proxy.const_defined? variable_name_to_class_name(strategy)
|
|
|
|
raise ArgumentError, "Unknown strategy: #{strategy}"
|
|
|
|
end
|
2008-12-11 15:54:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Based on ActiveSupport's underscore inflector
|
|
|
|
def class_name_to_variable_name(name)
|
|
|
|
name.to_s.gsub(/::/, '/').
|
|
|
|
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
|
|
|
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
|
|
|
tr("-", "_").
|
|
|
|
downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
# Based on ActiveSupport's camelize inflector
|
|
|
|
def variable_name_to_class_name(name)
|
|
|
|
name.to_s.
|
|
|
|
gsub(/\/(.?)/) { "::#{$1.upcase}" }.
|
|
|
|
gsub(/(?:^|_)(.)/) { $1.upcase }
|
|
|
|
end
|
|
|
|
|
|
|
|
# From ActiveSupport
|
|
|
|
def symbolize_keys(hash)
|
|
|
|
hash.inject({}) do |options, (key, value)|
|
|
|
|
options[(key.to_sym rescue key) || key] = value
|
|
|
|
options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-28 18:20:25 -04:00
|
|
|
end
|