Class: XmlNodeStream::Node
- Inherits:
-
Object
- Object
- XmlNodeStream::Node
- Defined in:
- lib/xml_node_stream/node.rb
Overview
Representation of an XML node.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get the value of the node attribute with the given name.
-
#[]=(name, val) ⇒ Object
Set the value of the node attribute with the given name.
-
#add_child(node) ⇒ Object
Add a child node.
-
#ancestors ⇒ Object
Array of all ancestors of the node.
-
#append(text, strip_whitespace = true) ⇒ Object
Append text to the node value.
-
#append_cdata(text) ⇒ Object
Append CDATA to the node value.
-
#attributes ⇒ Object
Get the attributes of the node as a hash.
-
#children ⇒ Object
Array of the child nodes of the node.
-
#descendants ⇒ Object
Array of all descendants of the node.
-
#find(selector) ⇒ Object
Find the first node that matches the given XPath.
-
#finish! ⇒ Object
Called after end tag to ensure that whitespace at the end of the string is properly stripped.
-
#first_child ⇒ Object
Get the first child node.
-
#initialize(name, parent = nil, attributes = nil, value = nil) ⇒ Node
constructor
A new instance of Node.
-
#path ⇒ Object
Get the full XPath of the node.
-
#release! ⇒ Object
Release a node by removing it from the tree structure so that the Ruby garbage collector can reclaim the memory.
-
#remove_child(node) ⇒ Object
Remove a child node.
-
#root ⇒ Object
Get the root element of the node tree.
-
#select(selector) ⇒ Object
Find all nodes that match the given XPath.
Constructor Details
#initialize(name, parent = nil, attributes = nil, value = nil) ⇒ Node
Returns a new instance of Node.
8 9 10 11 12 13 14 |
# File 'lib/xml_node_stream/node.rb', line 8 def initialize (name, parent = nil, attributes = nil, value = nil) @name = name @attributes = attributes @parent = parent @parent.add_child(self) if @parent @value = value end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/xml_node_stream/node.rb', line 5 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
5 6 7 |
# File 'lib/xml_node_stream/node.rb', line 5 def parent @parent end |
#value ⇒ Object
Returns the value of attribute value.
6 7 8 |
# File 'lib/xml_node_stream/node.rb', line 6 def value @value end |
Instance Method Details
#[](name) ⇒ Object
Get the value of the node attribute with the given name.
69 70 71 |
# File 'lib/xml_node_stream/node.rb', line 69 def [] (name) return @attributes[name] if @attributes end |
#[]=(name, val) ⇒ Object
Set the value of the node attribute with the given name.
74 75 76 |
# File 'lib/xml_node_stream/node.rb', line 74 def []= (name, val) attributes[name] = val end |
#add_child(node) ⇒ Object
Add a child node.
79 80 81 82 |
# File 'lib/xml_node_stream/node.rb', line 79 def add_child (node) children << node node.instance_variable_set(:@parent, self) end |
#ancestors ⇒ Object
Array of all ancestors of the node.
38 39 40 41 42 43 44 |
# File 'lib/xml_node_stream/node.rb', line 38 def ancestors if @parent return [@parent] + @parent.ancestors else return [] end end |
#append(text, strip_whitespace = true) ⇒ Object
Append text to the node value. If strip_whitespace is true, whitespace at the beginning and end of the node value will be removed.
116 117 118 119 120 121 122 123 |
# File 'lib/xml_node_stream/node.rb', line 116 def append (text, strip_whitespace = true) if text @value ||= '' @last_strip_whitespace = strip_whitespace text = text.lstrip if @value.length == 0 and strip_whitespace @value << text if text.length > 0 end end |
#append_cdata(text) ⇒ Object
Append CDATA to the node value.
110 111 112 |
# File 'lib/xml_node_stream/node.rb', line 110 def append_cdata (text) append(text, false) end |
#attributes ⇒ Object
Get the attributes of the node as a hash.
47 48 49 |
# File 'lib/xml_node_stream/node.rb', line 47 def attributes @attributes ||= {} end |
#children ⇒ Object
Array of the child nodes of the node.
24 25 26 |
# File 'lib/xml_node_stream/node.rb', line 24 def children @children ||= [] end |
#descendants ⇒ Object
Array of all descendants of the node.
29 30 31 32 33 34 35 |
# File 'lib/xml_node_stream/node.rb', line 29 def descendants if children.empty? return children else return (children + children.collect{|child| child.descendants}).flatten end end |
#find(selector) ⇒ Object
Find the first node that matches the given XPath. See Selector for details.
99 100 101 |
# File 'lib/xml_node_stream/node.rb', line 99 def find (selector) select(selector).first end |
#finish! ⇒ Object
Called after end tag to ensure that whitespace at the end of the string is properly stripped.
126 127 128 |
# File 'lib/xml_node_stream/node.rb', line 126 def finish! #:nodoc @value.rstrip! if @value and @last_strip_whitespace end |
#first_child ⇒ Object
Get the first child node.
94 95 96 |
# File 'lib/xml_node_stream/node.rb', line 94 def first_child @children.first if @children end |
#path ⇒ Object
Get the full XPath of the node.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/xml_node_stream/node.rb', line 57 def path unless @path if @parent @path = "#{@parent.path}/#{@name}" else @path = "/#{@name}" end end return @path end |
#release! ⇒ Object
Release a node by removing it from the tree structure so that the Ruby garbage collector can reclaim the memory. This method should be called after you are done with a node. After it is called, the node will be removed from its parent’s children and will no longer be accessible.
19 20 21 |
# File 'lib/xml_node_stream/node.rb', line 19 def release! @parent.remove_child(self) if @parent end |
#remove_child(node) ⇒ Object
Remove a child node.
85 86 87 88 89 90 91 |
# File 'lib/xml_node_stream/node.rb', line 85 def remove_child (node) if @children if @children.delete(node) node.instance_variable_set(:@parent, nil) end end end |