Skip to content

Commit

Permalink
support for separate load hash extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Aug 5, 2020
1 parent 7bd171c commit 076561f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/serialized_hashie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def dump_extensions
@dump_extensions ||= Extensions.new
end

def load_hash_extensions
@load_hash_extensions ||= Extensions.new
end

end

end
6 changes: 5 additions & 1 deletion lib/serialized_hashie/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def load_hash(hash)
end

def load_value(value)
return load_hash(value) if value.is_a?(::Hash)
if value.is_a?(::Hash)
hash = SerializedHashie.load_hash_extensions.run(value)
return load_hash(hash)
end

return value.map { |v| load_value(v) } if value.is_a?(Array)

SerializedHashie.load_extensions.run(value)
Expand Down
8 changes: 8 additions & 0 deletions spec/serialized_hashie/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,13 @@
expect(hash).to be_a Hashie::Mash
expect(hash).to eq({ 'hello' => 'world', 'nested' => { 'vegetables' => [{ 'name' => 'potato' }, { 'name' => 'cucumber' }], 'more_nesting' => { 'fruits' => [{ 'name' => 'banana' }, { 'name' => 'apple' }] } } })
end

it 'should pass through hashes through their own extensions' do
SerializedHashie.load_hash_extensions.add(:test) { |hash| hash.transform_keys(&:upcase) }
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' => { 'NAME' => 'Michael' } })
end
end
end

0 comments on commit 076561f

Please sign in to comment.