Class: FactoryBot::Definition Private

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_bot/definition.rb

Overview

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.

API:

  • private

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, base_traits = []) ⇒ Definition

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

API:

  • private



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/factory_bot/definition.rb', line 6

def initialize(name = nil, base_traits = [])
  @declarations      = DeclarationList.new(name)
  @callbacks         = []
  @defined_traits    = Set.new
  @to_create         = nil
  @base_traits       = base_traits
  @additional_traits = []
  @constructor       = nil
  @attributes        = nil
  @compiled          = false
end

Instance Attribute Details

#declarationsObject (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.

API:

  • private



4
5
6
# File 'lib/factory_bot/definition.rb', line 4

def declarations
  @declarations
end

#defined_traitsObject (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.

API:

  • private



4
5
6
# File 'lib/factory_bot/definition.rb', line 4

def defined_traits
  @defined_traits
end

Instance Method Details

#add_callback(callback) ⇒ 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.

API:

  • private



71
72
73
# File 'lib/factory_bot/definition.rb', line 71

def add_callback(callback)
  @callbacks << callback
end

#after(*names, &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.

API:

  • private



91
92
93
# File 'lib/factory_bot/definition.rb', line 91

def after(*names, &block)
  callback(*names.map { |name| "after_#{name}" }, &block)
end

#append_traits(new_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.

API:

  • private



67
68
69
# File 'lib/factory_bot/definition.rb', line 67

def append_traits(new_traits)
  @additional_traits += new_traits
end

#attributesObject

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.

API:

  • private



20
21
22
23
24
25
26
27
# File 'lib/factory_bot/definition.rb', line 20

def attributes
  @attributes ||= AttributeList.new.tap do |attribute_list|
    attribute_lists = aggregate_from_traits_and_self(:attributes) { declarations.attributes }
    attribute_lists.each do |attributes|
      attribute_list.apply_attributes attributes
    end
  end
end

#before(*names, &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.

API:

  • private



87
88
89
# File 'lib/factory_bot/definition.rb', line 87

def before(*names, &block)
  callback(*names.map { |name| "before_#{name}" }, &block)
end

#callback(*names, &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.

API:

  • private



95
96
97
98
99
100
# File 'lib/factory_bot/definition.rb', line 95

def callback(*names, &block)
  names.each do |name|
    FactoryBot.register_callback(name)
    add_callback(Callback.new(name, block))
  end
end

#callbacksObject

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.

API:

  • private



41
42
43
# File 'lib/factory_bot/definition.rb', line 41

def callbacks
  aggregate_from_traits_and_self(:callbacks) { @callbacks }
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.

API:

  • private



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/factory_bot/definition.rb', line 45

def compile
  unless @compiled
    declarations.attributes

    defined_traits.each do |defined_trait|
      base_traits.each       { |bt| bt.define_trait defined_trait }
      additional_traits.each { |bt| bt.define_trait defined_trait }
    end

    @compiled = true
  end
end

#constructorObject

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.

API:

  • private



37
38
39
# File 'lib/factory_bot/definition.rb', line 37

def constructor
  aggregate_from_traits_and_self(:constructor) { @constructor }.last
end

#define_constructor(&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.

API:

  • private



83
84
85
# File 'lib/factory_bot/definition.rb', line 83

def define_constructor(&block)
  @constructor = block
end

#define_trait(trait) ⇒ 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.

API:

  • private



79
80
81
# File 'lib/factory_bot/definition.rb', line 79

def define_trait(trait)
  @defined_traits.add(trait)
end

#inherit_traits(new_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.

API:

  • private



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

def inherit_traits(new_traits)
  @base_traits += new_traits
end

#overridableObject

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.

API:

  • private



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

def overridable
  declarations.overridable
  self
end

#skip_createObject

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.

API:

  • private



75
76
77
# File 'lib/factory_bot/definition.rb', line 75

def skip_create
  @to_create = ->(instance) { }
end

#to_create(&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.

API:

  • private



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

def to_create(&block)
  if block_given?
    @to_create = block
  else
    aggregate_from_traits_and_self(:to_create) { @to_create }.last
  end
end