Class: RRTF::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/rrtf/node/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.



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

def initialize(parent)
   @parent = parent
end

Instance Attribute Details

#parentObject

Node parent.



6
7
8
# File 'lib/rrtf/node/node.rb', line 6

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)


42
43
44
# File 'lib/rrtf/node/node.rb', line 42

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.



30
31
32
33
34
35
36
37
# File 'lib/rrtf/node/node.rb', line 30

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.



19
20
21
22
23
24
25
26
# File 'lib/rrtf/node/node.rb', line 19

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.



47
48
49
50
51
# File 'lib/rrtf/node/node.rb', line 47

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