Module: DataMapper::Is::Tree::InstanceMethods

Defined in:
lib/dm_is_a_tree.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

Returns list of ancestors, starting with the root.

grandchild1.ancestors # => [root, child]


88
89
90
91
92
# File 'lib/dm_is_a_tree.rb', line 88

def ancestors
  node, nodes = self, []
  nodes << node = node.parent while node.parent
  nodes.reverse
end

#generationObject Also known as: self_and_siblings

Returns all children of the current node’s parent.

grandchild1.generation # => [grandchild1, grandchild2]


113
114
115
# File 'lib/dm_is_a_tree.rb', line 113

def generation
  parent ? parent.children : self.class.roots
end

#rootObject

grandchild1.root # => root



97
98
99
100
101
# File 'lib/dm_is_a_tree.rb', line 97

def root
  node = self
  node = node.parent while node.parent
  node
end

#siblingsObject

Returns all siblings of the current node.

grandchild1.siblings # => [grandchild2]


106
107
108
# File 'lib/dm_is_a_tree.rb', line 106

def siblings
  generation - [self]
end