Home > mongomapper-tree

mongomapper-tree

Mongomapper-tree is a project mainly written in Ruby, based on the MIT license.

= simplificator-mongomapper-tree

== What

Representing a tree structure in mongodb using mongomapper. Highly experimental...just plaing around with mongodb and mongmapper.

So is it smart to store a tree in a document base database in a relational fashion? Not sure. Sometimes embedded documents wont work (especialy when quering and you do not know at what depth to query).

Changing the parent of a node is not supported, when creating a child node you have to specify the parent.

== How class TreeNode include MongoMapper::Document include MongoMapper::Tree

key :name, String

key :path, String
key :depth, Integer

key :parent_id, Mongo::ObjectID
belongs_to :parent, :class_name => 'TreeNode'

many :children, :class_name => 'TreeNode', :foreign_key => :parent_id

end

node = TreeNode.create(:name => 'root') child = TreeNode.create(:name => 'child', :parent => node)

methods provided

node.depth node.ancestors node.self_and_ancestors node.descendants node.self_and_descendants

node.root node.ancestor_of?(child) node.descendant_of?(child)

== Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

== Copyright

Copyright (c) 2010 pascalbetz. See LICENSE for details.

Previous:mongo_blog