Skip to content

Commit

Permalink
add hash slice using IndifferentAccess hashie#529
Browse files Browse the repository at this point in the history
  • Loading branch information
gnomex committed Oct 1, 2020
1 parent a30327a commit c817f3b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -36,6 +36,7 @@ Any violations of this scheme are considered to be bugs.
### Fixed

* [#516](https://github.com/hashie/hashie/issues/516): Fixed `NoMethodError` raised when including `Hashie::Extensions::Mash::SymbolizeKeys` and `Hashie::Extensions::SymbolizeKeys` in mashes/hashes with non string or symbol keys - [@carolineartz](https://github.com/carolineartz).
* [#](): Fixed [slice doesn't work using symbols](https://github.com/hashie/hashie/issues/529) using hash with `IndifferentAccess` extension - [@gnomex](https://github.com/gnomex)
* Your contribution here.

### Security
Expand Down
5 changes: 5 additions & 0 deletions lib/hashie/extensions/indifferent_access.rb
Expand Up @@ -141,6 +141,11 @@ def merge!(*)
super.convert!
end

def slice(*keys)
string_keys = keys.map { |key| convert_key(key) }
super(*string_keys)
end

protected

def hash_lacking_indifference?(other)
Expand Down
10 changes: 10 additions & 0 deletions spec/hashie/extensions/indifferent_access_spec.rb
Expand Up @@ -315,6 +315,16 @@ def indifferent_writer(key, value)
end
end
end

describe '#slice' do
let(:h) { subject.build(foo: 'bar', baz: 'qux') }

it 'indifferently slices the hash' do
sliced_h = {'foo' => 'bar'}
expect(h.slice('foo')).to eq sliced_h
expect(h.slice(:foo)).to eq sliced_h
end
end
end

describe 'with merge initializer' do
Expand Down

0 comments on commit c817f3b

Please sign in to comment.