Method: Exchanger::Field#to_xml

Defined in:
lib/exchanger/field.rb

#to_xml(value, options = {}) ⇒ Object

Convert Ruby value to XML



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/exchanger/field.rb', line 97

def to_xml(value, options = {})
  if value.is_a?(Exchanger::Element)
    value.tag_name = tag_name
    value.to_xml(options)
  else
    doc = Nokogiri::XML::Document.new
    root = doc.create_element(tag_name)
    case value
    when Array
      value.each do |sub_value|
        root << sub_field.to_xml(sub_value, options)
      end
    when Boolean
      root << doc.create_text_node(value == true)
    when Time
      root << doc.create_text_node(value.xmlschema)
    else # String, Integer, etc
      root << doc.create_text_node(value.to_s)
    end
    root
  end
end