lib/xommelier/xml/element/serialization.rb in xommelier-0.1.27 vs lib/xommelier/xml/element/serialization.rb in xommelier-0.1.28
- old
+ new
@@ -20,10 +20,11 @@
def from_xml(xml, options = {})
new({}, options).tap do |doc|
doc.from_xml(xml, options)
end
end
+
alias_method :parse, :from_xml
alias_method :from_xommelier, :from_xml
def ns_element(ns, element)
[ns, element].compact.join(':')
@@ -59,10 +60,11 @@
self.class.elements.values.each do |element|
deserialize_element(element)
end
end
+
alias_method :from_xommelier, :from_xml
def to_xml(options = {})
options = SERIALIZATION_OPTIONS.merge(options)
save_options = options.slice(:encoding, *SAVE_OPTIONS)
@@ -91,33 +93,30 @@
current_xmlns = builder.doc.namespaces[prefix ? "xmlns:#{prefix}" : 'xmlns']
attributes.each do |name, value|
attribute = attribute_options(name)
attribute_name = attribute.attribute_name
ns = attribute.ns
- if ns.uri != current_xmlns
- if ns.as == :xml
- attribute_name = "xml:#{attribute_name}"
- elsif (attr_prefix = namespaces.key(ns.uri).try(:[], 6..-1).presence)
- attribute_name = "#{attr_prefix}:#{attribute_name}"
- end
+ if ns.uri != current_xmlns && (attr_prefix = namespaces.key(ns.uri).try(:[], 6..-1).presence)
+ attribute_name = "#{attr_prefix}:#{attribute_name}"
end
- serialize_attribute(attribute_name, value, attribute_values)
+ serialize_attribute(attribute_name, value, attribute_values) if (value != nil) || attribute.required?
end
@_xml_node = (prefix ? builder[prefix] : builder).
- send(element_name, attribute_values) do |xml|
+ send(element_name, attribute_values) do |xml|
self.class.elements.each do |name, element|
value = elements.fetch(name, options[:default])
unless value == nil
element.override(xmlns: xmlns) do
serialize_element(name, value, xml, element)
end
end
end
- xml.text(@text) if respond_to?(:text)
+ xml.text(@text) if text?
end.instance_variable_get(:@node)
builder.to_xml(save_options)
end
+
alias_method :to_xommelier, :to_xml
def to_hash
attributes.dup.tap do |hash|
@elements.each do |name, value|
@@ -215,11 +214,11 @@
end
# @param [Xommelier::Xml::Element::Structure::Attribute] attribute
def deserialize_attribute(attribute)
ns = attribute.ns
- if ns.default? || ns == xmlns
+ if ns == xmlns || attribute.xml?
send(attribute.writer, @_xml_node[attribute.attribute_name])
else
send(attribute.writer, @_xml_node.attribute_with_ns(attribute.attribute_name, ns.uri.to_s).try(:value))
end
end
@@ -257,20 +256,20 @@
value.each do |item|
serialize_element(name, item, xml, element)
end
end
else
- xmlns = element.overridden_xmlns || self.xmlns
+ xmlns = element.overridden_xmlns || self.xmlns
prefix = if xmlns != xml.doc.namespaces['xmlns']
xml.doc.namespaces.key(element.ns.uri).try(:[], 6..-1).presence
end
case value
when Xommelier::Xml::Element
value.to_xommelier(
- builder: xml,
+ builder: xml,
element_name: element.element_name,
- prefix: prefix,
- ns: element.ns
+ prefix: prefix,
+ ns: element.ns
)
else
(prefix ? xml[prefix] : xml).send(element.serializable_element_name) do
xml.text(value.to_xommelier)
end