Class: FactoryBot::Factory Private

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_bot/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
18
19
20
21
22
23
# File 'lib/factory_bot/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]
  @uri_manager = FactoryBot::UriManager.new(names)
  @definition = Definition.new(
    @name,
    options[:traits] || [],
    automatically_define_enum_traits: options[:automatically_define_enum_traits],
    uri_manager: @uri_manager
  )
  @compiled = false
end

Instance Attribute Details

#definition ⇒ Object (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_bot/factory.rb', line 7

def definition
  @definition
end

#name ⇒ Object (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_bot/factory.rb', line 7

def name
  @name
end

Instance Method Details

#associations ⇒ 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.



63
64
65
# File 'lib/factory_bot/factory.rb', line 63

def associations
  evaluator_class.attribute_list.associations
end

#build_class ⇒ 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.



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

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

#compile ⇒ 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.



96
97
98
99
100
101
102
103
104
105
# File 'lib/factory_bot/factory.rb', line 96

def compile
  unless @compiled
    parent.compile
    inherit_parent_traits
    @definition.inherit_automatically_define_enum_traits(parent.definition)
    @definition.compile(build_class)
    build_hierarchy
    @compiled = true
  end
end

#human_names ⇒ 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.



59
60
61
# File 'lib/factory_bot/factory.rb', line 59

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

#names ⇒ 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.

Names for this factory, including aliases.

Example:

factory :user, aliases: [:author] do
# ...
end

FactoryBot.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

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


92
93
94
# File 'lib/factory_bot/factory.rb', line 92

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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/factory_bot/factory.rb', line 39

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

  compile

  strategy = Strategy.lookup_strategy(build_strategy).new

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

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

  evaluation.notify(:before_all, nil)
  instance = strategy.result(evaluation).tap(&block)
  evaluation.notify(:after_all, instance)

  instance
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.



107
108
109
110
111
# File 'lib/factory_bot/factory.rb', line 107

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