A tree structure for Mongoid documents using the materialized path pattern
Ruby
Switch branches/tags
Pull request Compare This branch is 243 commits behind benedikt:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.bundle
autotest
lib/mongoid
spec
.gitignore
.rspec
Gemfile
Gemfile.lock
LICENSE
README.rdoc
Rakefile
mongoid-tree.gemspec

README.rdoc

mongoid-tree

A tree structure for Mongoid documents using the materialized path pattern

Requirements

  • mongoid (>= 2.0.0.beta.14)

Install

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

Usage

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

Utility methods

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.

Traversal

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

Callbacks

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.

Known issues

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

Repository

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

Copyright

Copyright © 2010 Benedikt Deicke. See LICENSE for details.