2017-10-20 15:20:28 -04:00
|
|
|
module FactoryBot
|
2012-05-05 02:31:31 -04:00
|
|
|
# @api private
|
2011-12-02 16:13:36 -05:00
|
|
|
class AttributeAssigner
|
2012-04-19 21:34:47 -04:00
|
|
|
def initialize(evaluator, build_class, &instance_builder)
|
2020-06-05 15:15:18 -04:00
|
|
|
@build_class = build_class
|
|
|
|
@instance_builder = instance_builder
|
|
|
|
@evaluator = evaluator
|
|
|
|
@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|
|
2014-05-29 14:09:00 -04:00
|
|
|
instance.public_send("#{attribute}=", get(attribute))
|
2011-12-02 16:13:36 -05:00
|
|
|
@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
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
attributes_to_set_on_hash.each_with_object({}) do |attribute, result|
|
2011-12-02 16:13:36 -05:00
|
|
|
result[attribute] = get(attribute)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-05-18 14:28:51 -04:00
|
|
|
def method_tracking_evaluator
|
2018-10-22 22:29:02 -04:00
|
|
|
@method_tracking_evaluator ||= Decorator::AttributeHash.new(
|
|
|
|
decorated_evaluator,
|
2020-06-05 15:15:18 -04:00
|
|
|
attribute_names_to_assign
|
2018-10-22 22:29:02 -04:00
|
|
|
)
|
2012-07-24 18:50:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def decorated_evaluator
|
2021-06-09 09:11:43 -04:00
|
|
|
Decorator::NewConstructor.new(
|
|
|
|
Decorator::InvocationTracker.new(@evaluator),
|
|
|
|
@build_class
|
2012-07-24 18:50:11 -04:00
|
|
|
)
|
2012-05-18 14:28:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def methods_invoked_on_evaluator
|
2012-06-08 14:18:12 -04:00
|
|
|
method_tracking_evaluator.__invoked_methods__
|
2012-05-18 14:28:51 -04:00
|
|
|
end
|
|
|
|
|
2011-12-02 16:13:36 -05:00
|
|
|
def build_class_instance
|
2012-07-27 14:10:38 -04:00
|
|
|
@build_class_instance ||= method_tracking_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-05-18 14:28:51 -04:00
|
|
|
(attribute_names_to_assign - @attribute_names_assigned - methods_invoked_on_evaluator).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
|
2018-12-12 11:05:13 -05:00
|
|
|
@attribute_names_to_assign ||=
|
|
|
|
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
|
2020-06-05 15:15:18 -04:00
|
|
|
@attribute_list.non_ignored.flat_map { |attribute|
|
2018-10-21 11:39:48 -04:00
|
|
|
override_names.map do |override|
|
2018-12-12 11:05:13 -05:00
|
|
|
attribute.name if ignorable_alias?(attribute, override)
|
2018-10-21 11:39:48 -04:00
|
|
|
end
|
2020-06-05 15:15:18 -04:00
|
|
|
}.compact
|
2011-12-16 12:13:59 -05:00
|
|
|
end
|
2018-12-12 11:05:13 -05:00
|
|
|
|
|
|
|
def ignorable_alias?(attribute, override)
|
|
|
|
attribute.alias_for?(override) &&
|
|
|
|
attribute.name != override &&
|
|
|
|
!ignored_attribute_names.include?(override)
|
|
|
|
end
|
2011-12-02 16:13:36 -05:00
|
|
|
end
|
|
|
|
end
|