mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
WIP: Remove runner and get acceptance tests green
This commit is contained in:
parent
148c3ec2b2
commit
9cee12a8dc
5 changed files with 40 additions and 87 deletions
|
@ -1,17 +1,15 @@
|
|||
module FactoryGirl
|
||||
class AttributeAssigner
|
||||
attr_reader :evaluator_class_instance
|
||||
|
||||
def initialize(build_class, evaluator_class_instance, attribute_names_to_assign = [])
|
||||
@build_class = build_class
|
||||
@evaluator_class_instance = evaluator_class_instance
|
||||
@attribute_names_to_assign = attribute_names_to_assign
|
||||
@attribute_names_assigned = []
|
||||
def initialize(build_class, evaluator_class_instance, attribute_list)
|
||||
@build_class = build_class
|
||||
@evaluator_class_instance = evaluator_class_instance
|
||||
@attribute_list = attribute_list
|
||||
@attribute_names_assigned = []
|
||||
end
|
||||
|
||||
def object
|
||||
build_class_instance.tap do |instance|
|
||||
(@attribute_names_to_assign - @attribute_names_assigned).each do |attribute|
|
||||
attributes_to_set_on_instance.each do |attribute|
|
||||
instance.send("#{attribute}=", get(attribute))
|
||||
@attribute_names_assigned << attribute
|
||||
end
|
||||
|
@ -19,7 +17,7 @@ module FactoryGirl
|
|||
end
|
||||
|
||||
def hash
|
||||
@attribute_names_to_assign.inject({}) do |result, attribute|
|
||||
attribute_names_to_assign.inject({}) do |result, attribute|
|
||||
result[attribute] = get(attribute)
|
||||
result
|
||||
end
|
||||
|
@ -34,5 +32,16 @@ module FactoryGirl
|
|||
def get(attribute_name)
|
||||
@evaluator_class_instance.send(attribute_name)
|
||||
end
|
||||
|
||||
def attributes_to_set_on_instance
|
||||
attribute_names_to_assign - @attribute_names_assigned
|
||||
end
|
||||
|
||||
def attribute_names_to_assign
|
||||
non_ignored_attribute_names = @attribute_list.reject(&:ignored).map(&:name)
|
||||
ignored_attribute_names = @attribute_list.select(&:ignored).map(&:name)
|
||||
override_names = @evaluator_class_instance.instance_variable_get(:@overrides).keys
|
||||
non_ignored_attribute_names + override_names - ignored_attribute_names
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
module FactoryGirl
|
||||
class Evaluator
|
||||
def initialize(build_strategy, overrides = {})
|
||||
@build_strategy = build_strategy
|
||||
@build_strategy = build_strategy
|
||||
@overrides = overrides.dup
|
||||
@cached_attributes = overrides
|
||||
end
|
||||
|
||||
|
|
|
@ -2,24 +2,27 @@ module FactoryGirl
|
|||
class EvaluatorClassDefiner
|
||||
attr_reader :attributes
|
||||
|
||||
def initialize
|
||||
@attributes = []
|
||||
def initialize(attributes)
|
||||
@attributes = attributes
|
||||
|
||||
@attributes.each do |attribute|
|
||||
define_attribute(attribute.name, attribute.to_proc)
|
||||
end
|
||||
end
|
||||
|
||||
def evaluator_class
|
||||
@evaluator_class ||= Class.new(FactoryGirl::Evaluator)
|
||||
end
|
||||
|
||||
def set(attribute)
|
||||
define_attribute(attribute.name, attribute.to_proc)
|
||||
@attributes << attribute.name unless attribute.ignored
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def define_attribute(attribute_name, attribute_proc)
|
||||
evaluator_class.send(:define_method, attribute_name) {
|
||||
@cached_attributes[attribute_name] ||= instance_exec(&attribute_proc)
|
||||
if @cached_attributes.key?(attribute_name)
|
||||
@cached_attributes[attribute_name]
|
||||
else
|
||||
@cached_attributes[attribute_name] = instance_exec(&attribute_proc)
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -35,17 +35,11 @@ module FactoryGirl
|
|||
|
||||
def run(proxy_class, overrides, &block) #:nodoc:
|
||||
block ||= lambda {|result| result }
|
||||
compile
|
||||
|
||||
runner_options = {
|
||||
:attributes => attributes,
|
||||
:callbacks => callbacks,
|
||||
:to_create => to_create,
|
||||
:build_class => build_class,
|
||||
:proxy_class => proxy_class,
|
||||
:overrides => overrides.dup
|
||||
}
|
||||
proxy = proxy_class.new(evaluator_class_definer, build_class, callbacks, overrides)
|
||||
|
||||
block[Runner.new(runner_options).run]
|
||||
block[proxy.result(to_create)]
|
||||
end
|
||||
|
||||
def human_names
|
||||
|
@ -145,61 +139,8 @@ module FactoryGirl
|
|||
@definition = @definition.clone
|
||||
end
|
||||
|
||||
class Runner
|
||||
def initialize(options = {})
|
||||
@attributes = options[:attributes]
|
||||
@callbacks = options[:callbacks]
|
||||
@to_create = options[:to_create]
|
||||
@build_class = options[:build_class]
|
||||
@proxy_class = options[:proxy_class]
|
||||
@overrides = options[:overrides]
|
||||
end
|
||||
|
||||
def run
|
||||
apply_attributes
|
||||
apply_remaining_overrides
|
||||
|
||||
proxy.result(@to_create)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def apply_attributes
|
||||
@attributes.each do |attribute|
|
||||
if overrides_for_attribute(attribute).any?
|
||||
handle_attribute_with_overrides(attribute)
|
||||
else
|
||||
handle_attribute_without_overrides(attribute)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def apply_remaining_overrides
|
||||
@overrides.each { |attr, val| add_static_attribute(attr, val) }
|
||||
end
|
||||
|
||||
def overrides_for_attribute(attribute)
|
||||
@overrides.select { |attr, val| attribute.alias_for?(attr) }
|
||||
end
|
||||
|
||||
def handle_attribute_with_overrides(attribute)
|
||||
overrides_for_attribute(attribute).each do |attr, val|
|
||||
add_static_attribute(attr, val, attribute.ignored)
|
||||
@overrides.delete(attr)
|
||||
end
|
||||
end
|
||||
|
||||
def add_static_attribute(attr, val, ignored = false)
|
||||
proxy.set(Attribute::Static.new(attr, val, ignored))
|
||||
end
|
||||
|
||||
def handle_attribute_without_overrides(attribute)
|
||||
proxy.set(attribute)
|
||||
end
|
||||
|
||||
def proxy
|
||||
@proxy ||= @proxy_class.new(@build_class, @callbacks)
|
||||
end
|
||||
def evaluator_class_definer
|
||||
@evaluator_class_definer ||= EvaluatorClassDefiner.new(attributes)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,15 +6,14 @@ require "factory_girl/proxy/stub"
|
|||
|
||||
module FactoryGirl
|
||||
class Proxy #:nodoc:
|
||||
def initialize(klass, callbacks = [])
|
||||
def initialize(evaluator_class_definer, klass, callbacks = [], overrides = {})
|
||||
@callbacks = process_callbacks(callbacks)
|
||||
@klass = klass
|
||||
@overrides = overrides
|
||||
|
||||
@evaluator_class_definer = EvaluatorClassDefiner.new
|
||||
@evaluator_class_definer = evaluator_class_definer
|
||||
end
|
||||
|
||||
delegate :set, :to => :@evaluator_class_definer
|
||||
|
||||
def run_callbacks(name)
|
||||
if @callbacks[name]
|
||||
@callbacks[name].each do |callback|
|
||||
|
@ -87,7 +86,7 @@ module FactoryGirl
|
|||
end
|
||||
|
||||
def anonymous_instance
|
||||
@anonymous_instance ||= @evaluator_class_definer.evaluator_class.new(self)
|
||||
@anonymous_instance ||= @evaluator_class_definer.evaluator_class.new(self, @overrides)
|
||||
end
|
||||
|
||||
def attribute_assigner
|
||||
|
|
Loading…
Reference in a new issue