Module: AxiomusApi::Serializable

Included in:
Base
Defined in:
lib/axiomus_api/serializable.rb

Instance Method Summary collapse

Instance Method Details

#to_xml(xml_header = false, tag_name = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/axiomus_api/serializable.rb', line 5

def to_xml(xml_header = false, tag_name = nil)
  serializable_fields = self.class.attribute_meta
  attributes = extract_attributes(serializable_fields)

  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.send(tag_name || self.tag_name, attributes) {
      serializable_fields.each do |f, opt|
        next if opt[:xml_type] == :attribute
        xml_name = opt[:xml_name] || f
        xml_type = opt[:xml_type] || :element
        val = self.send(f)
        next if val.nil?
        elements = opt[:array] ? val : [val]

        elements.each do |el|
          create_by_type(xml, el, xml_type, xml_name)
        end
      end
    }
  end

  xml = xml_header ? builder.to_xml : builder.doc.root.to_xml
  normalize_axiomus_xml(xml)
end