Class: FactoryBot::Definition Private
- Inherits:
-
Object
- Object
- FactoryBot::Definition
- 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
- #declarations ⇒ Object readonly private
- #defined_traits ⇒ Object readonly private
- #klass ⇒ Object private
- #name ⇒ Object readonly private
- #registered_enums ⇒ Object readonly private
- #uri_manager ⇒ Object readonly private
Instance Method Summary collapse
- #add_callback(callback) ⇒ Object private
- #after(*names, &block) ⇒ Object private
- #append_traits(new_traits) ⇒ Object private
- #attributes ⇒ Object private
- #before(*names, &block) ⇒ Object private
- #callback(*names, &block) ⇒ Object private
- #callbacks ⇒ Object private
- #compile(klass = nil) ⇒ Object private
- #constructor ⇒ Object private
- #define_constructor(&block) ⇒ Object private
- #define_trait(trait) ⇒ Object private
- #defined_traits_names ⇒ Object private
- #inherit_traits(new_traits) ⇒ Object private
-
#initialize(name, base_traits = [], **opts) ⇒ Definition
constructor
private
A new instance of Definition.
- #overridable ⇒ Object private
- #register_enum(enum) ⇒ Object private
- #skip_create ⇒ Object private
- #to_create(&block) ⇒ Object private
Constructor Details
#initialize(name, base_traits = [], **opts) ⇒ 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.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/factory_bot/definition.rb', line 7 def initialize(name, base_traits = [], **opts) @name = name @uri_manager = opts[:uri_manager] @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
#declarations ⇒ 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.
4 5 6 |
# File 'lib/factory_bot/definition.rb', line 4 def declarations @declarations end |
#defined_traits ⇒ 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.
4 5 6 |
# File 'lib/factory_bot/definition.rb', line 4 def defined_traits @defined_traits end |
#klass ⇒ 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.
5 6 7 |
# File 'lib/factory_bot/definition.rb', line 5 def klass @klass 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.
4 5 6 |
# File 'lib/factory_bot/definition.rb', line 4 def name @name end |
#registered_enums ⇒ 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.
4 5 6 |
# File 'lib/factory_bot/definition.rb', line 4 def registered_enums @registered_enums end |
#uri_manager ⇒ 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.
4 5 6 |
# File 'lib/factory_bot/definition.rb', line 4 def uri_manager @uri_manager 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.
87 88 89 |
# File 'lib/factory_bot/definition.rb', line 87 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.
115 116 117 |
# File 'lib/factory_bot/definition.rb', line 115 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.
83 84 85 |
# File 'lib/factory_bot/definition.rb', line 83 def append_traits(new_traits) @additional_traits += new_traits end |
#attributes ⇒ 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.
25 26 27 28 29 30 31 32 |
# File 'lib/factory_bot/definition.rb', line 25 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.
111 112 113 |
# File 'lib/factory_bot/definition.rb', line 111 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.
119 120 121 122 123 |
# File 'lib/factory_bot/definition.rb', line 119 def callback(*names, &block) names.each do |name| add_callback(Callback.new(name, block)) end end |
#callbacks ⇒ 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.
46 47 48 |
# File 'lib/factory_bot/definition.rb', line 46 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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/factory_bot/definition.rb', line 50 def compile(klass = nil) unless @compiled (klass) unless klass.nil? declarations.attributes self.klass ||= klass defined_traits.each do |defined_trait| defined_trait.klass ||= klass base_traits.each { |bt| bt.define_trait defined_trait } additional_traits.each { |at| at.define_trait defined_trait } end @compiled = true ActiveSupport::Notifications.instrument "factory_bot.compile_factory", { name: name, attributes: declarations.attributes, traits: defined_traits, class: klass || self.klass } end end |
#constructor ⇒ 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.
42 43 44 |
# File 'lib/factory_bot/definition.rb', line 42 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.
107 108 109 |
# File 'lib/factory_bot/definition.rb', line 107 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.
95 96 97 |
# File 'lib/factory_bot/definition.rb', line 95 def define_trait(trait) @defined_traits.add(trait) end |
#defined_traits_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.
99 100 101 |
# File 'lib/factory_bot/definition.rb', line 99 def defined_traits_names @defined_traits.map(&:name) 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.
79 80 81 |
# File 'lib/factory_bot/definition.rb', line 79 def inherit_traits(new_traits) @base_traits += new_traits end |
#overridable ⇒ 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.
74 75 76 77 |
# File 'lib/factory_bot/definition.rb', line 74 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.
103 104 105 |
# File 'lib/factory_bot/definition.rb', line 103 def register_enum(enum) @registered_enums << enum end |
#skip_create ⇒ 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.
91 92 93 |
# File 'lib/factory_bot/definition.rb', line 91 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.
34 35 36 37 38 39 40 |
# File 'lib/factory_bot/definition.rb', line 34 def to_create(&block) if block @to_create = block else aggregate_from_traits_and_self(:to_create) { @to_create }.last end end |