Module: Sequel::Plugins::Tree::InstanceMethods
- Defined in:
- lib/sequel_tree.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
-
#descendants ⇒ Object
Returns list of ancestors, starting from parent until root.
-
#root ⇒ Object
Returns the root node of the tree that this node descends from This node is returned if it is a root node itself.
-
#self_and_siblings ⇒ Object
Returns all siblings and a reference to the current node.
-
#siblings ⇒ Object
Returns all siblings of the current node.
Instance Method Details
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
53 54 55 56 57 |
# File 'lib/sequel_tree.rb', line 53 def ancestors node, nodes = self, [] nodes << node = node.parent while node.parent nodes end |
#descendants ⇒ Object
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
62 63 64 65 66 |
# File 'lib/sequel_tree.rb', line 62 def descendants nodes = self.children.dup nodes.each{|child| nodes.concat(child.descendants)} nodes end |
#root ⇒ Object
Returns the root node of the tree that this node descends from This node is returned if it is a root node itself.
70 71 72 |
# File 'lib/sequel_tree.rb', line 70 def root ancestors.last || self end |
#self_and_siblings ⇒ Object
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
84 85 86 |
# File 'lib/sequel_tree.rb', line 84 def self_and_siblings parent ? parent.children : model.roots end |
#siblings ⇒ Object
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
77 78 79 |
# File 'lib/sequel_tree.rb', line 77 def siblings self_and_siblings - [self] end |