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:



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/factory_girl/factory.rb', line 8

def initialize(name, options = {}) #:nodoc:
  assert_valid_options(options)
  @name             = name.to_s.underscore.to_sym
  @parent           = options[:parent]
  @aliases          = options[:aliases] || []
  @traits           = options[:traits]  || []
  @class_name       = options[:class]
  @default_strategy = options[:default_strategy]
  @defined_traits   = []
  @attribute_list   = AttributeList.new
  @compiled         = false
end

Instance Attribute Details

#nameObject (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_overridesObject



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

Returns:

  • (Boolean)


40
41
42
# File 'lib/factory_girl/factory.rb', line 40

def allow_overrides?
  @attribute_list.overridable?
end

#associationsObject



84
85
86
# File 'lib/factory_girl/factory.rb', line 84

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

#build_classObject

: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_strategyObject

: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_compiledObject



131
132
133
# File 'lib/factory_girl/factory.rb', line 131

def ensure_compiled
  compile unless @compiled
end

#factory_nameObject



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_namesObject



80
81
82
# File 'lib/factory_girl/factory.rb', line 80

def human_names
  names.map {|name| name.to_s.humanize.downcase }
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


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