Merge pull request #525 from yogeshjain999/indifferent-convert-change

Small amendments for Hash#merge with IndifferentAccess
This commit is contained in:
Daniel Doubrovkine (dB.) @dblockdotorg 2020-06-10 09:40:10 -04:00 committed by dblock
commit 1f1efdb14c
3 changed files with 42 additions and 2 deletions

View File

@ -20,6 +20,7 @@ Any violations of this scheme are considered to be bugs.
* [#521](https://github.com/hashie/hashie/pull/499): Do not convert keys that cannot be represented as symbols to `String` in `Mash` initialization - [@carolineartz](https://github.com/carolineartz).
* [#524](https://github.com/hashie/hashie/pull/524): Test with Ruby 2.7 - [@aried3r](https://github.com/aried3r).
* [#525](https://github.com/hashie/hashie/pull/525): Use `indifferent_writer` in `IndifferentAccess#convert!` - [@yogeshjain999](https://github.com/yogeshjain999).
* Your contribution here.
### Deprecated

View File

@ -74,7 +74,7 @@ module Hashie
# is injecting itself into member hashes.
def convert!
keys.each do |k| # rubocop:disable Performance/HashEachMethods
regular_writer convert_key(k), indifferent_value(regular_delete(k))
indifferent_writer k, regular_delete(k)
end
self
end
@ -133,7 +133,7 @@ module Hashie
def merge(*args)
result = super
IndifferentAccess.inject!(result) if hash_lacking_indifference?(result)
return IndifferentAccess.inject!(result) if hash_lacking_indifference?(result)
result.convert!
end

View File

@ -86,6 +86,45 @@ describe Hashie::Extensions::IndifferentAccess do
end
end
describe 'when overriding indifferent methods' do
let(:indifferent_hash) do
Class.new(::Hash) do
include Hashie::Extensions::IndifferentAccess
ALIASES = { cat: :grumpy }.freeze
# Override writer to maintain alias of the given key
def indifferent_writer(key, value)
indifferent_value = indifferent_value(value)
regular_writer convert_key(key), indifferent_value
regular_writer convert_key(ALIASES[key]), indifferent_value
end
alias_method :[]=, :indifferent_writer
end.new
end
it '#indifferent_writer' do
indifferent_hash[:cat] = 'meow'
expect(indifferent_hash[:cat]).to eq('meow')
expect(indifferent_hash['cat']).to eq('meow')
expect(indifferent_hash[:grumpy]).to eq('meow')
expect(indifferent_hash['grumpy']).to eq('meow')
end
it '#merge' do
merged_hash = indifferent_hash.merge(cat: 'meow')
expect(merged_hash[:cat]).to eq('meow')
expect(merged_hash['cat']).to eq('meow')
expect(merged_hash[:grumpy]).to eq('meow')
expect(merged_hash['grumpy']).to eq('meow')
end
end
describe 'when translating properties and ignoring undeclared' do
let(:value) { 'baz' }