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!
|
2020-06-05 15:15:18 -04:00
|
|
|
strategy_override = overrides.fetch(:strategy) {
|
2022-02-03 21:41:09 -05:00
|
|
|
FactoryBot.use_parent_strategy ? @build_strategy.to_sym : :create
|
2020-06-05 15:15:18 -04:00
|
|
|
}
|
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
|
|
|
|
2020-07-08 12:29:36 -04:00
|
|
|
attr_accessor :instance
|
2012-01-07 22:13:38 -05:00
|
|
|
|
2022-01-14 15:08:08 -05:00
|
|
|
def method_missing(method_name, *args, &block)
|
2021-02-13 05:58:41 -05:00
|
|
|
if @instance.respond_to?(method_name)
|
|
|
|
@instance.send(method_name, *args, &block)
|
|
|
|
else
|
|
|
|
SyntaxRunner.new.send(method_name, *args, &block)
|
2011-12-02 13:09:35 -05:00
|
|
|
end
|
|
|
|
end
|
2021-02-13 05:58:41 -05:00
|
|
|
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
|
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
|