1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/lib/factory_girl/attribute_assigner.rb

48 lines
1.3 KiB
Ruby
Raw Normal View History

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