Class: Factory

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options = {}) #:nodoc:
  options.assert_valid_keys(:class)
  @factory_name = name
  @options      = 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

Calls add_attribute using the missing method name as the name of the attribute, so that:

Factory.define :user do |f|
  f.name 'Billy Idol'
end

and:

Factory.define :user do |f|
  f.add_attribute :name, 'Billy Idol'
end

are equivilent.



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

#factory_nameObject (readonly)

Returns the value of attribute factory_name.



7
8
9
# File 'lib/factory_girl/factory.rb', line 7

def factory_name
  @factory_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)


158
159
160
# File 'lib/factory_girl/factory.rb', line 158

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.


172
173
174
# File 'lib/factory_girl/factory.rb', line 172

def build (name, attrs = {})
  factory_by_name(name).build(attrs)
end

.construct(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, does not raise an ActiveRecord::Invalid exception (differently from create())

Arguments:

attrs: (Hash)
  See attributes_for

Returns:

A saved instance of the class this factory generates, with generated
attributes assigned.


207
208
209
# File 'lib/factory_girl/factory.rb', line 207

def construct (name, attrs = {})
  factory_by_name(name).construct(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.


190
191
192
# File 'lib/factory_girl/factory.rb', line 190

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)

Yields:

  • (instance)


22
23
24
25
26
# File 'lib/factory_girl/factory.rb', line 22

def self.define (name, options = {})
  instance = Factory.new(name, options)
  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_classObject

:nodoc:



63
64
65
# File 'lib/factory_girl/factory.rb', line 63

def build_class #:nodoc:
  @build_class ||= @options[:class] || factory_name.to_s.classify.constantize
end

#construct(attrs = {}) ⇒ Object

:nodoc:



139
140
141
142
143
# File 'lib/factory_girl/factory.rb', line 139

def construct (attrs = {}) #:nodoc:
  instance = build_instance(attrs, :create)
  instance.save
  instance
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