Class: NOMS::XmlHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/noms/httpclient.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#set_deep

Constructor Details

#initialize(el) ⇒ XmlHash

Returns a new instance of XmlHash.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/noms/httpclient.rb', line 48

def initialize(el)
    super
    @element = el
    @name = el.name
    self['text'] = el.text
    el.attributes.each do |attr, value|
       self[attr] = value
    end
    self['children'] = []
    el.elements.each do |child|
       self['children'] << NOMS::XmlHash.new(child)
    end
end

Instance Attribute Details

#elementObject

Returns the value of attribute element.



46
47
48
# File 'lib/noms/httpclient.rb', line 46

def element
  @element
end

#nameObject

Returns the value of attribute name.



46
47
48
# File 'lib/noms/httpclient.rb', line 46

def name
  @name
end

Instance Method Details

#to_xml(name = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/noms/httpclient.rb', line 62

def to_xml(name=nil)
    el = REXML::Element.new(name || self.name)
    el.text = self['text'] if self.has_key? 'text'
    self.each do |key, val|
       next if ['children', 'text'].include? key
       el.add_attribute(key, val)
    end
    if self.has_key? 'children'
       self['children'].each do |child|
          el.add_element child.to_xml
       end
    end
    el
end