lib/atom/xml/parser.rb in ratom-0.3.0 vs lib/atom/xml/parser.rb in ratom-0.3.1

- old
+ new

@@ -66,10 +66,11 @@ o.class_eval do def o.element_specs; @element_specs ||= {}; end def o.attributes; @attributes ||= []; end def element_specs; self.class.element_specs; end def attributes; self.class.attributes; end + def o.namespace(ns = @namespace); @namespace = ns; end end o.send(:extend, DeclarationMethods) end def ==(o) @@ -82,30 +83,35 @@ else false end end - def to_xml(nodeonly = false, root_name = self.class.name.demodulize.downcase) - + def to_xml(nodeonly = false, root_name = self.class.name.demodulize.downcase, namespace = nil) node = XML::Node.new(root_name) - node['xmlns'] = Atom::NAMESPACE unless nodeonly + node['xmlns'] = self.class.namespace unless nodeonly || !self.class.respond_to?(:namespace) - self.class.element_specs.values.each do |spec| - if spec.single? - if attribute = self.send(spec.attribute) - if attribute.is_a?(Time) - node << XML::Node.new(spec.name, attribute.xmlschema) - elsif attribute.respond_to?(:to_xml) - node << attribute.to_xml(true) - else - n = XML::Node.new(spec.name) - n << attribute - node << n - end + self.class.element_specs.values.select {|s| s.single? }.each do |spec| + if attribute = self.send(spec.attribute) + if attribute.respond_to?(:to_xml) + node << attribute.to_xml(true, spec.name, spec.options[:namespace]) + else + n = XML::Node.new(spec.name) + n['xmlns'] = spec.options[:namespace] + n << (attribute.is_a?(Time)? attribute.xmlschema : attribute.to_s) + node << n end - else - self.send(spec.attribute).each do |attribute| + end + end + + self.class.element_specs.values.select {|s| !s.single? }.each do |spec| + self.send(spec.attribute).each do |attribute| + if attribute.respond_to?(:to_xml) node << attribute.to_xml(true, spec.name.singularize) + else + n = XML::Node.new(spec.name.singularize) + n['xmlns'] = spec.options[:namespace] + n << attribute.to_s + node << n end end end self.class.attributes.each do |attribute|