Module: DeepHashStruct

Defined in:
lib/deep_hash_struct.rb,
lib/deep_hash_struct/version.rb

Overview

Mixin module for converter.

Constant Summary collapse

VERSION =
"1.0.1"

Instance Method Summary collapse

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

Parameters:

  • input (Hash)

Returns:

  • (Struct)


13
14
15
16
17
18
19
# File 'lib/deep_hash_struct.rb', line 13

def deep_struct(input)
  output = input.clone
  levels = expand_levels(output)
  collapse_levels(levels)
  klass = Struct.new(*output.keys, keyword_init: true)
  klass.new(output)
end