summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Herold <michael.j.herold@gmail.com>2016-01-31 17:02:03 -0600
committerMichael Herold <michael.j.herold@gmail.com>2016-01-31 17:02:03 -0600
commitf722ee310f1517392c91677c9b4fcec92e8dbc36 (patch)
tree41e78af0443e370da364ae72439c88e4f2837037
parentb458e728f545539c8b83383c9c727c821f36274e (diff)
downloadhashie-f722ee310f1517392c91677c9b4fcec92e8dbc36.tar.gz
Fix `#merge` breaking indifferent access
`HashWithDifferentAccess` wasn't properly setting the indifferent access when merging in other hashes. This reruns `#convert!` after calling the superclass implementation of the method, so it preserves indifferent access on the resulting object. Fixes #345
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/hashie/extensions/indifferent_access.rb8
-rw-r--r--spec/hashie/extensions/indifferent_access_spec.rb26
3 files changed, 35 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 205202e..a1de520 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@
* [#319](https://github.com/intridea/hashie/pull/319): Fix a regression from 3.4.1 where `Hashie::Extensions::DeepFind` is no longer indifference-aware - [@michaelherold](https://github.com/michaelherold).
* [#240](https://github.com/intridea/hashie/pull/240): Fixed nesting twice with Clash keys - [@bartoszkopinski](https://github.com/bartoszkopinski).
* [#322](https://github.com/intridea/hashie/pull/322): Fixed `reverse_merge` issue with `Mash` subclasses - [@marshall-lee](https://github.com/marshall-lee).
+* [#346](https://github.com/intridea/hashie/pull/346): Fixed `merge` breaking indifferent access - [@docwhat](https://github.com/docwhat), [@michaelherold](https://github.com/michaelherold).
* Your contribution here.
## 3.4.3 (10/25/2015)
diff --git a/lib/hashie/extensions/indifferent_access.rb b/lib/hashie/extensions/indifferent_access.rb
index 3bbb93d..55648a6 100644
--- a/lib/hashie/extensions/indifferent_access.rb
+++ b/lib/hashie/extensions/indifferent_access.rb
@@ -133,6 +133,14 @@ module Hashie
self
end
+ def merge(*)
+ super.convert!
+ end
+
+ def merge!(*)
+ super.convert!
+ end
+
protected
def hash_lacking_indifference?(other)
diff --git a/spec/hashie/extensions/indifferent_access_spec.rb b/spec/hashie/extensions/indifferent_access_spec.rb
index dfdbcae..294c0b4 100644
--- a/spec/hashie/extensions/indifferent_access_spec.rb
+++ b/spec/hashie/extensions/indifferent_access_spec.rb
@@ -31,6 +31,32 @@ describe Hashie::Extensions::IndifferentAccess do
property :foo
end
+ describe '#merge' do
+ it 'indifferently merges in a hash' do
+ indifferent_hash = Class.new(::Hash) do
+ include Hashie::Extensions::IndifferentAccess
+ end.new
+
+ merged_hash = indifferent_hash.merge(:cat => 'meow')
+
+ expect(merged_hash[:cat]).to eq('meow')
+ expect(merged_hash['cat']).to eq('meow')
+ end
+ end
+
+ describe '#merge!' do
+ it 'indifferently merges in a hash' do
+ indifferent_hash = Class.new(::Hash) do
+ include Hashie::Extensions::IndifferentAccess
+ end.new
+
+ indifferent_hash.merge!(:cat => 'meow')
+
+ expect(indifferent_hash[:cat]).to eq('meow')
+ expect(indifferent_hash['cat']).to eq('meow')
+ end
+ end
+
describe 'when included in dash' do
let(:params) { { foo: 'bar' } }
subject { IndifferentHashWithDash.new(params) }