module Mongoid::Tree::Traversal::ClassMethods

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

Public Instance Methods

traverse(type = :depth_first, &block) click to toggle source

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.

@example

# 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
# File lib/mongoid/tree/traversal.rb, line 77
def traverse(type = :depth_first, &block)
  roots.collect { |root| root.traverse(type, &block) }.flatten
end