module Sequel::Plugins::Tree::InstanceMethods
Public Instance Methods
ancestors()
click to toggle source
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
# File lib/sequel/plugins/tree.rb, line 91 def ancestors node, nodes = self, [] meth = model.parent_association_name while par = node.send(meth) nodes << node = par end nodes end
descendants()
click to toggle source
Returns list of descendants
node.descendants # => [child1, child2, subchild1_1, subchild1_2, subchild2_1, subchild2_2]
# File lib/sequel/plugins/tree.rb, line 103 def descendants nodes = send(model.children_association_name).dup send(model.children_association_name).each{|child| nodes.concat(child.descendants)} nodes end
root()
click to toggle source
Returns the root node of the tree that this node descends from. This node is returned if it is a root node itself.
# File lib/sequel/plugins/tree.rb, line 111 def root ancestors.last || self end
root?()
click to toggle source
Returns true if this is a root node, false otherwise.
# File lib/sequel/plugins/tree.rb, line 116 def root? !new? && possible_root? end
self_and_siblings()
click to toggle source
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
# File lib/sequel/plugins/tree.rb, line 123 def self_and_siblings if parent = send(model.parent_association_name) parent.send(model.children_association_name) else model.roots end end
siblings()
click to toggle source
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
# File lib/sequel/plugins/tree.rb, line 134 def siblings self_and_siblings - [self] end
Private Instance Methods
possible_root?()
click to toggle source
True if if all parent columns values are not NULL.
# File lib/sequel/plugins/tree.rb, line 141 def possible_root? !Array(model.parent_column).map{|c| self[c]}.all? end