Public Instance methods
ancestors()
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
[show source]
# File lib/sequel/plugins/tree.rb 92 def ancestors 93 node, nodes = self, [] 94 meth = model.parent_association_name 95 while par = node.send(meth) 96 nodes << node = par 97 end 98 nodes 99 end
descendants()
Returns list of descendants
node.descendants # => [child1, child2, subchild1_1, subchild1_2, subchild2_1, subchild2_2]
[show source]
# File lib/sequel/plugins/tree.rb 104 def descendants 105 nodes = send(model.children_association_name).dup 106 send(model.children_association_name).each{|child| nodes.concat(child.descendants)} 107 nodes 108 end
root()
Returns the root node of the tree that this node descends from. This node is returned if it is a root node itself.
[show source]
# File lib/sequel/plugins/tree.rb 112 def root 113 ancestors.last || self 114 end
root?()
Returns true if this is a root node, false otherwise.
[show source]
# File lib/sequel/plugins/tree.rb 117 def root? 118 !new? && possible_root? 119 end
self_and_siblings()
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
[show source]
# File lib/sequel/plugins/tree.rb 124 def self_and_siblings 125 if parent = send(model.parent_association_name) 126 parent.send(model.children_association_name) 127 else 128 model.roots 129 end 130 end
siblings()
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
[show source]
# File lib/sequel/plugins/tree.rb 135 def siblings 136 self_and_siblings - [self] 137 end