Module: ROXML::InstanceMethods

Defined in:
lib/roxml.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#to_xml(params = {}) ⇒ Object

Returns an XML object representing this object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/roxml.rb', line 32

def to_xml(params = {})
  params.reverse_merge!(:name => self.class.tag_name, :namespace => self.class.roxml_namespace)
  params[:namespace] = nil if ['*', 'xmlns'].include?(params[:namespace])
  XML.new_node([params[:namespace], params[:name]].compact.join(':')).tap do |root|
    refs = (self.roxml_references.present? \
      ? self.roxml_references \
      : self.class.roxml_attrs.map {|attr| attr.to_ref(self) })
    refs.each do |ref|
      value = ref.to_xml(self)
      unless value.nil?
        ref.update_xml(root, value)
      end
    end
    if params[:namespaces]
      params[:namespaces].each { |prefix, url| root.add_namespace_definition(prefix, url) }
    end
  end
end