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