Class: FactoryGirl::Factory
- Inherits:
-
Object
- Object
- FactoryGirl::Factory
- Defined in:
- lib/factory_girl/factory.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
:nodoc:.
-
#traits ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #add_callback(name, &block) ⇒ Object
- #add_child(factory) ⇒ Object
- #allow_overrides ⇒ Object
- #allow_overrides? ⇒ Boolean
- #apply_attributes(attributes_to_apply) ⇒ Object
-
#apply_traits(traits) ⇒ Object
:nodoc:.
- #associations ⇒ Object
- #attributes ⇒ Object
-
#build_class ⇒ Object
:nodoc:.
-
#class_name ⇒ Object
:nodoc:.
-
#default_strategy ⇒ Object
:nodoc:.
- #define_attribute(attribute) ⇒ Object
- #define_trait(trait) ⇒ 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
- #trait_by_name(name) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Factory
:nodoc:
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/factory_girl/factory.rb', line 35 def initialize(name, = {}) #:nodoc: () @name = factory_name_for(name) @parent = [:parent] @options = @traits = [] @children = [] @attribute_list = AttributeList.new @inherited_attribute_list = AttributeList.new end |
Instance Attribute Details
#name ⇒ Object (readonly)
:nodoc:
15 16 17 |
# File 'lib/factory_girl/factory.rb', line 15 def name @name end |
#traits ⇒ Object (readonly)
:nodoc:
16 17 18 |
# File 'lib/factory_girl/factory.rb', line 16 def traits @traits end |
Instance Method Details
#add_callback(name, &block) ⇒ Object
92 93 94 |
# File 'lib/factory_girl/factory.rb', line 92 def add_callback(name, &block) @attribute_list.add_callback(name, &block) end |
#add_child(factory) ⇒ Object
66 67 68 |
# File 'lib/factory_girl/factory.rb', line 66 def add_child(factory) @children << factory unless @children.include?(factory) end |
#allow_overrides ⇒ Object
46 47 48 49 50 |
# File 'lib/factory_girl/factory.rb', line 46 def allow_overrides @attribute_list.overridable @inherited_attribute_list.overridable self end |
#allow_overrides? ⇒ Boolean
52 53 54 |
# File 'lib/factory_girl/factory.rb', line 52 def allow_overrides? @attribute_list.overridable? end |
#apply_attributes(attributes_to_apply) ⇒ Object
76 77 78 |
# File 'lib/factory_girl/factory.rb', line 76 def apply_attributes(attributes_to_apply) @attribute_list.apply_attributes(attributes_to_apply) end |
#apply_traits(traits) ⇒ Object
:nodoc:
70 71 72 73 74 |
# File 'lib/factory_girl/factory.rb', line 70 def apply_traits(traits) #:nodoc: traits.reverse.map { |name| trait_by_name(name) }.each do |trait| apply_attributes(trait.attributes) end end |
#associations ⇒ Object
123 124 125 |
# File 'lib/factory_girl/factory.rb', line 123 def associations attributes.select {|attribute| attribute.association? } end |
#attributes ⇒ Object
96 97 98 99 100 101 |
# File 'lib/factory_girl/factory.rb', line 96 def attributes AttributeList.new.tap do |list| list.apply_attributes(@attribute_list) list.apply_attributes(@inherited_attribute_list) end.to_a 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
80 81 82 83 84 85 86 |
# File 'lib/factory_girl/factory.rb', line 80 def define_attribute(attribute) if attribute.respond_to?(:factory) && attribute.factory == self.name raise AssociationDefinitionError, "Self-referencing association '#{attribute.name}' in factory '#{self.name}'" end @attribute_list.define_attribute(attribute).tap { update_children } end |
#define_trait(trait) ⇒ Object
88 89 90 |
# File 'lib/factory_girl/factory.rb', line 88 def define_trait(trait) @traits << trait 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
119 120 121 |
# File 'lib/factory_girl/factory.rb', line 119 def human_names names.map {|name| name.to_s.gsub('_', ' ') } end |
#inherit_from(parent) ⇒ Object
:nodoc:
56 57 58 59 60 61 62 63 64 |
# File 'lib/factory_girl/factory.rb', line 56 def inherit_from(parent) #:nodoc: @options[:class] ||= parent.class_name @options[:default_strategy] ||= parent.default_strategy allow_overrides if parent.allow_overrides? parent.add_child(self) @inherited_attribute_list.apply_attributes(parent.attributes) 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
162 163 164 |
# File 'lib/factory_girl/factory.rb', line 162 def names [name] + (@options[:aliases] || []) end |
#run(proxy_class, overrides) ⇒ Object
:nodoc:
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/factory_girl/factory.rb', line 103 def run(proxy_class, overrides) #:nodoc: proxy = proxy_class.new(build_class) overrides = symbolize_keys(overrides) attributes.each do |attribute| factory_overrides = overrides.select { |attr, val| attribute.aliases_for?(attr) } if factory_overrides.empty? attribute.add_to(proxy) else factory_overrides.each { |attr, val| proxy.set(attr, val, attribute.ignored); overrides.delete(attr) } end end overrides.each { |attr, val| proxy.set(attr, val) } proxy.result(@to_create_block) end |
#to_create(&block) ⇒ Object
166 167 168 |
# File 'lib/factory_girl/factory.rb', line 166 def to_create(&block) @to_create_block = block end |
#trait_by_name(name) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/factory_girl/factory.rb', line 127 def trait_by_name(name) if existing_attribute = trait_for(name) existing_attribute elsif @parent FactoryGirl.factory_by_name(@parent).trait_by_name(name) else FactoryGirl.trait_by_name(name) end end |