Class: FactoryGirl::Factory

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Factory

:nodoc:



35
36
37
38
39
40
# File 'lib/factory_girl/factory.rb', line 35

def initialize(name, options = {}) #:nodoc:
  assert_valid_options(options)
  @name       = factory_name_for(name)
  @options    = options
  @attributes = []
end

Instance Attribute Details

#attributesObject (readonly)

:nodoc:



16
17
18
# File 'lib/factory_girl/factory.rb', line 16

def attributes
  @attributes
end

#nameObject (readonly)

:nodoc:



15
16
17
# File 'lib/factory_girl/factory.rb', line 15

def name
  @name
end

Instance Method Details

#add_callback(name, &block) ⇒ Object



67
68
69
70
71
72
# File 'lib/factory_girl/factory.rb', line 67

def add_callback(name, &block)
  unless [:after_build, :after_create, :after_stub].include?(name.to_sym)
    raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are :after_build, :after_create, and :after_stub"
  end
  @attributes << Attribute::Callback.new(name.to_sym, block)
end

#associationsObject



91
92
93
# File 'lib/factory_girl/factory.rb', line 91

def associations
  attributes.select {|attribute| attribute.association? }
end

#build_classObject

:nodoc:



27
28
29
# File 'lib/factory_girl/factory.rb', line 27

def build_class #:nodoc:
  @build_class ||= class_for(class_name)
end

#class_nameObject

:nodoc:



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

def class_name #:nodoc:
  @options[:class] || name
end

#default_strategyObject

: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



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/factory_girl/factory.rb', line 55

def define_attribute(attribute)
  name = attribute.name
  # TODO: move these checks into Attribute
  if attribute_defined?(name)
    raise AttributeDefinitionError, "Attribute already defined: #{name}"
  end
  if attribute.respond_to?(:factory) && attribute.factory == self.name
    raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.name}'"
  end
  @attributes << attribute
end

#factory_nameObject



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_namesObject



87
88
89
# File 'lib/factory_girl/factory.rb', line 87

def human_names
  names.map {|name| name.to_s.gsub('_', ' ') }
end

#inherit_from(parent) ⇒ Object

:nodoc:



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/factory_girl/factory.rb', line 42

def inherit_from(parent) #:nodoc:
  @options[:class]            ||= parent.class_name
  @options[:default_strategy] ||= parent.default_strategy

  new_attributes = []
  parent.attributes.each do |attribute|
    unless attribute_defined?(attribute.name)
      new_attributes << attribute.clone
    end
  end
  @attributes.unshift *new_attributes
end

#namesObject

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
  author
end

FactoryGirl.create(:post).author.class
# => User


120
121
122
# File 'lib/factory_girl/factory.rb', line 120

def names
  [name] + (@options[:aliases] || [])
end

#run(proxy_class, overrides) ⇒ Object

:nodoc:



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/factory_girl/factory.rb', line 74

def run(proxy_class, overrides) #:nodoc:
  proxy = proxy_class.new(build_class)
  overrides = symbolize_keys(overrides)
  overrides.each {|attr, val| proxy.set(attr, val) }
  passed_keys = overrides.keys.collect {|k| FactoryGirl.aliases_for(k) }.flatten
  @attributes.each do |attribute|
    unless passed_keys.include?(attribute.name)
      attribute.add_to(proxy)
    end
  end
  proxy.result(@to_create_block)
end

#to_create(&block) ⇒ Object



124
125
126
# File 'lib/factory_girl/factory.rb', line 124

def to_create(&block)
  @to_create_block = block
end