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



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

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



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

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



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

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)


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

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