Module: Mongoid::Tree::Traversal::ClassMethods

Defined in:
lib/mongoid/tree/traversal.rb

Overview

This module implements class methods that will be available on the document that includes Mongoid::Tree::Traversal

Instance Method Summary collapse

Instance Method Details

#traverse(type = :depth_first, &block) ⇒ Object

Traverses the entire tree, one root at a time, using the given traversal method (Default is :depth_first).

See Mongoid::Tree::Traversal for available traversal methods.

Examples:


# Say we have the following tree, and want to print its hierarchy:
#   root_1
#     child_1_a
#   root_2
#     child_2_a
#       child_2_a_1

Node.traverse(:depth_first) do |node|
  indentation = '  ' * node.depth

  puts "#{indentation}#{node.name}"
end

77
78
79
# File 'lib/mongoid/tree/traversal.rb', line 77

def traverse(type = :depth_first, &block)
  roots.collect { |root| root.traverse(type, &block) }.flatten
end