Class: RTF::ContainerNode
- 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
Instance Attribute Summary collapse
-
#children ⇒ Object
Children elements of the node.
Attributes inherited from Node
Instance Method Summary collapse
-
#[](index) ⇒ Object
This method overloads the array dereference operator to allow for access to the child elements of a ContainerNode object.
-
#each ⇒ Object
This method provides for iteration over the contents of a ContainerNode object.
-
#first ⇒ Object
This method fetches the first node child for a ContainerNode object.
-
#initialize(parent) ⇒ ContainerNode
constructor
This is the constructor for the ContainerNode class.
-
#last ⇒ Object
This method fetches the last node child for a ContainerNode object.
-
#size ⇒ Object
This method returns a count of the number of children a ContainerNode object contains.
-
#store(node) ⇒ Object
This method adds a new node element to the end of the list of nodes maintained by a ContainerNode object.
-
#to_rtf ⇒ Object
This method generates the RTF text for a ContainerNode object.
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
#children ⇒ Object
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 |
#each ⇒ Object
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 |
#first ⇒ Object
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 |
#last ⇒ Object
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 |
#size ⇒ Object
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 |