Class: DeepStruct

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

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ DeepStruct

Returns a new instance of DeepStruct.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ext/deep_struct.rb', line 4

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

  if hash
    hash.each do |key, value|
      @table[key.to_sym] = process_value(value)
      @hash_table[key.to_sym] = value

      new_ostruct_member(key)
    end
  end
end

Instance Method Details

#process_value(value) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/ext/deep_struct.rb', line 18

def process_value(value)
  if value.is_a?(Hash)
    self.class.new(value)
  elsif value.is_a?(Array)
    value.map { |element| process_value(element) }
  else
    value
  end
end