1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/lib/factory_bot/definition_proxy.rb

169 lines
4.7 KiB
Ruby
Raw Normal View History

module FactoryBot
class DefinitionProxy
2018-10-07 18:02:54 -04:00
UNPROXIED_METHODS = %w(__send__ __id__ nil? send object_id extend instance_eval initialize block_given? raise caller method).freeze
(instance_methods + private_instance_methods).each do |method|
undef_method(method) unless UNPROXIED_METHODS.include?(method.to_s)
end
delegate :before, :after, :callback, to: :@definition
attr_reader :child_factories
2011-08-12 14:49:21 -04:00
def initialize(definition, ignore = false)
@definition = definition
@ignore = ignore
@child_factories = []
end
def singleton_method_added(name)
message = "Defining methods in blocks (trait or factory) is not supported (#{name})"
raise FactoryBot::MethodDefinitionError, message
end
# 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: +Symbol+ or +String+
2011-02-07 17:48:00 -05:00
# The name of this attribute. This will be assigned using "name=" for
# generated instances.
def add_attribute(name, &block)
declaration = Declaration::Dynamic.new(name, @ignore, block)
@definition.declare_attribute(declaration)
end
def transient(&block)
proxy = DefinitionProxy.new(@definition, true)
proxy.instance_eval(&block)
end
# Calls add_attribute using the missing method name as the name of the
# attribute, so that:
#
2010-10-01 18:50:01 -04:00
# factory :user do
# name { 'Billy Idol' }
# end
#
# and:
#
2010-10-01 18:50:01 -04:00
# factory :user do
# add_attribute(:name) { 'Billy Idol' }
2010-10-01 18:50:01 -04:00
# end
#
2011-07-01 19:08:03 -04:00
# are equivalent.
2010-10-01 18:50:01 -04:00
#
# If no argument or block is given, factory_bot will look for a sequence
2010-10-01 18:50:01 -04:00
# or association with the same name. This means that:
#
# factory :user do
# email { create(:email) }
2010-10-01 18:50:01 -04:00
# association :account
# end
#
# and:
#
# factory :user do
# email
# account
# end
#
2011-07-01 19:08:03 -04:00
# are equivalent.
def method_missing(name, *args, &block)
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
# Adds an attribute that will have unique values generated by a sequence with
# a specified format.
#
# The result of:
2010-10-01 18:50:01 -04:00
# factory :user do
# sequence(:email) { |n| "person#{n}@example.com" }
# end
#
# Is equal to:
2010-10-01 18:50:01 -04:00
# sequence(:email) { |n| "person#{n}@example.com" }
#
2010-10-01 18:50:01 -04:00
# factory :user do
# email { FactoryBot.generate(:email) }
# end
#
# Except that no globally available sequence will be defined.
2012-04-06 14:41:13 -04:00
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
# Adds an attribute that builds an association. The associated instance will
# be built using the same build strategy as the parent instance.
#
# Example:
2010-10-01 18:50:01 -04:00
# factory :user do
# name 'Joey'
# end
#
2010-10-01 18:50:01 -04:00
# factory :post do
2012-03-09 17:20:38 -05:00
# association :author, factory: :user
# end
#
# Arguments:
# * name: +Symbol+
# The name of this attribute.
# * options: +Hash+
#
# Options:
# * factory: +Symbol+ or +String+
# The 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.
def association(name, *options)
@definition.declare_attribute(Declaration::Association.new(name, *options))
end
def to_create(&block)
@definition.to_create(&block)
end
def skip_create
@definition.skip_create
end
def factory(name, options = {}, &block)
@child_factories << [name, options, block]
end
2011-08-12 14:49:21 -04:00
2011-08-12 16:16:17 -04:00
def trait(name, &block)
@definition.define_trait(Trait.new(name, &block))
2011-08-09 20:29:02 -04:00
end
def initialize_with(&block)
@definition.define_constructor(&block)
end
private
def __declare_attribute__(name, block)
if block.nil?
declaration = Declaration::Implicit.new(name, @definition, @ignore)
@definition.declare_attribute(declaration)
else
add_attribute(name, &block)
end
end
end
end