From f5ae5add05455ca36e8bd9d54a675a8b9e82e26e Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Wed, 11 Oct 2023 16:33:13 +0800 Subject: [PATCH] ! Revert `Make code "Object Shape friendly"` Fix issue happening on production environment --- lib/contracted_value/core.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/contracted_value/core.rb b/lib/contracted_value/core.rb index ef03920..7b62c2d 100644 --- a/lib/contracted_value/core.rb +++ b/lib/contracted_value/core.rb @@ -238,8 +238,6 @@ def initialize(input_attr_values = {}) ) end - @attr_values = {} - self.class.send(:attribute_set).each_attribute do |attribute| attr_value = attribute.extract_value(input_attr_values_hash) @@ -262,8 +260,8 @@ def initialize(input_attr_values = {}) # Using symbol since attribute names are limited in number # An alternative would be using frozen string - @attr_values.store( - attribute.name.to_sym, + instance_variable_set( + :"@#{attribute.name}", sometimes_frozen_attr_value, ) end @@ -274,7 +272,10 @@ def initialize(input_attr_values = {}) # rubocop:enable Metrics/CyclomaticComplexity def to_h - @attr_values.clone + self.class.send(:attribute_set). + each_attribute.each_with_object({}) do |attribute, hash| + hash[attribute.name] = instance_variable_get(:"@#{attribute.name}") + end end # == Class interface == # @@ -306,7 +307,7 @@ def attribute( ) @attribute_set = @attribute_set.add(attr) - define_method(name_in_sym) { @attr_values[name_in_sym] } + attr_reader(name_in_sym) end # @api private