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
87 def ancestors
88   node, nodes = self, []
89   meth = model.parent_association_name
90   while par = node.send(meth)
91     nodes << node = par
92   end
93   nodes
94 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
 99 def descendants
100   nodes = send(model.children_association_name).dup
101   send(model.children_association_name).each{|child| nodes.concat(child.descendants)}
102   nodes 
103 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
107 def root
108   ancestors.last || self
109 end
root?() click to toggle source

Returns true if this is a root node, false otherwise.

    # File lib/sequel/plugins/tree.rb
112 def root?
113   !new? && possible_root?
114 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
119 def self_and_siblings
120   if parent = send(model.parent_association_name)
121     parent.send(model.children_association_name)
122   else
123     model.roots
124   end
125 end
siblings() click to toggle source

Returns all siblings of the current node.

subchild1.siblings # => [subchild2]
    # File lib/sequel/plugins/tree.rb
130 def siblings
131   self_and_siblings - [self]
132 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
137 def possible_root?
138   !Array(model.parent_column).map{|c| self[c]}.all?
139 end