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 ancestors, starting from parent until root.
-
#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.
-
#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]
86 87 88 89 90 |
# File 'lib/sequel/plugins/tree.rb', line 86 def ancestors node, nodes = self, [] nodes << node = node.parent while node.parent nodes end |
#descendants ⇒ Object
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
95 96 97 98 99 |
# File 'lib/sequel/plugins/tree.rb', line 95 def descendants nodes = children.dup nodes.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.
103 104 105 |
# File 'lib/sequel/plugins/tree.rb', line 103 def root ancestors.last || self end |
#root? ⇒ Boolean
Returns true if this is a root node, false otherwise.
108 109 110 |
# File 'lib/sequel/plugins/tree.rb', line 108 def root? !new? && self[model.parent_column].nil? end |
#self_and_siblings ⇒ Object
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
115 116 117 |
# File 'lib/sequel/plugins/tree.rb', line 115 def self_and_siblings parent ? parent.children : model.roots end |
#siblings ⇒ Object
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
122 123 124 |
# File 'lib/sequel/plugins/tree.rb', line 122 def siblings self_and_siblings - [self] end |