Class: RTF::ContainerNode

Inherits:
Node
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rtf/node.rb

Overview

This class represents a Node that can contain other Node objects. Its a base class for more specific Node types.

Direct Known Subclasses

CommandNode, TableNode, TableRowNode

Instance Attribute Summary collapse

Attributes inherited from Node

#parent

Instance Method Summary collapse

Methods inherited from Node

#is_root?, #next_node, #previous_node, #root

Constructor Details

#initialize(parent) ⇒ ContainerNode

This is the constructor for the ContainerNode class.

Parameters

parent

A reference to the parent node that owners the new ContainerNode object.



150
151
152
153
154
# File 'lib/rtf/node.rb', line 150

def initialize(parent)
   super(parent)
   @children = []
   @children.concat(yield) if block_given?
end

Instance Attribute Details

#childrenObject

Children elements of the node



143
144
145
# File 'lib/rtf/node.rb', line 143

def children
  @children
end

Instance Method Details

#[](index) ⇒ Object

This method overloads the array dereference operator to allow for access to the child elements of a ContainerNode object.

Parameters

index

The offset from the first child of the child object to be returned. Negative index values work from the back of the list of children. An invalid index will cause a nil value to be returned.



201
202
203
# File 'lib/rtf/node.rb', line 201

def [](index)
   @children[index]
end

#eachObject

This method provides for iteration over the contents of a ContainerNode object.



183
184
185
# File 'lib/rtf/node.rb', line 183

def each
   @children.each {|child| yield child}
end

#firstObject

This method fetches the first node child for a ContainerNode object. If a container contains no children this method returns nil.



171
172
173
# File 'lib/rtf/node.rb', line 171

def first
   @children[0]
end

#lastObject

This method fetches the last node child for a ContainerNode object. If a container contains no children this method returns nil.



177
178
179
# File 'lib/rtf/node.rb', line 177

def last
   @children.last
end

#sizeObject

This method returns a count of the number of children a ContainerNode object contains.



189
190
191
# File 'lib/rtf/node.rb', line 189

def size
   @children.size
end

#store(node) ⇒ Object

This method adds a new node element to the end of the list of nodes maintained by a ContainerNode object. Nil objects are ignored.

Parameters

node

A reference to the Node object to be added.



161
162
163
164
165
166
167
# File 'lib/rtf/node.rb', line 161

def store(node)
   if node != nil
      @children.push(node) if @children.include?(Node) == false
      node.parent = self if node.parent != self
   end
   node
end

#to_rtfObject

This method generates the RTF text for a ContainerNode object.



206
207
208
# File 'lib/rtf/node.rb', line 206

def to_rtf
   RTFError.fire("#{self.class.name}.to_rtf method not yet implemented.")
end