Module: OpenstructUtils

Defined in:
lib/openstruct_utils.rb,
lib/openstruct_utils/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.openstruct_to_hash(struct) ⇒ Object



11
12
13
14
15
# File 'lib/openstruct_utils.rb', line 11

def self.openstruct_to_hash struct
  hash = struct.marshal_dump
  
  recurisive_transform_openstruct_to_hash hash
end

.openstruct_to_json(struct) ⇒ Object



6
7
8
# File 'lib/openstruct_utils.rb', line 6

def self.openstruct_to_json struct
  JSON.pretty_generate openstruct_to_hash struct
end

.recurisive_transform_openstruct_to_hash(hash) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openstruct_utils.rb', line 17

def self.recurisive_transform_openstruct_to_hash hash
  
 
    hash.each_pair do |key, val|
      if val.instance_of?(Hash)
        hash[key] = recurisive_transform_openstruct_to_hash val
      elsif val.instance_of?(Array)
        hash[key] = val.map do |arr|
            if arr.instance_of?(OpenStruct)
              recurisive_transform_openstruct_to_hash arr.marshal_dump
            elsif arr.instance_of?(Hash)
              recurisive_transform_openstruct_to_hash arr
            else
              arr
            end
        end
      elsif val.instance_of?(OpenStruct)
        hash[key] = recurisive_transform_openstruct_to_hash val.marshal_dump
      end
    end
  
  hash
end