Module: HashSerializer

Defined in:
lib/hash-serializer.rb,
lib/hash-serializer/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.to_hash(obj, root_name = nil) ⇒ Object

Instance method to parse an object to an equivalent Hash format Used to serialize body data



11
12
13
14
15
16
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/hash-serializer.rb', line 11

def to_hash(obj, root_name = nil)
  result = Hash.new
  obj.instance_variables.each do |column|
    aux = obj.instance_variable_get(column)
    unless aux.nil?
      if aux.instance_of?(String) or aux.kind_of?(String)
        # It's required to erase any '_' character because
        # soap4r has included it (not idea about the reason)
        result[get_column_value(column)] = aux.to_s
      else
        if aux.is_a?(Array)
          result_aux = Array.new
          aux.each do |elem|
            result_aux << HashSerializer.to_hash(elem)
          end
        else
          result_aux = HashSerializer.to_hash(aux)
        end
        result[get_column_value(column)] = result_aux
      end
    end
  end
  unless root_name.nil?
    temp = result
    result = {}
    result[root_name] = temp
  end
  result
end

.to_json(obj, root_name = nil) ⇒ Object

Convert the Hashed object to a String containing the object JSON format



44
45
46
# File 'lib/hash-serializer.rb', line 44

def to_json(obj, root_name = nil)
  to_hash(obj, root_name).to_json
end