Skip to content

Commit

Permalink
support for load hash extensions to return non-hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Aug 5, 2020
1 parent 076561f commit 2820d1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/serialized_hashie/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ def load_hash(hash)
def load_value(value)
if value.is_a?(::Hash)
hash = SerializedHashie.load_hash_extensions.run(value)
return load_hash(hash)

# If the result is still a hash, we'll return that here
return load_hash(hash) if hash.is_a?(::Hash)

# If the result is not a hash, we'll just return whatever
# was returned as a normal value.
return load_value(hash)
end

return value.map { |v| load_value(v) } if value.is_a?(Array)
Expand Down
13 changes: 12 additions & 1 deletion spec/serialized_hashie/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
end

context '.load' do
after(:each) { SerializedHashie.load_extensions.reset }
after(:each) do
SerializedHashie.load_extensions.reset
SerializedHashie.load_hash_extensions.reset
end

it 'should create a Hashie::Mash from the given JSON' do
hash = described_class.load('{"hello":"world"}')
Expand Down Expand Up @@ -106,5 +109,13 @@
expect(hash).to be_a Hashie::Mash
expect(hash).to eq({ 'some_hash' => { 'NAME' => 'Michael' } })
end

it 'should pass hashses through their own extension and return non-hash values properly' do
SerializedHashie.load_hash_extensions.add(:test) { |hash| hash.key?('name') ? hash['name'] : hash }
hash = described_class.load('{"some_hash":{"name":"Michael"}}')
expect(hash).to be_a SerializedHashie::Hash
expect(hash).to be_a Hashie::Mash
expect(hash).to eq({ 'some_hash' => 'Michael' })
end
end
end

0 comments on commit 2820d1a

Please sign in to comment.