class Phobos::DeepStruct

Public Class Methods

new(hash = nil) click to toggle source

Based on docs.omniref.com/ruby/2.3.0/files/lib/ostruct.rb#line=88

Calls superclass method
# File lib/phobos/deep_struct.rb, line 10
def initialize(hash = nil)
  super
  @hash_table = {}

  hash&.each_pair do |key, value|
    key = key.to_sym
    @table[key] = to_deep_struct(value)
    @hash_table[key] = value
  end
end

Public Instance Methods

to_h() click to toggle source
# File lib/phobos/deep_struct.rb, line 21
def to_h
  @hash_table.dup
end
Also aliased as: to_hash
to_hash()
Alias for: to_h

Private Instance Methods

to_deep_struct(value) click to toggle source
# File lib/phobos/deep_struct.rb, line 28
def to_deep_struct(value)
  case value
  when Hash
    self.class.new(value)
  when Enumerable
    value.map { |el| to_deep_struct(el) }
  else
    value
  end
end