Class: Object

Inherits:
BasicObject
Defined in:
lib/hash-serializer/extensions/object.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_column_value(column) ⇒ Object

Retrieves the attribute name



52
53
54
55
56
57
58
# File 'lib/hash-serializer/extensions/object.rb', line 52

def get_column_value(column)
  column = column.to_s.sub(/@/, '')
  unless column.index('_').nil?
    column = column.split('_')[1]
  end
  column
end

Instance Method Details

#serialize_to_hash(root_name = nil) ⇒ Object

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



9
10
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
# File 'lib/hash-serializer/extensions/object.rb', line 9

def serialize_to_hash(root_name = nil)
  result = Hash.new
  self.instance_variables.each do |column|
    aux = self.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[self.class.get_column_value(column)] = aux.to_s
      else
        if aux.is_a?(Array)
          result_aux = Array.new
          aux.each do |elem|
            result_aux << elem.serialize_to_hash
          end
        else
          result_aux = aux.serialize_to_hash
        end
        result[self.class.get_column_value(column)] = result_aux
      end
    end
  end
  unless root_name.nil?
    temp = result
    result = {}
    result[root_name] = temp
  end
  result
end

#serialize_to_json(root_name = nil) ⇒ Object

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



42
43
44
# File 'lib/hash-serializer/extensions/object.rb', line 42

def serialize_to_json(root_name = nil)
  serialize_to_hash(root_name).to_json
end