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

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

#parentObject

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.

Returns:

  • (Boolean)


50
51
52
# File 'lib/rtf/node.rb', line 50

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.



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_nodeObject

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

#rootObject

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