Skip to content

Commit

Permalink
Fix issue #100, hashify nested types within hash maps
Browse files Browse the repository at this point in the history
Co-authored-by: Brendan Weibrecht <brendan@weibrecht.net.au>
  • Loading branch information
oeoeaio and ZimbiX committed Sep 20, 2018
1 parent b2c4150 commit aadf2a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dry/struct/hashify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Hashify
# @return [Hash, Array]
def self.[](value)
if value.respond_to?(:to_hash)
value.to_hash
Hash[value.to_hash.map{ |k, item| [k, self[item]] }]
elsif value.respond_to?(:to_ary)
value.to_ary.map { |item| self[item] }
else
Expand Down
15 changes: 15 additions & 0 deletions spec/dry/struct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,21 @@ def obj.map
expect(type.new.to_hash).to eq (attributes)
end
end

context 'on an Dry::Types::Hash.map with nested types' do
NestedType = Class.new(Dry::Struct::Value) do
attribute :age, Dry::Types['strict.integer']
end

type = Class.new(Dry::Struct) do
attribute :people, Dry::Types['hash'].map(Dry::Types['strict.string'], NestedType)
end

it 'hashifies the values within the hash map' do
attributes = { people: { 'John' => { age: 35 } } }
expect(type.new(attributes).to_hash).to eq(attributes)
end
end
end

describe 'pseudonamed structs' do
Expand Down

0 comments on commit aadf2a7

Please sign in to comment.