From 4ff255a6907cfa01e29fdc9b0f409082f3ee4259 Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Fri, 10 Jul 2015 03:10:45 +0000 Subject: active_support/indifferent_hash: fix cloning default_proc on dup --- activesupport/CHANGELOG.md | 4 ++++ .../lib/active_support/hash_with_indifferent_access.rb | 10 +++++++++- activesupport/test/core_ext/hash_ext_test.rb | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 6ebbdbc3db..e2eb539353 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Fix setting `default_proc` on `HashWithIndifferentAccess#dup` + + *Simon Eskildsen* + * Fix a range of values for parameters of the Time#change *Nikolay Kondratyev* diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 4f71f13971..bf34a4ef32 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -188,7 +188,7 @@ module ActiveSupport # dup[:a][:c] # => "c" def dup self.class.new(self).tap do |new_hash| - new_hash.default = default + set_defaults(new_hash) end end @@ -275,6 +275,14 @@ module ActiveSupport value end end + + def set_defaults(target) + if default_proc + target.default_proc = default_proc.dup + else + target.default = default + end + end end end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index e10bee5e00..137a03873e 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -998,6 +998,23 @@ class HashExtTest < ActiveSupport::TestCase assert hash.key?('a') assert_equal 1, hash[:a] end + + def test_dup_with_default_proc + hash = HashWithIndifferentAccess.new + hash.default_proc = proc { |h, v| raise "walrus" } + assert_nothing_raised { hash.dup } + end + + def test_dup_with_default_proc_sets_proc + hash = HashWithIndifferentAccess.new + hash.default_proc = proc { |h, k| k + 1 } + new_hash = hash.dup + + assert_equal 3, new_hash[2] + + new_hash.default = 2 + assert_equal 2, new_hash[:non_existant] + end end class IWriteMyOwnXML -- cgit v1.2.3 From fa30dd6d2e3151ac2be16673d40cb458a76f1dbe Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Fri, 10 Jul 2015 03:11:37 +0000 Subject: active_support/indifferent_hash: dont raise on to_hash when default_proc raises --- activesupport/CHANGELOG.md | 5 +++++ .../lib/active_support/hash_with_indifferent_access.rb | 4 +++- activesupport/test/core_ext/hash_ext_test.rb | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index e2eb539353..4ffaa666b9 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* Fix not calling `#default` on `HashWithIndifferentAcess#to_hash` when only + `default_proc` is set, which could raise. + + *Simon Eskildsen* + * Fix setting `default_proc` on `HashWithIndifferentAccess#dup` *Simon Eskildsen* diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index bf34a4ef32..63690a1342 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -247,7 +247,9 @@ module ActiveSupport # Convert to a regular hash with string keys. def to_hash - _new_hash = Hash.new(default) + _new_hash = Hash.new + set_defaults(_new_hash) + each do |key, value| _new_hash[key] = convert_value(value, for: :to_hash) end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 137a03873e..6f66d70217 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -1015,6 +1015,20 @@ class HashExtTest < ActiveSupport::TestCase new_hash.default = 2 assert_equal 2, new_hash[:non_existant] end + + def test_to_hash_with_raising_default_proc + hash = HashWithIndifferentAccess.new + hash.default_proc = proc { |h, k| raise "walrus" } + + assert_nothing_raised { hash.to_hash } + end + + def test_new_from_hash_copying_default_should_not_raise_when_default_proc_does + hash = Hash.new + hash.default_proc = proc { |h, k| raise "walrus" } + + assert_nothing_raised { HashWithIndifferentAccess.new_from_hash_copying_default(hash) } + end end class IWriteMyOwnXML -- cgit v1.2.3 From 9578d574f3be8da966ee7355dfb1604936a103cb Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Fri, 10 Jul 2015 15:29:23 +0000 Subject: test/hash: move lonely indifferent hash test --- activesupport/test/core_ext/hash_ext_test.rb | 6 ++++++ activesupport/test/hash_with_indifferent_access_test.rb | 11 ----------- 2 files changed, 6 insertions(+), 11 deletions(-) delete mode 100644 activesupport/test/hash_with_indifferent_access_test.rb (limited to 'activesupport') diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 6f66d70217..cae7b85f11 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -523,6 +523,12 @@ class HashExtTest < ActiveSupport::TestCase assert_equal 5, merged[:b] end + def test_reverse_merge + hash = HashWithIndifferentAccess.new key: :old_value + hash.reverse_merge! key: :new_value + assert_equal :old_value, hash[:key] + end + def test_indifferent_reverse_merging hash = HashWithIndifferentAccess.new('some' => 'value', 'other' => 'value') hash.reverse_merge!(:some => 'noclobber', :another => 'clobber') diff --git a/activesupport/test/hash_with_indifferent_access_test.rb b/activesupport/test/hash_with_indifferent_access_test.rb deleted file mode 100644 index 1facd691fa..0000000000 --- a/activesupport/test/hash_with_indifferent_access_test.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'abstract_unit' -require 'active_support/hash_with_indifferent_access' - -class HashWithIndifferentAccessTest < ActiveSupport::TestCase - def test_reverse_merge - hash = HashWithIndifferentAccess.new key: :old_value - hash.reverse_merge! key: :new_value - assert_equal :old_value, hash[:key] - end - -end -- cgit v1.2.3