Class: FactoryGirl::Factory
- Inherits:
-
Object
- Object
- FactoryGirl::Factory
- Defined in:
- lib/factory_girl/factory.rb
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
:nodoc:.
-
#name ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #associations ⇒ Object
-
#build_class ⇒ Object
:nodoc:.
- #compile ⇒ Object
- #human_names ⇒ Object
-
#initialize(name, options = {}) ⇒ Factory
constructor
:nodoc:.
-
#names ⇒ Object
Names for this factory, including aliases.
-
#run(strategy_class, overrides, &block) ⇒ Object
:nodoc:.
- #with_traits(traits) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Factory
:nodoc:
8 9 10 11 12 13 14 15 16 |
# File 'lib/factory_girl/factory.rb', line 8 def initialize(name, = {}) #:nodoc: () @name = name.is_a?(Symbol) ? name : name.to_s.underscore.to_sym @parent = [:parent] @aliases = [:aliases] || [] @class_name = [:class] @definition = Definition.new(@name, [:traits] || []) @compiled = false end |
Instance Attribute Details
#definition ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/factory_girl/factory.rb', line 6 def definition @definition end |
#name ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/factory_girl/factory.rb', line 6 def name @name end |
Instance Method Details
#associations ⇒ Object
48 49 50 |
# File 'lib/factory_girl/factory.rb', line 48 def associations evaluator_class.attribute_list.associations end |
#build_class ⇒ Object
:nodoc:
21 22 23 24 25 26 27 |
# File 'lib/factory_girl/factory.rb', line 21 def build_class #:nodoc: @build_class ||= if class_name.is_a? Class class_name else class_name.to_s.camelize.constantize end end |
#compile ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/factory_girl/factory.rb', line 81 def compile unless @compiled parent.compile parent.defined_traits.each {|trait| define_trait(trait) } @definition.compile @compiled = true end end |
#human_names ⇒ Object
44 45 46 |
# File 'lib/factory_girl/factory.rb', line 44 def human_names names.map {|name| name.to_s.humanize.downcase } end |
#names ⇒ Object
Names for this factory, including aliases.
Example:
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:
factory :user, aliases: [:author] do
# ...
end
factory :post do
end
FactoryGirl.create(:post)..class
# => User
77 78 79 |
# File 'lib/factory_girl/factory.rb', line 77 def names [name] + @aliases end |
#run(strategy_class, overrides, &block) ⇒ Object
:nodoc:
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/factory_girl/factory.rb', line 29 def run(strategy_class, overrides, &block) #:nodoc: block ||= lambda {|result| result } compile strategy = strategy_class.new evaluator = evaluator_class.new(strategy, overrides.symbolize_keys) attribute_assigner = AttributeAssigner.new(evaluator, &instance_builder) evaluation = Evaluation.new(attribute_assigner, to_create) evaluation.add_observer(CallbackRunner.new(callbacks, evaluator)) strategy.result(evaluation).tap(&block) end |
#with_traits(traits) ⇒ Object
90 91 92 93 94 |
# File 'lib/factory_girl/factory.rb', line 90 def with_traits(traits) self.clone.tap do |factory_with_traits| factory_with_traits.inherit_traits traits end end |