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.
154 155 156 157 158 159 160 161 162 |
# File 'lib/edge/forest.rb', line 154 def ancestors _ancestors = [] node = self while(node = node.parent) _ancestors.push(node) end _ancestors end |
#descendants ⇒ Object
Returns all descendants
165 166 167 168 169 170 171 |
# File 'lib/edge/forest.rb', line 165 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.
138 139 140 |
# File 'lib/edge/forest.rb', line 138 def root parent ? parent.root : self end |
#root? ⇒ Boolean
Returns true is this node is a root or false otherwise
143 144 145 |
# File 'lib/edge/forest.rb', line 143 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.
149 150 151 |
# File 'lib/edge/forest.rb', line 149 def siblings parent ? parent.children - [self] : [] end |