Class: FactoryBot::DefinitionProxy
- Inherits:
-
Object
- Object
- FactoryBot::DefinitionProxy
- Defined in:
- lib/factory_bot/definition_proxy.rb
Constant Summary collapse
- UNPROXIED_METHODS =
%w( __send__ __id__ nil? send object_id extend instance_eval initialize block_given? raise caller method ).freeze
Instance Attribute Summary collapse
-
#child_factories ⇒ Object
readonly
Returns the value of attribute child_factories.
Instance Method Summary collapse
-
#add_attribute(name, &block) ⇒ Object
Adds an attribute to the factory.
-
#association(name, *options) ⇒ Object
Adds an attribute that builds an association.
- #factory(name, options = {}, &block) ⇒ Object
-
#initialize(definition, ignore = false) ⇒ DefinitionProxy
constructor
A new instance of DefinitionProxy.
- #initialize_with(&block) ⇒ Object
-
#method_missing(name, *args, &block) ⇒ Object
Calls add_attribute using the missing method name as the name of the attribute, so that:.
-
#sequence(name, *args, &block) ⇒ Object
Adds an attribute that will have unique values generated by a sequence with a specified format.
- #singleton_method_added(name) ⇒ Object
- #skip_create ⇒ Object
- #to_create(&block) ⇒ Object
- #trait(name, &block) ⇒ Object
- #transient(&block) ⇒ Object
Constructor Details
#initialize(definition, ignore = false) ⇒ DefinitionProxy
Returns a new instance of DefinitionProxy.
26 27 28 29 30 |
# File 'lib/factory_bot/definition_proxy.rb', line 26 def initialize(definition, ignore = false) @definition = definition @ignore = ignore @child_factories = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Calls add_attribute using the missing method name as the name of the attribute, so that:
factory :user do
name { 'Billy Idol' }
end
and:
factory :user do
add_attribute(:name) { 'Billy Idol' }
end
are equivalent.
If no argument or block is given, factory_bot will first look for an association, then for a sequence, and finally for a trait with the same name. This means that given an “admin” trait, an “email” sequence, and an “account” factory:
factory :user, traits: [:admin] do
email { generate(:email) }
association :account
end
and:
factory :user do
admin
email
account
end
are equivalent.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/factory_bot/definition_proxy.rb', line 91 def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing if args.empty? __declare_attribute__(name, block) elsif args.first.respond_to?(:has_key?) && args.first.has_key?(:factory) association(name, *args) else raise NoMethodError.new( "undefined method '#{name}' in '#{@definition.name}' factory", ) end end |
Instance Attribute Details
#child_factories ⇒ Object (readonly)
Returns the value of attribute child_factories.
24 25 26 |
# File 'lib/factory_bot/definition_proxy.rb', line 24 def child_factories @child_factories end |
Instance Method Details
#add_attribute(name, &block) ⇒ Object
Adds an attribute to the factory. The attribute value will be generated “lazily” by calling the block whenever an instance is generated. The block will not be called if the attribute is overridden for a specific instance.
Arguments:
-
name:
SymbolorStringThe name of this attribute. This will be assigned using “name=” for generated instances.
47 48 49 50 |
# File 'lib/factory_bot/definition_proxy.rb', line 47 def add_attribute(name, &block) declaration = Declaration::Dynamic.new(name, @ignore, block) @definition.declare_attribute(declaration) end |
#association(name, *options) ⇒ Object
Adds an attribute that builds an association. The associated instance will be built using the same build strategy as the parent instance.
Example:
factory :user do
name 'Joey'
end
factory :post do
association :author, factory: :user
end
Arguments:
-
name:
SymbolThe name of this attribute. -
options:
Hash
Options:
-
factory:
SymbolorStringThe name of the factory to use when building the associated instance. If no name is given, the name of the attribute is assumed to be the name of the factory. For example, a "user" association will by default use the "user" factory.
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/factory_bot/definition_proxy.rb', line 149 def association(name, *) if block_given? raise AssociationDefinitionError.new( "Unexpected block passed to '#{name}' association "\ "in '#{@definition.name}' factory", ) else declaration = Declaration::Association.new(name, *) @definition.declare_attribute(declaration) end end |
#factory(name, options = {}, &block) ⇒ Object
169 170 171 |
# File 'lib/factory_bot/definition_proxy.rb', line 169 def factory(name, = {}, &block) @child_factories << [name, , block] end |
#initialize_with(&block) ⇒ Object
177 178 179 |
# File 'lib/factory_bot/definition_proxy.rb', line 177 def initialize_with(&block) @definition.define_constructor(&block) end |
#sequence(name, *args, &block) ⇒ Object
Adds an attribute that will have unique values generated by a sequence with a specified format.
The result of:
factory :user do
sequence(:email) { |n| "person#{n}@example.com" }
end
Is equal to:
sequence(:email) { |n| "person#{n}@example.com" }
factory :user do
email { FactoryBot.generate(:email) }
end
Except that no globally available sequence will be defined.
119 120 121 122 123 124 |
# File 'lib/factory_bot/definition_proxy.rb', line 119 def sequence(name, *args, &block) sequence_name = "__#{@definition.name}_#{name}__" sequence = Sequence.new(sequence_name, *args, &block) FactoryBot.register_sequence(sequence) add_attribute(name) { increment_sequence(sequence) } end |
#singleton_method_added(name) ⇒ Object
32 33 34 35 |
# File 'lib/factory_bot/definition_proxy.rb', line 32 def singleton_method_added(name) = "Defining methods in blocks (trait or factory) is not supported (#{name})" raise FactoryBot::MethodDefinitionError, end |
#skip_create ⇒ Object
165 166 167 |
# File 'lib/factory_bot/definition_proxy.rb', line 165 def skip_create @definition.skip_create end |
#to_create(&block) ⇒ Object
161 162 163 |
# File 'lib/factory_bot/definition_proxy.rb', line 161 def to_create(&block) @definition.to_create(&block) end |
#trait(name, &block) ⇒ Object
173 174 175 |
# File 'lib/factory_bot/definition_proxy.rb', line 173 def trait(name, &block) @definition.define_trait(Trait.new(name, &block)) end |
#transient(&block) ⇒ Object
52 53 54 55 |
# File 'lib/factory_bot/definition_proxy.rb', line 52 def transient(&block) proxy = DefinitionProxy.new(@definition, true) proxy.instance_eval(&block) end |