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
94 def ancestors
95   node, nodes = self, []
96   nodes << node = node.parent while node.parent
97   nodes
98 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
103 def descendants
104   nodes = children.dup
105   children.each{|child| nodes.concat(child.descendants)}
106   nodes 
107 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
111 def root
112   ancestors.last || self
113 end
root?() click to toggle source

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

    # File lib/sequel/plugins/tree.rb
116 def root?
117   !new? && possible_root?
118 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
123 def self_and_siblings
124   parent ? parent.children : model.roots
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