lib/xommelier/xml/element/serialization.rb in xommelier-0.1.5 vs lib/xommelier/xml/element/serialization.rb in xommelier-0.1.6
- old
+ new
@@ -71,11 +71,11 @@
attribute_values = children_namespaces.inject({xmlns: xmlns.uri}) do |hash, ns|
hash["xmlns:#{ns.as}"] = ns.uri
hash
end
attribute_values.delete("xmlns:#{xmlns.as.to_s}")
- attribute_values.delete("xmlns:xml")
+ attribute_values.delete('xmlns:xml')
namespaces = attribute_values
prefix = nil
end
current_xmlns = builder.doc.namespaces[prefix ? "xmlns:#{prefix}" : 'xmlns']
attributes.each do |name, value|
@@ -101,10 +101,29 @@
end
builder.to_xml
end
alias_method :to_xommelier, :to_xml
+ def to_hash
+ attributes.dup.tap do |hash|
+ @elements.each do |name, value|
+ options = element_options(name)
+ type = options[:type]
+ value = Array.wrap(value)
+ if type < Xml::Element
+ value = value.map(&:to_hash)
+ end
+ if value.count > 1
+ name = name.to_s.pluralize.to_sym
+ else
+ value = value.first
+ end
+ hash[name] = value
+ end
+ end
+ end
+
protected
def element_xpath(xmldoc = self.xml_document, name = nil)
self.class.element_xpath(xmldoc, name)
end
@@ -149,10 +168,10 @@
end
end
end
def typecast_element(type, node, options)
- if type < Xommelier::Xml::Element
+ if type < Xml::Element
type.from_xommelier(xml_document, options.merge(node: node))
else
type.from_xommelier(node.text)
end
end