Class: Factory::AttributeProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_girl/attribute_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory, attr, strategy, values) ⇒ AttributeProxy

:nodoc:



7
8
9
10
11
12
# File 'lib/factory_girl/attribute_proxy.rb', line 7

def initialize (factory, attr, strategy, values) #:nodoc:
  @factory        = factory
  @attribute_name = attr
  @strategy       = strategy
  @current_values = values
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Undefined methods are delegated to value_for, which means that:

Factory.define :user do |f|
  f.first_name 'Ben'
  f.last_name {|a| a.value_for(:first_name) }
end

and:

Factory.define :user do |f|
  f.first_name 'Ben'
  f.last_name {|a| a.first_name }
end

are equivilent.



86
87
88
# File 'lib/factory_girl/attribute_proxy.rb', line 86

def method_missing (name, *args, &block)
  current_values[name]
end

Instance Attribute Details

#attribute_nameObject

:nodoc:



5
6
7
# File 'lib/factory_girl/attribute_proxy.rb', line 5

def attribute_name
  @attribute_name
end

#current_valuesObject

:nodoc:



5
6
7
# File 'lib/factory_girl/attribute_proxy.rb', line 5

def current_values
  @current_values
end

#factoryObject

:nodoc:



5
6
7
# File 'lib/factory_girl/attribute_proxy.rb', line 5

def factory
  @factory
end

#strategyObject

:nodoc:



5
6
7
# File 'lib/factory_girl/attribute_proxy.rb', line 5

def strategy
  @strategy
end

Instance Method Details

#association(name, attributes = {}) ⇒ Object

Generates an association using the current build strategy.

Arguments:

name: (Symbol)
  The name of the factory that should be used to generate this
  association.
attributes: (Hash)
  A hash of attributes that should be overridden for this association.

Returns:

The generated association for the current build strategy. Note that
assocaitions are not generated for the attributes_for strategy. Returns
nil in this case.

Example:

Factory.define :user do |f|
  # ...
end

Factory.define :post do |f|
  # ...
  f.author {|a| a.association :user, :name => 'Joe' }
end

# Builds (but doesn't save) a Post and a User
Factory.build(:post)

# Builds and saves a User, builds a Post, assigns the User to the 
# author association, and saves the User.
Factory.create(:post)


46
47
48
49
50
51
52
# File 'lib/factory_girl/attribute_proxy.rb', line 46

def association (name, attributes = {})
  if strategy == :attributes_for
    nil
  else
    Factory.send(strategy, name, attributes)
  end
end

#value_for(attribute) ⇒ Object

Returns the value for specified attribute. A value will only be available if it was overridden when calling the factory, or if a value is added earlier in the definition of the factory.

Arguments:

attribute: (Symbol)
  The attribute whose value should be returned.

Returns:

The value of the requested attribute. (Object)


64
65
66
67
68
69
# File 'lib/factory_girl/attribute_proxy.rb', line 64

def value_for (attribute)
  unless current_values.key?(attribute)
    raise ArgumentError, "No such attribute: #{attribute.inspect}"
  end
  current_values[attribute]
end