2011-12-02 16:13:36 -05:00
|
|
|
module FactoryGirl
|
|
|
|
class AttributeAssigner
|
2012-04-19 21:34:47 -04:00
|
|
|
def initialize(evaluator, build_class, &instance_builder)
|
|
|
|
@build_class = build_class
|
2012-01-20 13:04:48 -05:00
|
|
|
@instance_builder = instance_builder
|
2011-12-05 20:08:47 -05:00
|
|
|
@evaluator = evaluator
|
2012-01-03 09:31:40 -05:00
|
|
|
@attribute_list = evaluator.class.attribute_list
|
2011-12-04 01:34:50 -05:00
|
|
|
@attribute_names_assigned = []
|
2011-12-02 16:13:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def object
|
2012-01-07 22:13:38 -05:00
|
|
|
@evaluator.instance = build_class_instance
|
2011-12-02 16:13:36 -05:00
|
|
|
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
|
2012-04-19 21:34:47 -04:00
|
|
|
@evaluator.instance = build_hash
|
2012-01-07 22:13:38 -05:00
|
|
|
|
2011-12-05 21:00:46 -05:00
|
|
|
attributes_to_set_on_hash.inject({}) do |result, attribute|
|
2011-12-02 16:13:36 -05:00
|
|
|
result[attribute] = get(attribute)
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def build_class_instance
|
2012-01-20 13:04:48 -05:00
|
|
|
@build_class_instance ||= @evaluator.instance_exec(&@instance_builder)
|
2011-12-02 16:13:36 -05:00
|
|
|
end
|
|
|
|
|
2012-04-19 21:34:47 -04:00
|
|
|
def build_hash
|
|
|
|
@build_hash ||= NullObject.new(hash_instance_methods_to_respond_to)
|
|
|
|
end
|
|
|
|
|
2011-12-02 16:13:36 -05:00
|
|
|
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
|
2012-03-16 13:04:31 -04:00
|
|
|
(attribute_names_to_assign - @attribute_names_assigned).uniq
|
2011-12-04 01:34:50 -05:00
|
|
|
end
|
|
|
|
|
2011-12-05 21:00:46 -05:00
|
|
|
def attributes_to_set_on_hash
|
|
|
|
attribute_names_to_assign - association_names
|
|
|
|
end
|
|
|
|
|
2011-12-04 01:34:50 -05:00
|
|
|
def attribute_names_to_assign
|
2011-12-16 14:45:03 -05:00
|
|
|
non_ignored_attribute_names + override_names - ignored_attribute_names - alias_names_to_ignore
|
2011-12-04 01:34:50 -05:00
|
|
|
end
|
2011-12-05 21:00:46 -05:00
|
|
|
|
|
|
|
def non_ignored_attribute_names
|
2012-04-24 17:40:29 -04:00
|
|
|
@attribute_list.non_ignored.names
|
2011-12-05 21:00:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ignored_attribute_names
|
2012-04-24 17:40:29 -04:00
|
|
|
@attribute_list.ignored.names
|
2011-12-05 21:00:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def association_names
|
2012-04-24 17:40:29 -04:00
|
|
|
@attribute_list.associations.names
|
2011-12-05 21:00:46 -05:00
|
|
|
end
|
2011-12-16 12:13:59 -05:00
|
|
|
|
|
|
|
def override_names
|
2012-04-20 22:33:54 -04:00
|
|
|
@evaluator.__override_names__
|
2011-12-16 12:13:59 -05:00
|
|
|
end
|
|
|
|
|
2012-04-19 21:34:47 -04:00
|
|
|
def hash_instance_methods_to_respond_to
|
2012-04-24 17:40:29 -04:00
|
|
|
@attribute_list.names + override_names + @build_class.instance_methods
|
2012-04-19 21:34:47 -04:00
|
|
|
end
|
|
|
|
|
2011-12-16 14:45:03 -05:00
|
|
|
def alias_names_to_ignore
|
2012-04-24 17:20:44 -04:00
|
|
|
@attribute_list.non_ignored.map do |attribute|
|
2012-03-13 20:12:28 -04:00
|
|
|
override_names.map {|override| attribute.name if attribute.alias_for?(override) && attribute.name != override && !ignored_attribute_names.include?(override) }
|
2011-12-16 12:13:59 -05:00
|
|
|
end.flatten.compact
|
|
|
|
end
|
2011-12-02 16:13:36 -05:00
|
|
|
end
|
|
|
|
end
|