Module: SimpleTree::InstanceMethods

Defined in:
lib/simple_tree.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

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

Returns:

  • (Boolean)


46
47
48
# File 'lib/simple_tree.rb', line 46

def child?
  self.parent != nil
end

#descendantsObject



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

#rootObject

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

#root?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/simple_tree.rb', line 42

def root?
  self.root == self
end

#self_and_ancestorsObject



30
31
32
33
# File 'lib/simple_tree.rb', line 30

def self_and_ancestors
  nodes = [] << self
  nodes += self.ancestors
end

#self_and_descendantsObject



70
71
72
73
# File 'lib/simple_tree.rb', line 70

def self_and_descendants
  node = [] << self
  node += self.descendants 
end

#self_and_siblingsObject

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

#siblingsObject

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