Skip to content

purify/mongoid-tree

 
 

Repository files navigation

A tree structure for Mongoid documents using the materialized path pattern

  • mongoid (>= 2.0.0.beta.14)

To install mongoid_tree, simply add it to your Gemfile:

gem 'mongoid-tree'

In order to get the latest development version of mongoid-tree:

gem 'mongoid-tree', :git => 'git://github.com/benedikt/mongoid-tree'

You might want to add the :require => 'mongoid/tree' option as well and finally run

bundle install

Read the API documentation at benedikt.github.com/mongoid-tree and take a look at the Mongoid::Tree module

require 'mongoid/tree'

class Node
  include Mongoid::Document
  include Mongoid::Tree
end

There are several utility methods that help getting to other related documents in the tree:

Node.root
Node.roots
Node.leaves

node.root
node.parent
node.children
node.ancestors
node.ancestors_and_self
node.descendants
node.descendants_and_self
node.siblings
node.siblings_and_self
node.leaves

In addition it’s possible to check certain aspects of the documents position in the tree:

node.root?
node.leaf?
node.depth
node.ancestor_of?(other)
node.descendant_of?(other)

See Mongoid::Tree for more information on these methods.

It’s possible to traverse the tree using different traversal methods. See Mongoid::Tree::Traversal for details

node.traverse(:breadth_first) do |n|
  # Do something with Node n
end

There are two callbacks that are called before and after the rearranging process. This enables you to do additional computations after the documents position in the tree is updated. See Mongoid::Tree for details.

See github.com/benedikt/mongoid-tree/issues

See github.com/benedikt/mongoid-tree and feel free to fork it!

Copyright © 2010 Benedikt Deicke. See LICENSE for details.

About

A tree structure for Mongoid documents using the materialized path pattern

Resources

License

Stars

Watchers

Forks

Packages

No packages published