Module: Sequel::Plugins::Tree::InstanceMethods

Defined in:
lib/sequel/plugins/tree.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

Returns list of ancestors, starting from parent until root.

subchild1.ancestors # => [child1, root]


87
88
89
90
91
# File 'lib/sequel/plugins/tree.rb', line 87

def ancestors
  node, nodes = self, []
  nodes << node = node.parent while node.parent
  nodes
end

#descendantsObject

Returns list of descendants

node.descendants # => [child1, child2, subchild1_1, subchild1_2, subchild2_1, subchild2_2]


96
97
98
99
100
# File 'lib/sequel/plugins/tree.rb', line 96

def descendants
  nodes = children.dup
  children.each{|child| nodes.concat(child.descendants)}
  nodes 
end

#rootObject

Returns the root node of the tree that this node descends from. This node is returned if it is a root node itself.



104
105
106
# File 'lib/sequel/plugins/tree.rb', line 104

def root
  ancestors.last || self
end

#root?Boolean

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

Returns:

  • (Boolean)


109
110
111
# File 'lib/sequel/plugins/tree.rb', line 109

def root?
  !new? && possible_root?
end

#self_and_siblingsObject

Returns all siblings and a reference to the current node.

subchild1.self_and_siblings # => [subchild1, subchild2]


116
117
118
# File 'lib/sequel/plugins/tree.rb', line 116

def self_and_siblings
  parent ? parent.children : model.roots
end

#siblingsObject

Returns all siblings of the current node.

subchild1.siblings # => [subchild2]


123
124
125
# File 'lib/sequel/plugins/tree.rb', line 123

def siblings
  self_and_siblings - [self]
end