lib/xommelier/xml/element/serialization.rb in xommelier-0.1.11 vs lib/xommelier/xml/element/serialization.rb in xommelier-0.1.12
- old
+ new
@@ -61,15 +61,16 @@
def to_xml(options = {})
options = SERIALIZATION_OPTIONS.merge(options)
element_name = options.delete(:element_name) { self.element_name }
element_name = element_name.to_s
element_name << '_' if %w(text class id).include?(element_name)
+ xmlns = options[:ns] || self.xmlns
if options[:builder] # Non-root element
builder = options.delete(:builder)
attribute_values = {}
namespaces = builder.doc.namespaces
- prefix = builder.doc.namespaces.key(xmlns.uri)[6..-1].presence
+ prefix = options[:prefix] || builder.doc.namespaces.key(xmlns.uri)[6..-1].presence
else # Root element
builder = Nokogiri::XML::Builder.new(options)
attribute_values = children_namespaces.inject({xmlns: xmlns.uri}) do |hash, ns|
hash["xmlns:#{ns.as}"] = ns.uri
hash
@@ -81,28 +82,28 @@
end
current_xmlns = builder.doc.namespaces[prefix ? "xmlns:#{prefix}" : 'xmlns']
attributes.each do |name, value|
attribute_options = attribute_options(name)
attribute_name = attribute_options[:attribute_name]
- if (ns = attribute_options[:ns]).uri != current_xmlns
+ ns = attribute_options[:ns]
+ if ns.uri != current_xmlns
if ns.as == :xml
attribute_name = "xml:#{attribute_options[:attribute_name]}"
elsif attr_prefix = namespaces.key(ns.uri).try(:[], 6..-1).presence
attribute_name = "#{attr_prefix}:#{attribute_options[:attribute_name]}"
end
end
serialize_attribute(attribute_name, value, attribute_values)
end
@_xml_node = (prefix ? builder[prefix] : builder).
send(element_name, attribute_values) do |xml|
- elements.each do |name, value|
- serialize_element(
- name,
- value,
- xml,
- element_options(name).merge(parent_ns_prefix: prefix)
- )
+ self.class.elements.each do |name, element_options|
+ value = elements.fetch(name, options[:default])
+ if value
+ serialize_element(name, value, xml,
+ element_options.merge(overriden_xmlns: xmlns))
+ end
end
xml.text(@text) if respond_to?(:text)
end.instance_variable_get(:@node)
builder.to_xml
end
@@ -181,22 +182,25 @@
type.from_xommelier(node.text)
end
end
def serialize_element(name, value, xml, options = {})
- prefix = if options[:ns].try(:!=, xmlns)
- xml.doc.namespaces.key(options[:ns].uri)[6..-1].presence
- else
- nil
- end
case options[:count]
when :any, :many
single_element = options.merge(count: :one)
value.each { |item| serialize_element(name, item, xml, single_element) }
else
+ xmlns = options[:overriden_xmlns] || self.xmlns
+ prefix = if options[:prefix]
+ options[:prefix]
+ elsif options[:ns].try(:!=, xmlns)
+ xml.doc.namespaces.key(options[:ns].uri)[6..-1].presence
+ else
+ nil
+ end
case value
when Xommelier::Xml::Element
- value.to_xommelier(builder: xml, element_name: options[:element_name])
+ value.to_xommelier(builder: xml, element_name: options[:element_name], prefix: prefix, ns: options[:ns])
else
element_name = options[:element_name].to_s
element_name << '_' if %w(text class id).include?(element_name)
(prefix ? xml[prefix] : xml).send(element_name) { xml.text(value.to_xommelier) }
end