Class: Factory
- Inherits:
-
Object
- Object
- Factory
- Defined in:
- lib/factory_girl/factory.rb,
lib/factory_girl/sequence.rb,
lib/factory_girl/attribute_proxy.rb
Defined Under Namespace
Classes: AttributeProxy, Sequence
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.attributes_for(name, attrs = {}) ⇒ Object
Generates and returns a Hash of attributes from this factory.
-
.build(name, attrs = {}) ⇒ Object
Generates and returns an instance from this factory.
-
.create(name, attrs = {}) ⇒ Object
Generates, saves, and returns an instance from this factory.
-
.define(name, options = {}) {|instance| ... } ⇒ Object
Defines a new factory that can be used by the build strategies (create and build) to build new objects.
-
.next(sequence) ⇒ Object
Generates and returns the next value in a sequence.
-
.sequence(name, &block) ⇒ Object
Defines a new sequence that can be used to generate unique values in a specific format.
Instance Method Summary collapse
-
#add_attribute(name, value = nil, &block) ⇒ Object
Adds an attribute that should be assigned on generated instances for this factory.
-
#attributes_for(attrs = {}) ⇒ Object
:nodoc:.
-
#build(attrs = {}) ⇒ Object
:nodoc:.
-
#build_class ⇒ Object
:nodoc:.
-
#create(attrs = {}) ⇒ Object
:nodoc:.
-
#initialize(name, options = {}) ⇒ Factory
constructor
:nodoc:.
-
#method_missing(name, *args, &block) ⇒ Object
Calls add_attribute using the missing method name as the name of the attribute, so that:.
Constructor Details
#initialize(name, options = {}) ⇒ Factory
:nodoc:
67 68 69 70 71 72 73 74 75 |
# File 'lib/factory_girl/factory.rb', line 67 def initialize (name, = {}) #:nodoc: .assert_valid_keys(:class) @name = name @options = @static_attributes = {} @lazy_attribute_blocks = {} @lazy_attribute_names = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
121 122 123 |
# File 'lib/factory_girl/factory.rb', line 121 def method_missing (name, *args, &block) add_attribute(name, *args, &block) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/factory_girl/factory.rb', line 7 def name @name end |
Class Method Details
.attributes_for(name, attrs = {}) ⇒ Object
Generates and returns a Hash of attributes from this factory. Attributes can be individually overridden by passing in a Hash of attribute => value pairs.
Arguments:
attrs: (Hash)
Attributes to overwrite for this set.
Returns:
A set of attributes that can be used to build an instance of the class
this factory generates. (Hash)
152 153 154 |
# File 'lib/factory_girl/factory.rb', line 152 def attributes_for (name, attrs = {}) factory_by_name(name).attributes_for(attrs) end |
.build(name, attrs = {}) ⇒ Object
Generates and returns an instance from this factory. Attributes can be individually overridden by passing in a Hash of attribute => value pairs.
Arguments:
attrs: (Hash)
See attributes_for
Returns:
An instance of the class this factory generates, with generated
attributes assigned.
166 167 168 |
# File 'lib/factory_girl/factory.rb', line 166 def build (name, attrs = {}) factory_by_name(name).build(attrs) end |
.create(name, attrs = {}) ⇒ Object
Generates, saves, and returns an instance from this factory. Attributes can be individually overridden by passing in a Hash of attribute => value pairs.
If the instance is not valid, an ActiveRecord::Invalid exception will be raised.
Arguments:
attrs: (Hash)
See attributes_for
Returns:
A saved instance of the class this factory generates, with generated
attributes assigned.
184 185 186 |
# File 'lib/factory_girl/factory.rb', line 184 def create (name, attrs = {}) factory_by_name(name).create(attrs) end |
.define(name, options = {}) {|instance| ... } ⇒ Object
Defines a new factory that can be used by the build strategies (create and build) to build new objects.
Arguments:
name: (Symbol)
A unique name used to identify this factory.
options: (Hash)
class: the class that will be used when generating instances for this
factory. If not specified, the class will be guessed from the
factory name.
Yields:
The newly created factory (Factory)
22 23 24 25 26 |
# File 'lib/factory_girl/factory.rb', line 22 def self.define (name, = {}) instance = Factory.new(name, ) yield(instance) self.factories[name] = instance end |
.next(sequence) ⇒ Object
Generates and returns the next value in a sequence.
Arguments:
name: (Symbol)
The name of the sequence that a value should be generated for.
Returns:
The next value in the sequence. (Object)
55 56 57 58 59 60 61 |
# File 'lib/factory_girl/factory.rb', line 55 def self.next (sequence) unless self.sequences.key?(sequence) raise "No such sequence: #{sequence}" end self.sequences[sequence].next end |
.sequence(name, &block) ⇒ Object
Defines a new sequence that can be used to generate unique values in a specific format.
Arguments:
name: (Symbol)
A unique name for this sequence. This name will be referenced when
calling next to generate new values from this sequence.
block: (Proc)
The code to generate each value in the sequence. This block will be
called with a unique number each time a value in the sequence is to be
generated. The block should return the generated value for the
sequence.
Example:
Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
43 44 45 |
# File 'lib/factory_girl/factory.rb', line 43 def self.sequence (name, &block) self.sequences[name] = Sequence.new(&block) end |
Instance Method Details
#add_attribute(name, value = nil, &block) ⇒ Object
Adds an attribute that should be assigned on generated instances for this factory.
This method should be called with either a value or block, but not both. If called with a block, the attribute will be generated “lazily,” whenever an instance is generated. Lazy attribute blocks will not be called if that attribute is overriden for a specific instance.
When defining lazy attributes, an instance of Factory::AttributeProxy will be yielded, allowing associations to be built using the correct build strategy.
Arguments:
name: (Symbol)
The name of this attribute. This will be assigned using :"#{name}=" for
generated instances.
value: (Object)
If no block is given, this value will be used for this attribute.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/factory_girl/factory.rb', line 95 def add_attribute (name, value = nil, &block) if block_given? unless value.nil? raise ArgumentError, "Both value and block given" end @lazy_attribute_blocks[name] = block @lazy_attribute_names << name else @static_attributes[name] = value end end |
#attributes_for(attrs = {}) ⇒ Object
:nodoc:
125 126 127 |
# File 'lib/factory_girl/factory.rb', line 125 def attributes_for (attrs = {}) #:nodoc: build_attributes_hash(attrs, :attributes_for) end |
#build(attrs = {}) ⇒ Object
:nodoc:
129 130 131 |
# File 'lib/factory_girl/factory.rb', line 129 def build (attrs = {}) #:nodoc: build_instance(attrs, :build) end |
#build_class ⇒ Object
:nodoc:
63 64 65 |
# File 'lib/factory_girl/factory.rb', line 63 def build_class #:nodoc: @build_class ||= @options[:class] || name.to_s.classify.constantize end |
#create(attrs = {}) ⇒ Object
:nodoc:
133 134 135 136 137 |
# File 'lib/factory_girl/factory.rb', line 133 def create (attrs = {}) #:nodoc: instance = build_instance(attrs, :create) instance.save! instance end |