Module: SimpleTree::InstanceMethods
- Defined in:
- lib/simple_tree.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns list of ancestors, starting from parent until root.
- #child? ⇒ Boolean
- #descendants ⇒ Object
- #move_to_child_of(parent) ⇒ Object
-
#root ⇒ Object
Returns the root node of the tree.
- #root? ⇒ Boolean
- #self_and_ancestors ⇒ Object
- #self_and_descendants ⇒ Object
-
#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]
24 25 26 27 28 |
# File 'lib/simple_tree.rb', line 24 def ancestors node, nodes = self,[] nodes << node = node.parent while node.parent nodes end |
#child? ⇒ Boolean
46 47 48 |
# File 'lib/simple_tree.rb', line 46 def child? self.parent != nil end |
#descendants ⇒ Object
63 64 65 66 67 68 |
# File 'lib/simple_tree.rb', line 63 def descendants return [] if children.empty? nodes = children children.each {|c| nodes = nodes + c.descendants} nodes end |
#move_to_child_of(parent) ⇒ Object
75 76 77 |
# File 'lib/simple_tree.rb', line 75 def move_to_child_of(parent) self.parent = parent end |
#root ⇒ Object
Returns the root node of the tree.
36 37 38 39 40 |
# File 'lib/simple_tree.rb', line 36 def root node = self node = node.parent while node.parent node end |
#self_and_ancestors ⇒ Object
30 31 32 33 |
# File 'lib/simple_tree.rb', line 30 def self_and_ancestors nodes = [] << self nodes += self.ancestors end |
#self_and_descendants ⇒ Object
70 71 72 73 |
# File 'lib/simple_tree.rb', line 70 def self_and_descendants node = [] << self node += self.descendants end |
#self_and_siblings ⇒ Object
Returns all siblings and a reference to the current node.
subchild1.self_and_siblings # => [subchild1, subchild2]
59 60 61 |
# File 'lib/simple_tree.rb', line 59 def self_and_siblings parent ? parent.children : [] end |
#siblings ⇒ Object
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
52 53 54 |
# File 'lib/simple_tree.rb', line 52 def siblings self_and_siblings - [self] end |