Module: DeepStruct
- Defined in:
- lib/deep_struct.rb,
lib/deep_struct/version.rb
Overview
Mixin module for converter.
Constant Summary collapse
- VERSION =
"0.1.7"
Instance Method Summary collapse
-
#deep_struct(input) ⇒ Struct
Converts a Hash to a Struct.
Instance Method Details
#deep_struct(input) ⇒ Struct
Converts a Hash to a Struct. If the Hash has nested Hash objects, they are converted to Struct as well.
output = deep_struct({x: 1, y: {z: 2}})
output.x #=> 1
output.y.z #=> 2
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/deep_struct.rb', line 13 def deep_struct(input) output = input.clone levels = output.keys.map { |key| [key, output] } levels.each do |key, context| if context[key].is_a?(Hash) # overwrite the shared reference with input by a new reference context[key] = context[key].clone subset = context[key] subset.each { |subkey, subvalue| levels.push([subkey, subset]) if subvalue.is_a?(Hash) } end end levels.reverse_each do |k, context| if context[k].is_a?(Hash) klass = Struct.new(*context[k].keys, keyword_init: true) context[k] = klass.new(context[k]) end end klass = Struct.new(*output.keys, keyword_init: true) klass.new(output) end |