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