aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Eskildsen <sirup@sirupsen.com>2015-07-10 03:10:45 +0000
committerSimon Eskildsen <sirup@sirupsen.com>2015-07-10 15:54:31 +0000
commit4ff255a6907cfa01e29fdc9b0f409082f3ee4259 (patch)
tree224f69b8b3fccb13ea4c00701be996238d80c7d7
parent776e5991a9abf11818cb803e80976cea8b454c17 (diff)
downloadrails-4ff255a6907cfa01e29fdc9b0f409082f3ee4259.tar.gz
rails-4ff255a6907cfa01e29fdc9b0f409082f3ee4259.tar.bz2
rails-4ff255a6907cfa01e29fdc9b0f409082f3ee4259.zip
active_support/indifferent_hash: fix cloning default_proc on dup
-rw-r--r--activesupport/CHANGELOG.md4
-rw-r--r--activesupport/lib/active_support/hash_with_indifferent_access.rb10
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb17
3 files changed, 30 insertions, 1 deletions
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