Class: FactoryBot::Definition Private

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_bot/definition.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, 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.


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

def initialize(name, base_traits = [])
  @name = name
  @declarations = DeclarationList.new(name)
  @callbacks = []
  @defined_traits = Set.new
  @registered_enums = []
  @to_create = nil
  @base_traits = base_traits
  @additional_traits = []
  @constructor = nil
  @attributes = nil
  @compiled = false
  @expanded_enum_traits = 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.


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.


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

def defined_traits
  @defined_traits
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.


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

def name
  @name
end

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


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

def registered_enums
  @registered_enums
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.


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

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.


100
101
102
# File 'lib/factory_bot/definition.rb', line 100

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.


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

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.


23
24
25
26
27
28
29
30
# File 'lib/factory_bot/definition.rb', line 23

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.


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

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.


104
105
106
107
108
# File 'lib/factory_bot/definition.rb', line 104

def callback(*names, &block)
  names.each do |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.


44
45
46
# File 'lib/factory_bot/definition.rb', line 44

def callbacks
  aggregate_from_traits_and_self(:callbacks) { @callbacks }
end

#compile(klass = nil) ⇒ 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.


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/factory_bot/definition.rb', line 48

def compile(klass = nil)
  unless @compiled
    expand_enum_traits(klass) unless klass.nil?

    declarations.attributes

    defined_traits.each do |defined_trait|
      base_traits.each { |bt| bt.define_trait defined_trait }
      additional_traits.each { |at| at.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.


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

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.


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

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.


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

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.


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

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.


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

def overridable
  declarations.overridable
  self
end

#register_enum(enum) ⇒ 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.


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

def register_enum(enum)
  @registered_enums << enum
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.


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

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.


32
33
34
35
36
37
38
# File 'lib/factory_bot/definition.rb', line 32

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