lib/xommelier/xml/element/serialization.rb in xommelier-0.1.8 vs lib/xommelier/xml/element/serialization.rb in xommelier-0.1.9

- old
+ new

@@ -13,12 +13,11 @@ encoding: 'utf-8' } module ClassMethods def from_xml(xml, options = {}) - new.tap do |doc| - doc.options = options + new({}, options).tap do |doc| doc.from_xml(xml, options) end end alias_method :parse, :from_xml alias_method :from_xommelier, :from_xml @@ -41,10 +40,11 @@ case xml when IO, String xml = Nokogiri::XML(xml) end @_xml_node = options.delete(:node) { xml.at_xpath(element_xpath(xml.document, element_name)) } + validate if options[:validate] if text? && @_xml_node.text? self.text = @_xml_node.text end @@ -70,11 +70,11 @@ builder = Nokogiri::XML::Builder.new(options) 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:#{xmlns.as}") attribute_values.delete('xmlns:xml') namespaces = attribute_values prefix = nil end current_xmlns = builder.doc.namespaces[prefix ? "xmlns:#{prefix}" : 'xmlns'] @@ -88,19 +88,22 @@ attribute_name = "#{attr_prefix}:#{attribute_options[:attribute_name]}" end end serialize_attribute(attribute_name, value, attribute_values) end - (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)) - end - if respond_to?(:text) - xml.text @text - 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) + ) end + xml.text(@text) if respond_to?(:text) + end.instance_variable_get(:@node) builder.to_xml end alias_method :to_xommelier, :to_xml def to_hash @@ -141,10 +144,10 @@ result end end def xml_document - @_xml_node.document + @_xml_node.try(:document) end def xmlns_xpath(xml_document = self.xml_document) self.class.xmlns_xpath(xml_document) end