Class: FactoryGirl::Factory Private

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

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Factory.



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

def initialize(name, options = {})
  assert_valid_options(options)
  @name             = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym
  @parent           = options[:parent]
  @aliases          = options[:aliases] || []
  @class_name       = options[:class]
  @definition       = Definition.new(@name, options[:traits] || [])
  @compiled         = false
end

Instance Attribute Details

#definitionObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def definition
  @definition
end

#nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def name
  @name
end

Instance Method Details

#associationsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
# File 'lib/factory_girl/factory.rb', line 50

def associations
  evaluator_class.attribute_list.associations
end

#build_classObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

#compileObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
86
87
88
89
90
91
# File 'lib/factory_girl/factory.rb', line 83

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

#human_namesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
# File 'lib/factory_girl/factory.rb', line 46

def human_names
  names.map { |name| name.to_s.humanize.downcase }
end

#namesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


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

def names
  [name] + @aliases
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/factory_girl/factory.rb', line 30

def run(build_strategy, overrides, &block)
  block ||= ->(result) { result }
  compile

  strategy = StrategyCalculator.new(build_strategy).strategy.new

  evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
  attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor)

  evaluation =
    Evaluation.new(evaluator, attribute_assigner, compiled_to_create)
  evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))

  strategy.result(evaluation).tap(&block)
end

#with_traits(traits) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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