Class: FactoryGirl::Factory
- Inherits:
-
Object
- Object
- FactoryGirl::Factory
- Defined in:
- lib/factory_girl/factory.rb
Defined Under Namespace
Classes: Runner
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
:nodoc:.
-
#name ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #associations ⇒ Object
-
#build_class ⇒ Object
:nodoc:.
- #compile ⇒ Object
-
#default_strategy ⇒ Object
:nodoc:.
- #factory_name ⇒ Object
- #human_names ⇒ Object
-
#initialize(name, options = {}) ⇒ Factory
constructor
:nodoc:.
-
#names ⇒ Object
Names for this factory, including aliases.
-
#run(proxy_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 17 18 |
# 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] @default_strategy = [:default_strategy] @definition = Definition.new(@name) inherit_traits([:traits] || []) 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
54 55 56 |
# File 'lib/factory_girl/factory.rb', line 54 def associations attributes.select {|attribute| attribute.association? } end |
#build_class ⇒ Object
:nodoc:
28 29 30 |
# File 'lib/factory_girl/factory.rb', line 28 def build_class #:nodoc: class_name.to_s.camelize.constantize end |
#compile ⇒ Object
87 88 89 90 91 |
# File 'lib/factory_girl/factory.rb', line 87 def compile parent.defined_traits.each {|trait| define_trait(trait) } parent.compile @definition.compile end |
#default_strategy ⇒ Object
:nodoc:
32 33 34 |
# File 'lib/factory_girl/factory.rb', line 32 def default_strategy #:nodoc: @default_strategy || parent.default_strategy || :create end |
#factory_name ⇒ Object
23 24 25 26 |
# File 'lib/factory_girl/factory.rb', line 23 def factory_name $stderr.puts "DEPRECATION WARNING: factory.factory_name is deprecated; use factory.name instead." name end |
#human_names ⇒ Object
50 51 52 |
# File 'lib/factory_girl/factory.rb', line 50 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
83 84 85 |
# File 'lib/factory_girl/factory.rb', line 83 def names [name] + @aliases end |
#run(proxy_class, overrides, &block) ⇒ Object
:nodoc:
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/factory_girl/factory.rb', line 36 def run(proxy_class, overrides, &block) #:nodoc: = { :attributes => attributes, :callbacks => callbacks, :to_create => to_create, :build_class => build_class, :proxy_class => proxy_class } result = Runner.new().run(overrides) block ? block.call(result) : result end |
#with_traits(traits) ⇒ Object
93 94 95 96 97 |
# File 'lib/factory_girl/factory.rb', line 93 def with_traits(traits) self.clone.tap do |factory_with_traits| factory_with_traits.inherit_traits traits end end |