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.is_a?(Symbol) ? name : name.to_s.underscore.to_sym
  @parent           = options[:parent]
  @aliases          = options[:aliases] || []
  @class_name       = options[:class]
  @default_strategy = options[:default_strategy]
  @definition       = Definition.new(@name)
  @compiled         = false

  inherit_traits(options[:traits] || [])
end

Instance Attribute Details

#definitionObject (readonly)

:nodoc:



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

def definition
  @definition
end

#nameObject (readonly)

:nodoc:



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

def name
  @name
end

Instance Method Details

#associationsObject



57
58
59
# File 'lib/factory_girl/factory.rb', line 57

def associations
  attributes.associations
end

#build_classObject

:nodoc:



29
30
31
32
33
34
35
# File 'lib/factory_girl/factory.rb', line 29

def build_class #:nodoc:
  @build_class ||= if class_name.is_a? Class
    class_name
  else
    class_name.to_s.camelize.constantize
  end
end

#compileObject



90
91
92
93
94
95
96
97
# File 'lib/factory_girl/factory.rb', line 90

def compile
  unless @compiled
    parent.defined_traits.each {|trait| define_trait(trait) }
    parent.compile
    @definition.compile
    @compiled = true
  end
end

#default_strategyObject

:nodoc:



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

def default_strategy #:nodoc:
  @default_strategy || parent.default_strategy
end

#factory_nameObject



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

def factory_name
  $stderr.puts "DEPRECATION WARNING: factory.factory_name is deprecated; use factory.name instead."
  name
end

#human_namesObject



53
54
55
# File 'lib/factory_girl/factory.rb', line 53

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


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

def names
  [name] + @aliases
end

#run(proxy_class, overrides, &block) ⇒ Object

:nodoc:



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

def run(proxy_class, overrides, &block) #:nodoc:
  block ||= lambda {|result| result }
  compile

  proxy = proxy_class.new

  evaluator = evaluator_class.new(proxy, overrides.symbolize_keys)
  attribute_assigner = AttributeAssigner.new(build_class, evaluator)

  proxy.result(attribute_assigner, to_create).tap(&block)
end

#with_traits(traits) ⇒ Object



99
100
101
102
103
# File 'lib/factory_girl/factory.rb', line 99

def with_traits(traits)
  self.clone.tap do |factory_with_traits|
    factory_with_traits.inherit_traits traits
  end
end