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

Defined in:
lib/dm-is-tree/is/tree.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

Returns list of ancestors, starting with the root.

grandchild1.ancestors # => [root, child]


116
117
118
119
120
# File 'lib/dm-is-tree/is/tree.rb', line 116

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]


142
143
144
# File 'lib/dm-is-tree/is/tree.rb', line 142

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

#rootObject Also known as: first_root

Returns the root node of the current node’s tree.

grandchild1.root # => root


125
126
127
128
129
# File 'lib/dm-is-tree/is/tree.rb', line 125

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

#siblingsObject

Returns all siblings of the current node.

grandchild1.siblings # => [grandchild2]


135
136
137
# File 'lib/dm-is-tree/is/tree.rb', line 135

def siblings
  generation.reject{|r| r.key == self.key }
end