Class: FactoryGirl::Factory
- Inherits:
-
Object
- Object
- FactoryGirl::Factory
- Defined in:
- lib/factory_girl/factory.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
:nodoc:.
-
#name ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #add_callback(name, &block) ⇒ Object
- #associations ⇒ Object
-
#build_class ⇒ Object
:nodoc:.
-
#class_name ⇒ Object
:nodoc:.
-
#default_strategy ⇒ Object
:nodoc:.
- #define_attribute(attribute) ⇒ Object
- #factory_name ⇒ Object
- #human_names ⇒ Object
-
#inherit_from(parent) ⇒ Object
:nodoc:.
-
#initialize(name, options = {}) ⇒ Factory
constructor
:nodoc:.
-
#names ⇒ Object
Names for this factory, including aliases.
-
#run(proxy_class, overrides) ⇒ Object
:nodoc:.
- #to_create(&block) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Factory
:nodoc:
35 36 37 38 39 40 |
# File 'lib/factory_girl/factory.rb', line 35 def initialize(name, = {}) #:nodoc: () @name = factory_name_for(name) @options = @attributes = [] end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
:nodoc:
16 17 18 |
# File 'lib/factory_girl/factory.rb', line 16 def attributes @attributes end |
#name ⇒ Object (readonly)
:nodoc:
15 16 17 |
# File 'lib/factory_girl/factory.rb', line 15 def name @name end |
Instance Method Details
#add_callback(name, &block) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/factory_girl/factory.rb', line 69 def add_callback(name, &block) 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 |
#associations ⇒ Object
93 94 95 |
# File 'lib/factory_girl/factory.rb', line 93 def associations attributes.select {|attribute| attribute.association? } end |
#build_class ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/factory_girl/factory.rb', line 27 def build_class #:nodoc: @build_class ||= class_for(class_name) end |
#class_name ⇒ Object
:nodoc:
23 24 25 |
# File 'lib/factory_girl/factory.rb', line 23 def class_name #:nodoc: @options[:class] || name end |
#default_strategy ⇒ Object
:nodoc:
31 32 33 |
# File 'lib/factory_girl/factory.rb', line 31 def default_strategy #:nodoc: @options[:default_strategy] || :create end |
#define_attribute(attribute) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/factory_girl/factory.rb', line 57 def define_attribute(attribute) name = attribute.name # TODO: move these checks into Attribute if attribute_defined?(name) raise AttributeDefinitionError, "Attribute already defined: #{name}" end if attribute.respond_to?(:factory) && attribute.factory == self.name raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.name}'" end @attributes << attribute end |
#factory_name ⇒ Object
18 19 20 21 |
# File 'lib/factory_girl/factory.rb', line 18 def factory_name puts "WARNING: factory.factory_name is deprecated. Use factory.name instead." name end |
#human_names ⇒ Object
89 90 91 |
# File 'lib/factory_girl/factory.rb', line 89 def human_names names.map {|name| name.to_s.gsub('_', ' ') } end |
#inherit_from(parent) ⇒ Object
:nodoc:
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/factory_girl/factory.rb', line 42 def inherit_from(parent) #:nodoc: @options[:class] ||= parent.class_name @options[:default_strategy] ||= parent.default_strategy new_attributes = [] parent.attributes.each do |attribute| unless attribute_defined?(attribute.name) new_attributes << attribute.clone end end @attributes += new_attributes @attributes.sort! 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
122 123 124 |
# File 'lib/factory_girl/factory.rb', line 122 def names [name] + (@options[:aliases] || []) end |
#run(proxy_class, overrides) ⇒ Object
:nodoc:
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/factory_girl/factory.rb', line 76 def run(proxy_class, overrides) #:nodoc: proxy = proxy_class.new(build_class) overrides = symbolize_keys(overrides) overrides.each {|attr, val| proxy.set(attr, val) } passed_keys = overrides.keys.collect {|k| FactoryGirl.aliases_for(k) }.flatten @attributes.each do |attribute| unless passed_keys.include?(attribute.name) attribute.add_to(proxy) end end proxy.result(@to_create_block) end |
#to_create(&block) ⇒ Object
126 127 128 |
# File 'lib/factory_girl/factory.rb', line 126 def to_create(&block) @to_create_block = block end |