From 514c4b4d5319f10a275813f1af5a21ed3f41263d Mon Sep 17 00:00:00 2001 From: lulalala Date: Sat, 19 Jan 2019 09:53:25 +0800 Subject: Freeze DeprecationHandling array and hash --- activemodel/lib/active_model/errors.rb | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 4b8b601b4b..81a65a60a7 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -151,7 +151,7 @@ module ActiveModel keys = keys.map(&:to_sym) - results = messages.slice!(*keys) + results = messages.dup.slice!(*keys) @errors.keep_if do |error| keys.include?(error.attribute) @@ -625,16 +625,7 @@ module ActiveModel class DeprecationHandlingMessageHash < SimpleDelegator def initialize(errors) @errors = errors - - content = @errors.to_hash - content.each do |attribute, value| - content[attribute] = DeprecationHandlingMessageArray.new(value, @errors, attribute) - end - content.default_proc = proc do |hash, attribute| - hash[attribute] = DeprecationHandlingMessageArray.new([], @errors, attribute) - end - - super(content) + super(prepare_content) end def []=(attribute, value) @@ -644,15 +635,31 @@ module ActiveModel @errors.add(attribute, message) end - super(attribute, DeprecationHandlingMessageArray.new(@errors.messages_for(attribute), @errors, attribute)) + __setobj__ prepare_content end + + private + + def prepare_content + content = @errors.to_hash + content.each do |attribute, value| + content[attribute] = DeprecationHandlingMessageArray.new(value, @errors, attribute) + end + content.default_proc = proc do |hash, attribute| + hash = hash.dup + hash[attribute] = DeprecationHandlingMessageArray.new([], @errors, attribute) + __setobj__ hash.freeze + hash[attribute] + end + content.freeze + end end class DeprecationHandlingMessageArray < SimpleDelegator def initialize(content, errors, attribute) @errors = errors @attribute = attribute - super(content) + super(content.freeze) end def <<(message) -- cgit v1.2.3