2018-10-07 21:45:51 -04:00
|
|
|
require "active_support/core_ext/hash/except"
|
|
|
|
require "active_support/core_ext/class/attribute"
|
2012-01-03 09:31:40 -05:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
module FactoryBot
|
2012-05-05 02:31:31 -04:00
|
|
|
# @api private
|
2011-12-02 13:09:35 -05:00
|
|
|
class Evaluator
|
2012-04-13 14:20:19 -04:00
|
|
|
class_attribute :attribute_lists
|
2012-01-03 09:31:40 -05:00
|
|
|
|
2012-01-28 19:00:35 -05:00
|
|
|
private_instance_methods.each do |method|
|
2020-04-24 17:11:12 -04:00
|
|
|
undef_method(method) unless method.match?(/^__|initialize/)
|
2012-01-28 19:00:35 -05:00
|
|
|
end
|
2011-12-18 23:10:03 -05:00
|
|
|
|
2012-07-24 18:50:11 -04:00
|
|
|
def initialize(build_strategy, overrides = {})
|
2013-01-18 13:27:30 -05:00
|
|
|
@build_strategy = build_strategy
|
|
|
|
@overrides = overrides
|
2011-12-02 13:09:35 -05:00
|
|
|
@cached_attributes = overrides
|
2013-01-18 13:27:30 -05:00
|
|
|
@instance = nil
|
2011-12-16 11:37:30 -05:00
|
|
|
|
2012-01-28 19:00:35 -05:00
|
|
|
@overrides.each do |name, value|
|
2012-05-05 00:49:04 -04:00
|
|
|
singleton_class.define_attribute(name) { value }
|
2012-01-28 19:00:35 -05:00
|
|
|
end
|
2011-12-02 13:09:35 -05:00
|
|
|
end
|
|
|
|
|
2012-06-29 11:06:08 -04:00
|
|
|
def association(factory_name, *traits_and_overrides)
|
|
|
|
overrides = traits_and_overrides.extract_options!
|
2016-12-16 05:28:09 -05:00
|
|
|
strategy_override = overrides.fetch(:strategy) do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.use_parent_strategy ? @build_strategy.class : :create
|
2016-12-16 05:28:09 -05:00
|
|
|
end
|
2012-02-17 11:29:11 -05:00
|
|
|
|
2012-06-29 11:06:08 -04:00
|
|
|
traits_and_overrides += [overrides.except(:strategy)]
|
|
|
|
|
|
|
|
runner = FactoryRunner.new(factory_name, strategy_override, traits_and_overrides)
|
2012-02-10 16:56:07 -05:00
|
|
|
@build_strategy.association(runner)
|
2012-02-08 12:13:27 -05:00
|
|
|
end
|
2011-12-02 13:17:57 -05:00
|
|
|
|
2012-01-07 22:13:38 -05:00
|
|
|
def instance=(object_instance)
|
|
|
|
@instance = object_instance
|
|
|
|
end
|
|
|
|
|
2019-05-12 22:43:16 -04:00
|
|
|
def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissingSuper
|
2012-05-18 11:51:59 -04:00
|
|
|
if @instance.respond_to?(method_name)
|
|
|
|
@instance.send(method_name, *args, &block)
|
2011-12-02 13:09:35 -05:00
|
|
|
else
|
2012-05-18 11:51:59 -04:00
|
|
|
SyntaxRunner.new.send(method_name, *args, &block)
|
2011-12-02 13:09:35 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-05 20:18:34 -05:00
|
|
|
|
2018-10-05 14:54:08 -04:00
|
|
|
def respond_to_missing?(method_name, _include_private = false)
|
2013-01-11 12:05:17 -05:00
|
|
|
@instance.respond_to?(method_name) || SyntaxRunner.new.respond_to?(method_name)
|
|
|
|
end
|
|
|
|
|
2012-04-20 22:33:54 -04:00
|
|
|
def __override_names__
|
|
|
|
@overrides.keys
|
2011-12-05 20:18:34 -05:00
|
|
|
end
|
2012-05-05 00:53:19 -04:00
|
|
|
|
2013-01-11 12:05:17 -05:00
|
|
|
def increment_sequence(sequence)
|
|
|
|
sequence.next(self)
|
|
|
|
end
|
|
|
|
|
2012-05-05 00:53:19 -04:00
|
|
|
def self.attribute_list
|
|
|
|
AttributeList.new.tap do |list|
|
|
|
|
attribute_lists.each do |attribute_list|
|
|
|
|
list.apply_attributes attribute_list.to_a
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.define_attribute(name, &block)
|
2019-03-20 19:24:37 -04:00
|
|
|
if instance_methods(false).include?(name) || private_instance_methods(false).include?(name)
|
2015-07-21 05:52:21 -04:00
|
|
|
undef_method(name)
|
|
|
|
end
|
|
|
|
|
2012-05-05 00:53:19 -04:00
|
|
|
define_method(name) do
|
|
|
|
if @cached_attributes.key?(name)
|
|
|
|
@cached_attributes[name]
|
|
|
|
else
|
|
|
|
@cached_attributes[name] = instance_exec(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-12-02 13:09:35 -05:00
|
|
|
end
|
|
|
|
end
|