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.



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

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

Instance Attribute Details

#childrenObject

Children elements of the node



134
135
136
# File 'lib/rtf/node.rb', line 134

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.



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

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

#eachObject

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



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

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.



162
163
164
# File 'lib/rtf/node.rb', line 162

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.



168
169
170
# File 'lib/rtf/node.rb', line 168

def last
   @children.last
end

#sizeObject

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



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

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.



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

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.



197
198
199
# File 'lib/rtf/node.rb', line 197

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