Class: FactoryGirl::Factory
- Inherits:
-
Object
- Object
- FactoryGirl::Factory
- Defined in:
- lib/factory_girl/factory.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #add_callback(name, &block) ⇒ Object
- #allow_overrides ⇒ Object
- #allow_overrides? ⇒ Boolean
- #associations ⇒ Object
-
#build_class ⇒ Object
:nodoc:.
- #declare_attribute(declaration) ⇒ Object
-
#default_strategy ⇒ Object
:nodoc:.
- #define_trait(trait) ⇒ Object
- #ensure_compiled ⇒ Object
- #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:.
- #to_create(&block) ⇒ Object
- #trait_by_name(name) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Factory
:nodoc:
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/factory_girl/factory.rb', line 8 def initialize(name, = {}) #:nodoc: () @name = name.to_s.underscore.to_sym @parent = [:parent] @aliases = [:aliases] || [] @traits = [:traits] || [] @class_name = [:class] @default_strategy = [:default_strategy] @defined_traits = [] @attribute_list = AttributeList.new @compiled = false end |
Instance Attribute Details
#name ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/factory_girl/factory.rb', line 6 def name @name end |
Instance Method Details
#add_callback(name, &block) ⇒ Object
48 49 50 |
# File 'lib/factory_girl/factory.rb', line 48 def add_callback(name, &block) @attribute_list.add_callback(Callback.new(name, block)) end |
#allow_overrides ⇒ Object
34 35 36 37 38 |
# File 'lib/factory_girl/factory.rb', line 34 def allow_overrides @compiled = false @attribute_list.overridable self end |
#allow_overrides? ⇒ Boolean
40 41 42 |
# File 'lib/factory_girl/factory.rb', line 40 def allow_overrides? @attribute_list.overridable? end |
#associations ⇒ Object
84 85 86 |
# File 'lib/factory_girl/factory.rb', line 84 def associations attributes.select {|attribute| attribute.association? } end |
#build_class ⇒ Object
:nodoc:
26 27 28 |
# File 'lib/factory_girl/factory.rb', line 26 def build_class #:nodoc: @build_class ||= class_name.to_s.camelize.constantize end |
#declare_attribute(declaration) ⇒ Object
135 136 137 |
# File 'lib/factory_girl/factory.rb', line 135 def declare_attribute(declaration) @attribute_list.declare_attribute(declaration) end |
#default_strategy ⇒ Object
:nodoc:
30 31 32 |
# File 'lib/factory_girl/factory.rb', line 30 def default_strategy #:nodoc: @default_strategy || (parent && parent.default_strategy) || :create end |
#define_trait(trait) ⇒ Object
44 45 46 |
# File 'lib/factory_girl/factory.rb', line 44 def define_trait(trait) @defined_traits << trait end |
#ensure_compiled ⇒ Object
131 132 133 |
# File 'lib/factory_girl/factory.rb', line 131 def ensure_compiled compile unless @compiled end |
#factory_name ⇒ Object
21 22 23 24 |
# File 'lib/factory_girl/factory.rb', line 21 def factory_name $stderr.puts "DEPRECATION WARNING: factory.factory_name is deprecated; use factory.name instead." name end |
#human_names ⇒ Object
80 81 82 |
# File 'lib/factory_girl/factory.rb', line 80 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
123 124 125 |
# File 'lib/factory_girl/factory.rb', line 123 def names [name] + @aliases end |
#run(proxy_class, overrides, &block) ⇒ Object
:nodoc:
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/factory_girl/factory.rb', line 52 def run(proxy_class, overrides, &block) #:nodoc: ensure_compiled proxy = proxy_class.new(build_class) callbacks.each { |callback| proxy.add_callback(callback) } overrides = overrides.symbolize_keys 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 do |attr, val| if attribute.ignored proxy.set_ignored(attr, val) else proxy.set(attr, val) end overrides.delete(attr) end end end overrides.each { |attr, val| proxy.set(attr, val) } result = proxy.result(@to_create_block) block ? block.call(result) : result end |
#to_create(&block) ⇒ Object
127 128 129 |
# File 'lib/factory_girl/factory.rb', line 127 def to_create(&block) @to_create_block = block end |
#trait_by_name(name) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/factory_girl/factory.rb', line 88 def trait_by_name(name) if existing_attribute = trait_for(name) existing_attribute elsif parent parent.trait_by_name(name) else FactoryGirl.trait_by_name(name) end end |