1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/lib/factory_girl/factory.rb

163 lines
3.9 KiB
Ruby
Raw Normal View History

2012-05-05 01:14:21 -04:00
require 'active_support/core_ext/hash/keys'
require 'active_support/inflector'
module FactoryGirl
2012-05-05 02:31:31 -04:00
# @api private
class Factory
2012-05-05 02:31:31 -04:00
attr_reader :name, :definition
2011-08-12 10:35:41 -04:00
2012-05-05 02:31:31 -04:00
def initialize(name, options = {})
assert_valid_options(options)
@name = name.is_a?(Symbol) ? name : name.to_s.underscore.to_sym
2011-09-23 16:33:39 -04:00
@parent = options[:parent]
@aliases = options[:aliases] || []
@class_name = options[:class]
@definition = Definition.new(@name, options[:traits] || [])
2012-01-08 00:57:40 -05:00
@compiled = false
end
delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
:defined_traits, :inherit_traits, :append_traits, to: :@definition
2011-10-14 22:34:51 -04:00
2012-05-05 02:31:31 -04:00
def build_class
@build_class ||= if class_name.is_a? Class
class_name
else
class_name.to_s.camelize.constantize
end
end
2012-05-05 02:31:31 -04:00
def run(build_strategy, overrides, &block)
2012-04-20 16:59:39 -04:00
block ||= ->(result) { result }
compile
2011-11-20 21:42:59 -05:00
strategy = StrategyCalculator.new(build_strategy).strategy.new
2011-12-16 11:15:13 -05:00
evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor)
2011-12-16 11:15:13 -05:00
evaluation = Evaluation.new(attribute_assigner, compiled_to_create)
evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))
strategy.result(evaluation).tap(&block)
end
2009-09-15 15:47:47 -04:00
def human_names
names.map {|name| name.to_s.humanize.downcase }
2009-09-15 15:47:47 -04:00
end
def associations
evaluator_class.attribute_list.associations
end
# Names for this factory, including aliases.
#
# Example:
#
2012-03-09 17:20:38 -05:00
# factory :user, aliases: [:author] do
# # ...
# end
#
# FactoryGirl.create(:author).class
# # => User
#
# Because an attribute defined without a value or block will build an
# association with the same name, this allows associations to be defined
# without factories, such as:
#
2012-03-09 17:20:38 -05:00
# factory :user, aliases: [:author] do
# # ...
# end
#
# factory :post do
# author
# end
#
# FactoryGirl.create(:post).author.class
# # => User
def names
2011-09-23 16:33:39 -04:00
[name] + @aliases
end
def compile
2012-01-08 00:57:40 -05:00
unless @compiled
parent.compile
parent.defined_traits.each {|trait| define_trait(trait) }
2012-01-08 00:57:40 -05:00
@definition.compile
build_hierarchy
2012-01-08 00:57:40 -05:00
@compiled = true
end
end
def with_traits(traits)
self.clone.tap do |factory_with_traits|
factory_with_traits.append_traits traits
end
end
protected
2012-05-05 02:31:31 -04:00
def class_name
2011-10-28 21:23:06 -04:00
@class_name || parent.class_name || name
end
2012-01-03 09:31:40 -05:00
def evaluator_class
@evaluator_class ||= EvaluatorClassDefiner.new(attributes, parent.evaluator_class).evaluator_class
2012-01-03 09:31:40 -05:00
end
2011-10-07 16:43:36 -04:00
def attributes
compile
AttributeList.new(@name).tap do |list|
list.apply_attributes definition.attributes
2011-10-07 16:43:36 -04:00
end
end
def hierarchy_class
@hierarchy_class ||= Class.new(parent.hierarchy_class)
end
def hierarchy_instance
@hierarchy_instance ||= hierarchy_class.new
end
def build_hierarchy
2012-09-14 08:22:28 -04:00
hierarchy_class.build_from_definition definition
end
2011-10-07 16:06:28 -04:00
def callbacks
hierarchy_instance.callbacks
2011-10-07 16:06:28 -04:00
end
def compiled_to_create
hierarchy_instance.to_create
end
def compiled_constructor
hierarchy_instance.constructor
end
private
def assert_valid_options(options)
2012-03-09 15:24:40 -05:00
options.assert_valid_keys(:class, :parent, :aliases, :traits)
end
2010-06-07 15:51:18 -04:00
def parent
2011-10-28 21:23:06 -04:00
if @parent
FactoryGirl.factory_by_name(@parent)
else
NullFactory.new
end
end
def initialize_copy(source)
super
@definition = @definition.clone
@evaluator_class = nil
@hierarchy_class = nil
@hierarchy_instance = nil
@compiled = false
end
end
end