Module: DataMapper::Is::Tree::InstanceMethods
- Defined in:
- lib/dm-is-tree/is/tree.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns list of ancestors, starting with the root.
-
#generation ⇒ Object
(also: #self_and_siblings)
Returns all children of the current node’s parent.
-
#root ⇒ Object
(also: #first_root)
Returns the root node of the current node’s tree.
-
#siblings ⇒ Object
Returns all siblings of the current node.
Instance Method Details
#ancestors ⇒ Object
Returns list of ancestors, starting with the root.
grandchild1.ancestors # => [root, child]
116 117 118 119 120 |
# File 'lib/dm-is-tree/is/tree.rb', line 116 def ancestors node, nodes = self, [] nodes << node = node.parent while node.parent nodes.reverse end |
#generation ⇒ Object Also known as: self_and_siblings
Returns all children of the current node’s parent.
grandchild1.generation # => [grandchild1, grandchild2]
142 143 144 |
# File 'lib/dm-is-tree/is/tree.rb', line 142 def generation parent ? parent.children : self.class.roots end |
#root ⇒ Object Also known as: first_root
Returns the root node of the current node’s tree.
grandchild1.root # => root
125 126 127 128 129 |
# File 'lib/dm-is-tree/is/tree.rb', line 125 def root node = self node = node.parent while node.parent node end |
#siblings ⇒ Object
Returns all siblings of the current node.
grandchild1.siblings # => [grandchild2]
135 136 137 |
# File 'lib/dm-is-tree/is/tree.rb', line 135 def siblings generation.reject{|r| r.key == self.key } end |