diff options
author | yuuji.yaginuma <yuuji.yaginuma@gmail.com> | 2017-09-27 13:18:34 +0900 |
---|---|---|
committer | yuuji.yaginuma <yuuji.yaginuma@gmail.com> | 2017-09-27 14:02:12 +0900 |
commit | de9e3238a008f8f20c5beedefb8787b6e51359d0 (patch) | |
tree | f46bd3643758534647907f5529e9e3d89aeee77e /activesupport | |
parent | 48ef5e0ad500bdba706f10b65456f3b03b556212 (diff) | |
download | rails-de9e3238a008f8f20c5beedefb8787b6e51359d0.tar.gz rails-de9e3238a008f8f20c5beedefb8787b6e51359d0.tar.bz2 rails-de9e3238a008f8f20c5beedefb8787b6e51359d0.zip |
Ensure `HashWithIndifferentAccess#transform_keys` to return `HashWithIndifferentAccess`
Currently, `#transform_values`, `#select` and `#reject` return instance
of `HashWithIndifferentAccess`. But `#transform_keys` returns instance
of Hash. This behavior is a bit confusing.
I think that `HashWithIndifferentAccess#transform_keys` should also return
instance of `HashWithIndifferentAccess` as well as other methods.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/hash_with_indifferent_access.rb | 5 | ||||
-rw-r--r-- | activesupport/test/hash_with_indifferent_access_test.rb | 7 |
3 files changed, 16 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 487984cbd3..493ebeb01f 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Return an instance of `HashWithIndifferentAccess` from `HashWithIndifferentAccess#transform_keys`. + + *Yuji Yaginuma* + * Add key rotation support to `MessageEncryptor` and `MessageVerifier` This change introduces a `rotate` method to both the `MessageEncryptor` and diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 12291af443..fcc13feb8c 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -306,6 +306,11 @@ module ActiveSupport dup.tap { |hash| hash.transform_values!(*args, &block) } end + def transform_keys(*args, &block) + return to_enum(:transform_keys) unless block_given? + dup.tap { |hash| hash.transform_keys!(*args, &block) } + end + def compact dup.tap(&:compact!) end diff --git a/activesupport/test/hash_with_indifferent_access_test.rb b/activesupport/test/hash_with_indifferent_access_test.rb index b3788ee65c..b878ac20fa 100644 --- a/activesupport/test/hash_with_indifferent_access_test.rb +++ b/activesupport/test/hash_with_indifferent_access_test.rb @@ -399,6 +399,13 @@ class HashWithIndifferentAccessTest < ActiveSupport::TestCase assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings end + def test_indifferent_transform_keys + hash = ActiveSupport::HashWithIndifferentAccess.new(@strings).transform_keys { |k| k * 2 } + + assert_equal({ "aa" => 1, "bb" => 2 }, hash) + assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash + end + def test_indifferent_compact hash_contain_nil_value = @strings.merge("z" => nil) hash = ActiveSupport::HashWithIndifferentAccess.new(hash_contain_nil_value) |