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]


81
82
83
84
85
# File 'lib/sequel/plugins/tree.rb', line 81

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

#descendantsObject

Returns list of ancestors, starting from parent until root.

subchild1.ancestors # => [child1, root]


90
91
92
93
94
# File 'lib/sequel/plugins/tree.rb', line 90

def descendants
  nodes = children.dup
  nodes.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.



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

def root
  ancestors.last || self
end

#root?Boolean

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

Returns:

  • (Boolean)


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

def root?
  !new? && self[model.parent_column].nil?
end

#self_and_siblingsObject

Returns all siblings and a reference to the current node.

subchild1.self_and_siblings # => [subchild1, subchild2]


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

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

#siblingsObject

Returns all siblings of the current node.

subchild1.siblings # => [subchild2]


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

def siblings
  self_and_siblings - [self]
end