Ensure IndifferentAccess is injected after merge

During non-destructive merges (i.e. `#merge`), the `IndifferentAccess` mixin was
failing to inject itself into the resulting Hash. This caused a `NoMethodError`
to be thrown when attempting to perform the action.

Now, we properly inject the mixin when necessary so the `#merge` method works.
This commit is contained in:
Michael Herold 2018-02-04 16:39:20 -06:00
parent a9d0e0ac7b
commit a82c594710
No known key found for this signature in database
GPG Key ID: 0325A44E1EA06F99
4 changed files with 25 additions and 4 deletions

View File

@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-02-24 07:11:40 -0600 using RuboCop version 0.34.2.
# on 2018-02-04 16:33:11 -0600 using RuboCop version 0.34.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
@ -34,6 +34,11 @@ Metrics/LineLength:
Metrics/MethodLength:
Max: 28
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 102
# Offense count: 6
Metrics/PerceivedComplexity:
Max: 10
@ -48,7 +53,7 @@ Style/CaseEquality:
Style/Documentation:
Enabled: false
# Offense count: 11
# Offense count: 10
Style/DoubleNegation:
Exclude:
- 'lib/hashie/dash.rb'

View File

@ -29,6 +29,7 @@ scheme are considered to be bugs.
### Fixed
* [#435](https://github.com/intridea/hashie/pull/435): Mash `default_proc`s are now propagated down to nested sub-Hashes - [@michaelherold](https://github.com/michaelherold).
* [#436](https://github.com/intridea/hashie/pull/436): Ensure that `Hashie::Extensions::IndifferentAccess` injects itself after a non-destructive merge - [@michaelherold](https://github.com/michaelherold).
* Your contribution here.
### Security

View File

@ -133,8 +133,10 @@ module Hashie
self
end
def merge(*)
super.convert!
def merge(*args)
result = super
IndifferentAccess.inject!(result) if hash_lacking_indifference?(result)
result.convert!
end
def merge!(*)

View File

@ -49,6 +49,19 @@ describe Hashie::Extensions::IndifferentAccess do
expect(merged_hash[:cat]).to eq('meow')
expect(merged_hash['cat']).to eq('meow')
end
it 'injects the resulting new Hash with IndifferentAccess' do
hash = IndifferentHashWithMergeInitializer.new(
:cat => 'meow',
'dog' => { name: 'Mango', sound: 'woof' }
)
dog = hash[:dog]
merged = dog.merge(foo: 'bar')
expect(merged[:foo]).to eq('bar')
expect(merged['foo']).to eq('bar')
end
end
describe '#merge!' do