2011-12-02 16:13:36 -05:00
|
|
|
module FactoryGirl
|
|
|
|
class AttributeAssigner
|
2011-12-05 20:08:47 -05:00
|
|
|
def initialize(build_class, evaluator, attribute_list)
|
2011-12-04 01:34:50 -05:00
|
|
|
@build_class = build_class
|
2011-12-05 20:08:47 -05:00
|
|
|
@evaluator = evaluator
|
2011-12-04 01:34:50 -05:00
|
|
|
@attribute_list = attribute_list
|
|
|
|
@attribute_names_assigned = []
|
2011-12-02 16:13:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def object
|
|
|
|
build_class_instance.tap do |instance|
|
2011-12-04 01:34:50 -05:00
|
|
|
attributes_to_set_on_instance.each do |attribute|
|
2011-12-02 16:13:36 -05:00
|
|
|
instance.send("#{attribute}=", get(attribute))
|
|
|
|
@attribute_names_assigned << attribute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash
|
2011-12-04 01:34:50 -05:00
|
|
|
attribute_names_to_assign.inject({}) do |result, attribute|
|
2011-12-02 16:13:36 -05:00
|
|
|
result[attribute] = get(attribute)
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def build_class_instance
|
|
|
|
@build_class_instance ||= @build_class.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(attribute_name)
|
2011-12-05 20:08:47 -05:00
|
|
|
@evaluator.send(attribute_name)
|
2011-12-02 16:13:36 -05:00
|
|
|
end
|
2011-12-04 01:34:50 -05:00
|
|
|
|
|
|
|
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)
|
2011-12-05 20:18:34 -05:00
|
|
|
override_names = @evaluator.__overrides.keys
|
2011-12-04 01:34:50 -05:00
|
|
|
non_ignored_attribute_names + override_names - ignored_attribute_names
|
|
|
|
end
|
2011-12-02 16:13:36 -05:00
|
|
|
end
|
|
|
|
end
|