Class: LibXmlNode

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

Overview

This class is required to represent Nodes that contain attributes as well as other content, for example: <karin zak=“lebt”>schon</karin> We cannot non-ambigiously represent this node as a hash because Hashes may contain arbitrary keys. Having a separate class for representing such nodes allows for easy testing for the class (which we have to do anyway since hash values may be Strings, Arrays or other Hashes already) Update: No need for testing when using the iterable method. Usage: LibXmlNode.new(“karin”, “zak”)

Constant Summary collapse

VERSION =
'0.3.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLibXmlNode

Returns a new instance of LibXmlNode.



27
28
29
30
31
# File 'lib/rtiss_libxml_to_hash.rb', line 27

def initialize
  @subnodes = {}
  @attributes = {}
  @text = ""
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



24
25
26
# File 'lib/rtiss_libxml_to_hash.rb', line 24

def attributes
  @attributes
end

#subnodesObject

Returns the value of attribute subnodes.



23
24
25
# File 'lib/rtiss_libxml_to_hash.rb', line 23

def subnodes
  @subnodes
end

#textObject

Returns the value of attribute text.



25
26
27
# File 'lib/rtiss_libxml_to_hash.rb', line 25

def text
  @text
end

Class Method Details

.create(n, a, t) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rtiss_libxml_to_hash.rb', line 33

def self.create(n, a, t)
  l = LibXmlNode.new
  l.subnodes = n
  l.attributes = a
  l.text = t
  l
end

Instance Method Details

#==(other) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/rtiss_libxml_to_hash.rb', line 73

def ==(other)
  if other.class == LibXmlNode
    if @subnodes == other.subnodes and @attributes == other.attributes and @text == other.text
      return true
    end
  end
  false
end

#add_attribute(key, value) ⇒ Object



41
42
43
# File 'lib/rtiss_libxml_to_hash.rb', line 41

def add_attribute(key, value)
  @attributes[key] = value
end

#add_node(key, value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rtiss_libxml_to_hash.rb', line 45

def add_node(key, value)
  if @subnodes[key] 
    if @subnodes[key].is_a? Object::Array
      @subnodes[key] << value
    else
      @subnodes[key] = [@subnodes[key], value]
    end
  else
    @subnodes[key] = value
  end
end

#add_text(t) ⇒ Object



57
58
59
# File 'lib/rtiss_libxml_to_hash.rb', line 57

def add_text(t)
  @text << t
end

#iterableObject



82
83
84
# File 'lib/rtiss_libxml_to_hash.rb', line 82

def iterable
  [self]
end

#simplifyObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rtiss_libxml_to_hash.rb', line 61

def simplify
  if @attributes.empty?
    if @text == ""
      return @subnodes
    end
    if @subnodes == {}
      return @text
    end
  end
  return self
end