Class: Gyoku::Array

Inherits:
Object
  • Object
show all
Defined in:
lib/gyoku/array.rb

Constant Summary collapse

NESTED_ELEMENT_NAME =
"element"

Class Method Summary collapse

Class Method Details

.to_xml(array, key, escape_xml = true, attributes = {}, options = {}) ⇒ Object

Translates a given array to XML. Accepts the XML key to add the elements to, whether to escape_xml and an optional Hash of attributes.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gyoku/array.rb', line 13

def self.to_xml(array, key, escape_xml = true, attributes = {}, options = {})

  self_closing = options.delete(:self_closing)
  unwrap = options[:unwrap] || false 

  iterate_with_xml array, key, attributes, options do |xml, item, attrs, index|
    if self_closing
      xml.tag!(key, attrs)
    else
      case item
        when ::Hash       then 
          if unwrap
            xml << Hash.to_xml(item, options)
          else
            xml.tag!(key, attrs) { xml << Hash.to_xml(item, options) }
          end
        when ::Array      then 
          xml.tag!(key, attrs) { xml << Array.to_xml(item, NESTED_ELEMENT_NAME) }
        when NilClass     then 
          xml.tag!(key, "xsi:nil" => "true")
        else              
          xml.tag!(key, attrs) { xml << XMLValue.create(item, escape_xml) }
      end
    end
  end
end