Class: RTF::Node
- Inherits:
-
Object
- Object
- RTF::Node
- Defined in:
- lib/rtf/node.rb
Overview
This class represents an element within an RTF document. The class provides a base class for more specific node types.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#parent ⇒ Object
Attribute accessor.
Instance Method Summary collapse
-
#initialize(parent) ⇒ Node
constructor
This is the constructor for the Node class.
-
#is_root? ⇒ Boolean
This method is used to determine whether a Node object represents a root or base element.
-
#next_node ⇒ Object
This method retrieves a Node objects next peer node, returning nil if the Node has no previous peer.
-
#previous_node ⇒ Object
This method retrieves a Node objects previous peer node, returning nil if the Node has no previous peer.
-
#root ⇒ Object
This method traverses a Node tree to locate the root element.
Constructor Details
#initialize(parent) ⇒ Node
This is the constructor for the Node class.
Parameters
- parent
-
A reference to the Node that owns the new Node. May be nil to indicate a base or root node.
21 22 23 |
# File 'lib/rtf/node.rb', line 21 def initialize(parent) @parent = parent end |
Instance Attribute Details
#parent ⇒ Object
Attribute accessor.
10 11 12 |
# File 'lib/rtf/node.rb', line 10 def parent @parent end |
Instance Method Details
#is_root? ⇒ Boolean
This method is used to determine whether a Node object represents a root or base element. The method returns true if the Nodes parent is nil, false otherwise.
50 51 52 |
# File 'lib/rtf/node.rb', line 50 def is_root? @parent == nil end |
#next_node ⇒ Object
This method retrieves a Node objects next peer node, returning nil if the Node has no previous peer.
38 39 40 41 42 43 44 45 |
# File 'lib/rtf/node.rb', line 38 def next_node peer = nil if parent != nil and parent.respond_to?(:children) index = parent.children.index(self) peer = parent.children[index + 1] end peer end |
#previous_node ⇒ Object
This method retrieves a Node objects previous peer node, returning nil if the Node has no previous peer.
27 28 29 30 31 32 33 34 |
# File 'lib/rtf/node.rb', line 27 def previous_node peer = nil if parent != nil and parent.respond_to?(:children) index = parent.children.index(self) peer = index > 0 ? parent.children[index - 1] : nil end peer end |
#root ⇒ Object
This method traverses a Node tree to locate the root element.
55 56 57 58 59 |
# File 'lib/rtf/node.rb', line 55 def root node = self node = node.parent while node.parent != nil node end |