Skip to content

Commit

Permalink
Merge pull request hashie#195 from michaelherold/indifferent_access_w…
Browse files Browse the repository at this point in the history
…eirdness

Fix hashie#178 and hashie#180 by injecting in place
  • Loading branch information
dblock committed Aug 18, 2014
2 parents ae4cab2 + 1971100 commit 1c4fee0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 3.2.1 (Next)

* [#183](https://github.com/intridea/hashie/pull/183): Added Mash#load with YAML file support - [@gregory](https://github.com/gregory).
* [#195](https://github.com/intridea/hashie/pull/195): Ensure that the same object is returned after injecting IndifferentAccess - [@michaelherold](https://github.com/michaelherold).
* [#197](https://github.com/intridea/hashie/pull/197): Dont convert keys to string on initalization of mash - [@gregory](https://github.com/gregory).
* [#201](https://github.com/intridea/hashie/pull/201): Hashie::Trash transforms can be inherited - [@fobocaster](https://github.com/fobocaster).
* [#189](https://github.com/intridea/hashie/pull/189): Added Rash#fetch - [@medcat](https://github.com/medcat).
Expand Down
4 changes: 2 additions & 2 deletions lib/hashie/extensions/indifferent_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def convert!

def convert_value(value)
if hash_lacking_indifference?(value)
IndifferentAccess.inject(value.dup)
IndifferentAccess.inject!(value)
elsif value.is_a?(::Array)
value.dup.replace(value.map { |e| convert_value(e) })
value.replace(value.map { |e| convert_value(e) })
else
value
end
Expand Down
48 changes: 48 additions & 0 deletions spec/hashie/extensions/indifferent_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ class IndifferentHashWithDash < Hashie::Dash
h = subject.build(:foo => 'bar', 'baz' => 'qux')
expect(h.values_at('foo', :baz)).to eq %w(bar qux)
end

it 'returns the same instance of the hash that was set' do
hash = Hash.new
h = subject.build(foo: hash)
expect(h.values_at(:foo)[0]).to be(hash)
end

it 'returns the same instance of the array that was set' do
array = Array.new
h = subject.build(foo: array)
expect(h.values_at(:foo)[0]).to be(array)
end

it 'returns the same instance of the string that was set' do
str = 'my string'
h = subject.build(foo: str)
expect(h.values_at(:foo)[0]).to be(str)
end

it 'returns the same instance of the object that was set' do
object = Object.new
h = subject.build(foo: object)
expect(h.values_at(:foo)[0]).to be(object)
end
end

describe '#fetch' do
Expand All @@ -60,6 +84,30 @@ class IndifferentHashWithDash < Hashie::Dash
expect(h.fetch(:foo)).to eq h.fetch('foo')
expect(h.fetch(:foo)).to eq 'bar'
end

it 'returns the same instance of the hash that was set' do
hash = Hash.new
h = subject.build(foo: hash)
expect(h.fetch(:foo)).to be(hash)
end

it 'returns the same instance of the array that was set' do
array = Array.new
h = subject.build(foo: array)
expect(h.fetch(:foo)).to be(array)
end

it 'returns the same instance of the string that was set' do
str = 'my string'
h = subject.build(foo: str)
expect(h.fetch(:foo)).to be(str)
end

it 'returns the same instance of the object that was set' do
object = Object.new
h = subject.build(foo: object)
expect(h.fetch(:foo)).to be(object)
end
end

describe '#delete' do
Expand Down

0 comments on commit 1c4fee0

Please sign in to comment.