module Sequel::Plugins::Tree::InstanceMethods

def ancestors

subchild1.ancestors # => [child1, root]

Returns list of ancestors, starting from parent until root.
def ancestors
  node, nodes = self, []
  nodes << node = node.parent while node.parent
  nodes
end

def descendants

subchild1.ancestors # => [child1, root]

Returns list of ancestors, starting from parent until root.
def descendants
  nodes = children.dup
  nodes.each{|child| nodes.concat(child.descendants)}
  nodes 
end

def root

This node is returned if it is a root node itself.
Returns the root node of the tree that this node descends from
def root
  ancestors.last || self
end

def root?

Returns true if this is a root node, false otherwise.
def root?
  !new? && self[model.parent_column].nil?
end

def self_and_siblings

subchild1.self_and_siblings # => [subchild1, subchild2]

Returns all siblings and a reference to the current node.
def self_and_siblings
  parent ? parent.children : model.roots
end

def siblings

subchild1.siblings # => [subchild2]

Returns all siblings of the current node.
def siblings
  self_and_siblings - [self]
end