Class: NestedStruct

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/util/nested_struct.rb

Overview

Public: turns JSON into a deeply nested OpenStruct. Will find Hashes with Hashes and within Arrays.

Instance Method Summary collapse

Constructor Details

#initialize(json = Hash.new) ⇒ NestedStruct

Returns a new instance of NestedStruct.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/util/nested_struct.rb', line 4

def initialize(json=Hash.new)
  @table = {}

  json.each do |key, value|
    @table[key.to_sym] =  if value.is_a? Hash
                            self.class.new(value)
                          elsif value.is_a? Array
                            # Turn all the jsons in the array into DeepStructs
                            value.collect do |i|
                              i.is_a?(Hash) ? self.class.new(i) : i
                            end
                          else
                            value
                          end

    new_ostruct_member(key)
  end
end

Instance Method Details

#as_json(*args) ⇒ Object



23
24
25
26
# File 'lib/util/nested_struct.rb', line 23

def as_json(*args)
  json = super
  json["table"]
end