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.
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
#children ⇒ Object
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 |
#each ⇒ Object
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 |
#first ⇒ Object
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 |
#last ⇒ Object
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 |
#size ⇒ Object
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 |