Class: Gyoku::Hash

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

Class Method Summary collapse

Class Method Details

.to_xml(hash, options = {}) ⇒ Object

Translates a given hash with options to XML.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gyoku/hash.rb', line 11

def self.to_xml(hash, options = {})
  iterate_with_xml hash do |xml, key, value, attributes|
    self_closing = key.to_s[-1, 1] == "/"
    escape_xml = key.to_s[-1, 1] != "!"
    xml_key = XMLKey.create key, options

    case
      when :content! === key  then xml << XMLValue.create(value, escape_xml, options)
      when ::Array === value  then xml << Array.to_xml(value, xml_key, escape_xml, attributes, options.merge(:self_closing => self_closing))
      when ::Hash === value   then xml.tag!(xml_key, attributes) { xml << Hash.to_xml(value, options) }
      when self_closing       then xml.tag!(xml_key, attributes)
      when NilClass === value then xml.tag!(xml_key, "xsi:nil" => "true")
      else                         xml.tag!(xml_key, attributes) { xml << XMLValue.create(value, escape_xml, options) }
    end
  end
end