Module: ParasutRubySdk::Helpers::DataHelper

Defined in:
lib/parasut_ruby_sdk/helpers/data_helper.rb

Instance Method Summary collapse

Instance Method Details

#convert_to_obj(hash) ⇒ Object

Convert hash to object



7
8
9
10
11
# File 'lib/parasut_ruby_sdk/helpers/data_helper.rb', line 7

def convert_to_obj(hash)
  open_struct =  OpenStruct.new
  set_open_struct_variables( open_struct, hash )
  open_struct
end

#set_open_struct_variables(open_struct, hash) ⇒ Object

Set hash variable to open_struct Recursive method for hash values



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

def set_open_struct_variables(open_struct, hash)
  hash.each do |key,value|
    if value.is_a? Hash
      # create new open struct value
      new_open_struct = OpenStruct.new
      open_struct.send("#{key}=", new_open_struct)
      set_open_struct_variables(new_open_struct, value)
    elsif value.is_a? Array
      open_struct.send("#{key}=", value)
      value.each_with_index do |val, index|
        if val.is_a? Hash
          # create new open struct value
          new_open_struct = OpenStruct.new
          # set array index value variable
          value[index] = new_open_struct
          set_open_struct_variables(new_open_struct, val)
        else
          # Do nothing
          # value[index] = val
        end
      end
    else
      open_struct.send("#{key}=", value)
    end
  end
end