Class: Factor::Common::DeepStruct

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/common/deep_struct.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ DeepStruct

Returns a new instance of DeepStruct.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/common/deep_struct.rb', line 6

def initialize(hash=nil)
  @table = {}
  @hash_table = {}

  if hash
    hash.each do |k,v|
      @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
      @hash_table[k.to_sym] = v

      new_ostruct_member(k)
    end
  end
end

Instance Method Details

#[](idx) ⇒ Object



32
33
34
35
# File 'lib/common/deep_struct.rb', line 32

def [](idx)
  hash = marshal_dump
  hash[idx.to_sym]
end

#inspectObject



28
29
30
# File 'lib/common/deep_struct.rb', line 28

def inspect
  @hash_table.inspect
end

#to_hObject



20
21
22
# File 'lib/common/deep_struct.rb', line 20

def to_h
  @hash_table
end

#to_sObject



24
25
26
# File 'lib/common/deep_struct.rb', line 24

def to_s
  @hast_table.to_s
end