2012-05-05 10:53:05 -04:00
|
|
|
require 'active_support/core_ext/hash/except'
|
|
|
|
require 'active_support/core_ext/class/attribute'
|
2012-01-03 09:31:40 -05:00
|
|
|
|
2011-12-02 13:09:35 -05:00
|
|
|
module FactoryGirl
|
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|
|
|
|
|
undef_method(method) unless method =~ /^__|initialize/
|
|
|
|
end
|
2011-12-18 23:10:03 -05:00
|
|
|
|
2012-04-23 22:48:33 -05:00
|
|
|
def initialize(build_class, build_strategy, overrides = {})
|
|
|
|
@build_class = build_class
|
2011-12-04 01:34:50 -05:00
|
|
|
@build_strategy = build_strategy
|
2011-12-16 14:27:38 -05:00
|
|
|
@overrides = overrides
|
2011-12-02 13:09:35 -05:00
|
|
|
@cached_attributes = overrides
|
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-04-23 22:48:33 -05:00
|
|
|
delegate :new, to: :@build_class
|
|
|
|
|
2012-02-08 12:13:27 -05:00
|
|
|
def association(factory_name, overrides = {})
|
2012-04-25 14:29:02 -05:00
|
|
|
strategy_override = overrides.fetch(:strategy) { :create }
|
2012-02-17 11:29:11 -05:00
|
|
|
|
2012-04-25 14:29:02 -05:00
|
|
|
runner = FactoryRunner.new(factory_name, strategy_override, [overrides.except(:strategy)])
|
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
|
|
|
|
|
2011-12-02 13:09:35 -05:00
|
|
|
def method_missing(method_name, *args, &block)
|
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
|
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
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
|