lib/xommelier/xml/element/serialization.rb in xommelier-0.1.14 vs lib/xommelier/xml/element/serialization.rb in xommelier-0.1.15
- old
+ new
@@ -20,17 +20,21 @@
end
end
alias_method :parse, :from_xml
alias_method :from_xommelier, :from_xml
+ def ns_element(ns, element)
+ [ns, element].compact.join(':')
+ end
+
def element_xpath(xmldoc = nil, name = nil)
- "#{xmlns_xpath(xmldoc)}:#{name || element_name}"
+ ns_element(xmlns_xpath(xmldoc), name || element_name)
end
def xmlns_xpath(xml_document = nil)
if xml_document
- prefix = xml_document.namespaces.key(xmlns.uri)
+ prefix = xml_document.namespaces.key(xmlns.try(:uri))
(prefix =~ /:/) ? prefix[6..-1] : prefix
else
xmlns.as
end
end
@@ -47,11 +51,11 @@
if text? && @_xml_node.text?
self.text = @_xml_node.text
end
self.class.attributes.each do |name, options|
- send(name, @_xml_node[name])
+ deserialize_attribute(name, options)
end
self.class.elements.each do |name, options|
deserialize_element(name, options)
end
@@ -128,10 +132,12 @@
end
end
protected
+ delegate :ns_element, to: 'self.class'
+
def element_xpath(xmldoc = self.xml_document, name = nil)
self.class.element_xpath(xmldoc, name)
end
def children_namespaces(namespaces = Set[xmlns])
@@ -158,13 +164,23 @@
def serialize_attribute(name, value, attributes)
attributes[name] = value.to_xommelier
end
+ def deserialize_attribute(name, options = nil)
+ options ||= self.element_options(name)
+ ns = options[:ns]
+ if ns.default? || ns == xmlns
+ send(name, @_xml_node[options[:attribute_name]])
+ else
+ send(name, @_xml_node.attribute_with_ns(options[:attribute_name].to_s, ns.uri.to_s).try(:value))
+ end
+ end
+
def deserialize_element(name, options = nil)
options ||= self.element_options(name)
type = options[:type]
- nodes = @_xml_node.xpath("./#{options[:ns].as}:#{options[:element_name]}", options[:ns].to_hash)
+ nodes = @_xml_node.xpath("./#{ns_element(options[:ns].as, options[:element_name])}", options[:ns].to_hash)
if nodes.any?
case options[:count]
when :any, :many
children = nodes.map { |node| typecast_element(type, node, options) }
send options[:plural], children