Module: SerialisableHelpers

Extended by:
SerialisableHelpers
Included in:
SerialisableHelpers
Defined in:
lib/serialisable.rb

Instance Method Summary collapse

Instance Method Details

#get_attributes(root, hash) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/serialisable.rb', line 118

def get_attributes(root, hash)
  hash.map do |name, (selector, type)|
    value = root.attributes[selector].value
    value = parse_type(value, type)

    [name, value]
  end
end

#get_multiples(root, hash) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/serialisable.rb', line 91

def get_multiples(root, hash)
  hash.map do |name, (selector, type)|
    if type.respond_to?(:__deserialise_all, true)
      [name, type.send(:__deserialise_all, root)]
    else
      values = root.children.find_all {|node| node.name == selector }
        .map {|node| node.children.to_s }
        .map {|value| parse_type(value, type) }

      [name, values]
    end
  end
end

#get_singles(root, hash) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/serialisable.rb', line 105

def get_singles(root, hash)
  hash.map do |name, (selector, type)|
    if selector.respond_to?(:__deserialise, true)
      [name, selector.send(:__deserialise, root)]
    else
      value = root.children.find {|node| node.name == selector }.children.to_s
      value = parse_type(value, type)

      [name, value]
    end
  end
end

#parse_type(value, type) ⇒ Object

Parses the value read from xml if the type given responds to the method #parse, otherwise returns the string value.

Parameters:

  • value (String)
  • type (#parse)


132
133
134
# File 'lib/serialisable.rb', line 132

def parse_type(value, type)
  type.respond_to?(:parse) ? type.parse(value) : value
end