From 5090a64aa3b9c08339c7aa370bfd6c08557ff8e7 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 14 Jun 2020 13:50:07 +0900 Subject: [PATCH] Lazy allocate `@forced_changes` It is almost no longer used in Active Record. --- .../lib/active_model/attribute_mutation_tracker.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/activemodel/lib/active_model/attribute_mutation_tracker.rb b/activemodel/lib/active_model/attribute_mutation_tracker.rb index d8cd48a53b..e46d2e76da 100644 --- a/activemodel/lib/active_model/attribute_mutation_tracker.rb +++ b/activemodel/lib/active_model/attribute_mutation_tracker.rb @@ -7,9 +7,8 @@ module ActiveModel class AttributeMutationTracker # :nodoc: OPTION_NOT_GIVEN = Object.new - def initialize(attributes, forced_changes = Set.new) + def initialize(attributes) @attributes = attributes - @forced_changes = forced_changes end def changed_attribute_names @@ -62,11 +61,15 @@ module ActiveModel end def force_change(attr_name) - forced_changes << attr_name + forced_changes[attr_name] = fetch_value(attr_name) end private - attr_reader :attributes, :forced_changes + attr_reader :attributes + + def forced_changes + @forced_changes ||= {} + end def attr_names attributes.keys @@ -82,7 +85,7 @@ module ActiveModel end class ForcedMutationTracker < AttributeMutationTracker # :nodoc: - def initialize(attributes, forced_changes = {}) + def initialize(attributes) super @finalized_changes = nil end