Skip to content

Commit

Permalink
Merge pull request #536 from michaelherold/export-normal-hash-from-in…
Browse files Browse the repository at this point in the history
…different

Allow exporting a normal, not-indifferent Hash
  • Loading branch information
dblock committed Oct 23, 2020
2 parents 09581e2 + a7d57c9 commit b741e5d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@ Any violations of this scheme are considered to be bugs.
* [#523](https://github.com/hashie/hashie/pull/523): Added TOC, ensure a keep-a-changelog formatted CHANGELOG - [@dblock](https://github.com/dblock).
* [#522](https://github.com/hashie/hashie/pull/522): Added eierlegende Wollmilchsau mascot graphic - [@carolineartz](https://github.com/carolineartz).
* [#530](https://github.com/hashie/hashie/pull/530): Added Hashie::Extensions::Dash::PredefinedValues - [@caalberts](https://github.com/caalberts).
* [#536](https://github.com/hashie/hashie/pull/536): Added exporting a normal Hash from an indifferent one through the `#to_hash` method - [@michaelherold](https://github.com/michaelherold).
* Your contribution here.

### Changed
Expand Down
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -333,6 +333,18 @@ myhash['fishes'][:food] = 'flakes'
myhash['fishes']['food'] # => "flakes"
```

To get back a normal, not-indifferent Hash, you can use `#to_hash` on the indifferent hash. It exports the keys as strings, not symbols:

```ruby
myhash = MyHash.new
myhash["foo"] = "bar"
myhash[:foo] #=> "bar"

normal_hash = myhash.to_hash
myhash["foo"] #=> "bar"
myhash[:foo] #=> nil
```

### IgnoreUndeclared

This extension can be mixed in to silently ignore undeclared properties on initialization instead of raising an error. This is useful when using a Trash to capture a subset of a larger hash.
Expand Down
12 changes: 12 additions & 0 deletions lib/hashie/extensions/indifferent_access.rb
Expand Up @@ -143,6 +143,18 @@ def merge!(*)
super.convert!
end

def to_hash
{}.tap do |result|
each_pair { |key, value| result[key] = value }

if default_proc
result.default_proc = default_proc
else
result.default = default
end
end
end

with_minimum_ruby('2.5.0') do
def slice(*keys)
string_keys = keys.map { |key| convert_key(key) }
Expand Down
30 changes: 30 additions & 0 deletions spec/hashie/extensions/indifferent_access_spec.rb
Expand Up @@ -77,6 +77,36 @@ class IndifferentHashWithIgnoreUndeclaredAndPropertyTranslation < Hashie::Dash
end
end

describe '#to_hash' do
let(:indifferent_hash) { Class.new(::Hash) { include Hashie::Extensions::IndifferentAccess } }

it 'returns a normal hash without indifference' do
indifferent = indifferent_hash.new
indifferent['cat'] = 'meow'

subject = indifferent.to_hash

expect(subject['cat']).to eq 'meow'
expect(subject[:cat]).to be_nil
end

it 'maintains the #default_proc when set' do
indifferent = indifferent_hash.new { |_hash, key| "Nothing here: #{key}" }

subject = indifferent.to_hash

expect(subject['babble']).to eq 'Nothing here: babble'
end

it 'maintains the #default when set' do
indifferent = indifferent_hash.new(0)

subject = indifferent.to_hash

expect(subject['babble']).to eq 0
end
end

describe 'when included in dash' do
let(:params) { { foo: 'bar' } }
subject { IndifferentHashWithDash.new(params) }
Expand Down

0 comments on commit b741e5d

Please sign in to comment.