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.



154
155
156
157
158
# File 'lib/rtf/node.rb', line 154

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

Instance Attribute Details

#childrenObject

Children elements of the node



147
148
149
# File 'lib/rtf/node.rb', line 147

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.



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

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

#eachObject

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



187
188
189
# File 'lib/rtf/node.rb', line 187

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.



175
176
177
# File 'lib/rtf/node.rb', line 175

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.



181
182
183
# File 'lib/rtf/node.rb', line 181

def last
   @children.last
end

#sizeObject

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



193
194
195
# File 'lib/rtf/node.rb', line 193

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.



165
166
167
168
169
170
171
# File 'lib/rtf/node.rb', line 165

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.



210
211
212
# File 'lib/rtf/node.rb', line 210

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