thoughtbot--factory_bot/lib/factory_bot/evaluator.rb

83 lines
2.2 KiB
Ruby
Raw Normal View History

require "active_support/core_ext/hash/except"
require "active_support/core_ext/class/attribute"
2012-01-03 14:31:40 +00:00
module FactoryBot
2012-05-05 06:31:31 +00:00
# @api private
class Evaluator
class_attribute :attribute_lists
2012-01-03 14:31:40 +00:00
private_instance_methods.each do |method|
undef_method(method) unless method =~ /^__|initialize/
end
def initialize(build_strategy, overrides = {})
2013-01-18 18:27:30 +00:00
@build_strategy = build_strategy
@overrides = overrides
@cached_attributes = overrides
2013-01-18 18:27:30 +00:00
@instance = nil
@overrides.each do |name, value|
singleton_class.define_attribute(name) { value }
end
end
def association(factory_name, *traits_and_overrides)
overrides = traits_and_overrides.extract_options!
strategy_override = overrides.fetch(:strategy) do
FactoryBot.use_parent_strategy ? @build_strategy.class : :create
end
traits_and_overrides += [overrides.except(:strategy)]
runner = FactoryRunner.new(factory_name, strategy_override, traits_and_overrides)
@build_strategy.association(runner)
end
def instance=(object_instance)
@instance = object_instance
end
def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissing
if @instance.respond_to?(method_name)
@instance.send(method_name, *args, &block)
else
SyntaxRunner.new.send(method_name, *args, &block)
end
end
2018-10-05 18:54:08 +00:00
def respond_to_missing?(method_name, _include_private = false)
@instance.respond_to?(method_name) || SyntaxRunner.new.respond_to?(method_name)
end
def __override_names__
@overrides.keys
end
2012-05-05 04:53:19 +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)
if instance_methods(false).include?(name) || private_instance_methods(false).include?(name)
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
end
end