Module: Edge::Forest::InstanceMethods
- Defined in:
- lib/edge/forest.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns all ancestors ordered by nearest ancestors first.
-
#descendants ⇒ Object
Returns all descendants.
-
#root ⇒ Object
Returns the root of this node.
-
#root? ⇒ Boolean
Returns true is this node is a root or false otherwise.
-
#siblings ⇒ Object
Returns all sibling nodes (nodes that have the same parent).
Instance Method Details
#ancestors ⇒ Object
Returns all ancestors ordered by nearest ancestors first.
151 152 153 154 155 156 157 158 159 |
# File 'lib/edge/forest.rb', line 151 def ancestors _ancestors = [] node = self while(node = node.parent) _ancestors.push(node) end _ancestors end |
#descendants ⇒ Object
Returns all descendants
162 163 164 165 166 167 168 |
# File 'lib/edge/forest.rb', line 162 def descendants if children.present? children + children.flat_map(&:descendants) else [] end end |
#root ⇒ Object
Returns the root of this node. If this node is root returns self.
135 136 137 |
# File 'lib/edge/forest.rb', line 135 def root parent ? parent.root : self end |
#root? ⇒ Boolean
Returns true is this node is a root or false otherwise
140 141 142 |
# File 'lib/edge/forest.rb', line 140 def root? !self[forest_foreign_key] end |
#siblings ⇒ Object
Returns all sibling nodes (nodes that have the same parent). If this node is a root node it returns an empty array.
146 147 148 |
# File 'lib/edge/forest.rb', line 146 def siblings parent ? parent.children - [self] : [] end |