summaryrefslogtreecommitdiff
path: root/spec/hashie/extensions/indifferent_access_spec.rb
diff options
context:
space:
mode:
authorMichael Herold <opensource@michaeljherold.com>2020-10-22 22:11:34 -0500
committerMichael Herold <opensource@michaeljherold.com>2020-10-22 22:11:34 -0500
commita7d57c921dd9a3e99aff95bcae685f831cade7f9 (patch)
treefef8c25140223fbc9fc052f5c16a8091fdf551ae /spec/hashie/extensions/indifferent_access_spec.rb
parent09581e249729cd7a962b4df47837247c79d4e7ea (diff)
downloadhashie-a7d57c921dd9a3e99aff95bcae685f831cade7f9.tar.gz
Allow exporting a normal, not-indifferent Hash
Following the conventions in `activesupport` and the semantics of the `#to_hash` method in Ruby's standard library, the `#to_hash` method for a hash that has mixed in `IndifferentAccess` will now export the hash as a normal, not-indifferent hash.
Diffstat (limited to 'spec/hashie/extensions/indifferent_access_spec.rb')
-rw-r--r--spec/hashie/extensions/indifferent_access_spec.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/spec/hashie/extensions/indifferent_access_spec.rb b/spec/hashie/extensions/indifferent_access_spec.rb
index c48ad0c..0aec787 100644
--- a/spec/hashie/extensions/indifferent_access_spec.rb
+++ b/spec/hashie/extensions/indifferent_access_spec.rb
@@ -77,6 +77,36 @@ describe Hashie::Extensions::IndifferentAccess do
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) }