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
|
module FactoryGirl
|
||||||
class AttributeAssigner
|
class AttributeAssigner
|
||||||
attr_reader :evaluator_class_instance
|
def initialize(build_class, evaluator_class_instance, attribute_list)
|
||||||
|
@build_class = build_class
|
||||||
def initialize(build_class, evaluator_class_instance, attribute_names_to_assign = [])
|
@evaluator_class_instance = evaluator_class_instance
|
||||||
@build_class = build_class
|
@attribute_list = attribute_list
|
||||||
@evaluator_class_instance = evaluator_class_instance
|
@attribute_names_assigned = []
|
||||||
@attribute_names_to_assign = attribute_names_to_assign
|
|
||||||
@attribute_names_assigned = []
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def object
|
def object
|
||||||
build_class_instance.tap do |instance|
|
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))
|
instance.send("#{attribute}=", get(attribute))
|
||||||
@attribute_names_assigned << attribute
|
@attribute_names_assigned << attribute
|
||||||
end
|
end
|
||||||
|
@ -19,7 +17,7 @@ module FactoryGirl
|
||||||
end
|
end
|
||||||
|
|
||||||
def hash
|
def hash
|
||||||
@attribute_names_to_assign.inject({}) do |result, attribute|
|
attribute_names_to_assign.inject({}) do |result, attribute|
|
||||||
result[attribute] = get(attribute)
|
result[attribute] = get(attribute)
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
@ -34,5 +32,16 @@ module FactoryGirl
|
||||||
def get(attribute_name)
|
def get(attribute_name)
|
||||||
@evaluator_class_instance.send(attribute_name)
|
@evaluator_class_instance.send(attribute_name)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
module FactoryGirl
|
module FactoryGirl
|
||||||
class Evaluator
|
class Evaluator
|
||||||
def initialize(build_strategy, overrides = {})
|
def initialize(build_strategy, overrides = {})
|
||||||
@build_strategy = build_strategy
|
@build_strategy = build_strategy
|
||||||
|
@overrides = overrides.dup
|
||||||
@cached_attributes = overrides
|
@cached_attributes = overrides
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,24 +2,27 @@ module FactoryGirl
|
||||||
class EvaluatorClassDefiner
|
class EvaluatorClassDefiner
|
||||||
attr_reader :attributes
|
attr_reader :attributes
|
||||||
|
|
||||||
def initialize
|
def initialize(attributes)
|
||||||
@attributes = []
|
@attributes = attributes
|
||||||
|
|
||||||
|
@attributes.each do |attribute|
|
||||||
|
define_attribute(attribute.name, attribute.to_proc)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def evaluator_class
|
def evaluator_class
|
||||||
@evaluator_class ||= Class.new(FactoryGirl::Evaluator)
|
@evaluator_class ||= Class.new(FactoryGirl::Evaluator)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(attribute)
|
|
||||||
define_attribute(attribute.name, attribute.to_proc)
|
|
||||||
@attributes << attribute.name unless attribute.ignored
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def define_attribute(attribute_name, attribute_proc)
|
def define_attribute(attribute_name, attribute_proc)
|
||||||
evaluator_class.send(:define_method, attribute_name) {
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -35,17 +35,11 @@ module FactoryGirl
|
||||||
|
|
||||||
def run(proxy_class, overrides, &block) #:nodoc:
|
def run(proxy_class, overrides, &block) #:nodoc:
|
||||||
block ||= lambda {|result| result }
|
block ||= lambda {|result| result }
|
||||||
|
compile
|
||||||
|
|
||||||
runner_options = {
|
proxy = proxy_class.new(evaluator_class_definer, build_class, callbacks, overrides)
|
||||||
:attributes => attributes,
|
|
||||||
:callbacks => callbacks,
|
|
||||||
:to_create => to_create,
|
|
||||||
:build_class => build_class,
|
|
||||||
:proxy_class => proxy_class,
|
|
||||||
:overrides => overrides.dup
|
|
||||||
}
|
|
||||||
|
|
||||||
block[Runner.new(runner_options).run]
|
block[proxy.result(to_create)]
|
||||||
end
|
end
|
||||||
|
|
||||||
def human_names
|
def human_names
|
||||||
|
@ -145,61 +139,8 @@ module FactoryGirl
|
||||||
@definition = @definition.clone
|
@definition = @definition.clone
|
||||||
end
|
end
|
||||||
|
|
||||||
class Runner
|
def evaluator_class_definer
|
||||||
def initialize(options = {})
|
@evaluator_class_definer ||= EvaluatorClassDefiner.new(attributes)
|
||||||
@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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,15 +6,14 @@ require "factory_girl/proxy/stub"
|
||||||
|
|
||||||
module FactoryGirl
|
module FactoryGirl
|
||||||
class Proxy #:nodoc:
|
class Proxy #:nodoc:
|
||||||
def initialize(klass, callbacks = [])
|
def initialize(evaluator_class_definer, klass, callbacks = [], overrides = {})
|
||||||
@callbacks = process_callbacks(callbacks)
|
@callbacks = process_callbacks(callbacks)
|
||||||
@klass = klass
|
@klass = klass
|
||||||
|
@overrides = overrides
|
||||||
|
|
||||||
@evaluator_class_definer = EvaluatorClassDefiner.new
|
@evaluator_class_definer = evaluator_class_definer
|
||||||
end
|
end
|
||||||
|
|
||||||
delegate :set, :to => :@evaluator_class_definer
|
|
||||||
|
|
||||||
def run_callbacks(name)
|
def run_callbacks(name)
|
||||||
if @callbacks[name]
|
if @callbacks[name]
|
||||||
@callbacks[name].each do |callback|
|
@callbacks[name].each do |callback|
|
||||||
|
@ -87,7 +86,7 @@ module FactoryGirl
|
||||||
end
|
end
|
||||||
|
|
||||||
def anonymous_instance
|
def anonymous_instance
|
||||||
@anonymous_instance ||= @evaluator_class_definer.evaluator_class.new(self)
|
@anonymous_instance ||= @evaluator_class_definer.evaluator_class.new(self, @overrides)
|
||||||
end
|
end
|
||||||
|
|
||||||
def attribute_assigner
|
def attribute_assigner
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue