Class: RTF::Node

Inherits:
Object
  • Object
show all
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

ContainerNode, ImageNode, TextNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Node

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.



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

def initialize(parent)
   @parent = parent
end

Instance Attribute Details

#parentObject

Node parent.



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.

Returns:

  • (Boolean)


46
47
48
# File 'lib/rtf/node.rb', line 46

def is_root?
   @parent == nil
end

#next_nodeObject

This method retrieves a Node objects next peer node, returning nil if the Node has no previous peer.



34
35
36
37
38
39
40
41
# File 'lib/rtf/node.rb', line 34

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_nodeObject

This method retrieves a Node objects previous peer node, returning nil if the Node has no previous peer.



23
24
25
26
27
28
29
30
# File 'lib/rtf/node.rb', line 23

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

#rootObject

This method traverses a Node tree to locate the root element.



51
52
53
54
55
# File 'lib/rtf/node.rb', line 51

def root
   node = self
   node = node.parent while node.parent != nil
   node
end