class FactoryGirl::Factory

@api private

Attributes

definition[R]
name[R]

Public Class Methods

new(name, options = {}) click to toggle source
# File lib/factory_girl/factory.rb, line 9
def initialize(name, options = {})
  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]
  @definition       = Definition.new(@name, options[:traits] || [])
  @compiled         = false
end

Public Instance Methods

associations() click to toggle source
# File lib/factory_girl/factory.rb, line 49
def associations
  evaluator_class.attribute_list.associations
end
build_class() click to toggle source
# 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
compile() click to toggle source
# File lib/factory_girl/factory.rb, line 82
def compile
  unless @compiled
    parent.compile
    parent.defined_traits.each {|trait| define_trait(trait) }
    @definition.compile
    build_hierarchy
    @compiled = true
  end
end
human_names() click to toggle source
# File lib/factory_girl/factory.rb, line 45
def human_names
  names.map {|name| name.to_s.humanize.downcase }
end
names() click to toggle source

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
# File lib/factory_girl/factory.rb, line 78
def names
  [name] + @aliases
end
run(build_strategy, overrides, &block) click to toggle source
# 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(attribute_assigner, compiled_to_create)
  evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))

  strategy.result(evaluation).tap(&block)
end
with_traits(traits) click to toggle source
# File lib/factory_girl/factory.rb, line 92
def with_traits(traits)
  self.clone.tap do |factory_with_traits|
    factory_with_traits.append_traits traits
  end
end

Protected Instance Methods

attributes() click to toggle source
# File lib/factory_girl/factory.rb, line 108
def attributes
  compile
  AttributeList.new(@name).tap do |list|
    list.apply_attributes definition.attributes
  end
end
build_hierarchy() click to toggle source
# File lib/factory_girl/factory.rb, line 123
def build_hierarchy
  hierarchy_class.build_to_create &definition.to_create
  hierarchy_class.build_constructor &definition.constructor
  hierarchy_class.add_callbacks definition.callbacks
end
callbacks() click to toggle source
# File lib/factory_girl/factory.rb, line 129
def callbacks
  hierarchy_instance.callbacks
end
class_name() click to toggle source
# File lib/factory_girl/factory.rb, line 100
def class_name
  @class_name || parent.class_name || name
end
compiled_constructor() click to toggle source
# File lib/factory_girl/factory.rb, line 137
def compiled_constructor
  hierarchy_instance.constructor
end
compiled_to_create() click to toggle source
# File lib/factory_girl/factory.rb, line 133
def compiled_to_create
  hierarchy_instance.to_create
end
evaluator_class() click to toggle source
# File lib/factory_girl/factory.rb, line 104
def evaluator_class
  @evaluator_class ||= EvaluatorClassDefiner.new(attributes, parent.evaluator_class).evaluator_class
end
hierarchy_class() click to toggle source
# File lib/factory_girl/factory.rb, line 115
def hierarchy_class
  @hierarchy_class ||= Class.new(parent.hierarchy_class)
end
hierarchy_instance() click to toggle source
# File lib/factory_girl/factory.rb, line 119
def hierarchy_instance
  @hierarchy_instance ||= hierarchy_class.new
end