Class: XmlFu::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/xml-fu/hash.rb

Overview

By definition, a hash is an UNORDERED list of key/value pairs There's no sense in trying to order the keys. If order is of concern, use Array.to_xml

Class Method Summary collapse

Class Method Details

.filter(hash) ⇒ Object

Class method to filter out attributes and content from a given hash



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xml-fu/hash.rb', line 31

def self.filter(hash)
  attribs = {}
  content = hash.dup

  content.keys.select{|k| k =~ /^@/ }.each do |k|
    attribs[k[1..-1]] = content.delete(k)
  end

  # Use _content value if defined
  content = content.delete("=") || content

  return [content, attribs]
end

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

Convert Hash to XML String



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

def self.to_xml(hash, options={})
  each_with_xml hash, options do |xml, name, value, attributes|
    case value
    when ::OpenStruct then
      node = Node.new( name, OpenStruct.to_xml(value, options) )
      # We want the value returned from OpenStruct.to_xml NOT to be escaped
      node.escape_xml = false

      xml << node.to_xml
    else
      xml << Node.new(name, value, attributes).to_xml
    end
  end
end