Class: DeepOpenStruct

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/cuba/tools/inflectors.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ DeepOpenStruct

Returns a new instance of DeepOpenStruct.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cuba/tools/inflectors.rb', line 25

def initialize hash = nil

  @table = {}
  @hash_table = {}

  hash = [hash] unless hash.is_a? Hash

  if hash
    hash.each do |k,v|

      if v.is_a? Array
        @table[k.to_sym] ||= []
        @hash_table[k.to_sym] ||= []

        v.each { | entry |
          @table[k.to_sym] << entry
          @hash_table[k.to_sym] << entry
        }
      else
        @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
end

Instance Method Details

#to_hObject



52
53
54
# File 'lib/cuba/tools/inflectors.rb', line 52

def to_h
  @hash_table
end