Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow exporting a normal, not-indifferent Hash #536

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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