Class: NxBuild::XmlNode

Inherits:
Object
  • Object
show all
Defined in:
lib/nxbuild/xml/node.rb

Direct Known Subclasses

Xml

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag) ⇒ XmlNode

Returns a new instance of XmlNode.



7
8
9
10
11
# File 'lib/nxbuild/xml/node.rb', line 7

def initialize(tag)
  @tag = tag
  @attr = {}
  @children = []
end

Instance Attribute Details

#attrObject

Returns the value of attribute attr.



4
5
6
# File 'lib/nxbuild/xml/node.rb', line 4

def attr
  @attr
end

#childrenObject

Returns the value of attribute children.



3
4
5
# File 'lib/nxbuild/xml/node.rb', line 3

def children
  @children
end

#tagObject (readonly)

Returns the value of attribute tag.



5
6
7
# File 'lib/nxbuild/xml/node.rb', line 5

def tag
  @tag
end

Instance Method Details

#<<(value) ⇒ Object



21
22
23
# File 'lib/nxbuild/xml/node.rb', line 21

def <<(value)
  @children << value
end

#[](str) ⇒ Object



13
14
15
# File 'lib/nxbuild/xml/node.rb', line 13

def [](str)
  @attr[str]
end

#[]=(str, value) ⇒ Object



17
18
19
# File 'lib/nxbuild/xml/node.rb', line 17

def []=(str, value)
  @attr[str] = value
end

#dump(buffer, indent = 0) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nxbuild/xml/node.rb', line 25

def dump(buffer, indent = 0)
  buffer << "  " * indent
  tagline = "<" + @tag
  @attr.each do |k, v|
    tagline += " #{k}=\"#{v}\""
  end
  if children.empty?
    tagline += "/>\n"
    buffer << tagline
    return
  end
  tagline += ">"
  unless children.size == 1 && !(children.first.is_a? XmlNode)
    tagline += "\n"
  end
  buffer << tagline
  children.each do |c|
    if c.is_a? XmlNode
      c.dump(buffer, indent + 1)
    elsif children.size == 1
      buffer << c
    else
      buffer << "  " * (indent + 1) + c + "\n"
    end
  end
  unless children.size == 1 && !(children.first.is_a? XmlNode)
    buffer << "  " * indent
  end
  buffer << "</#{@tag}>\n"
end