Permalink
Browse files

Removed some whitespace

  • Loading branch information...
1 parent f967c41 commit 5cdd156958c16cb7d40317679cb46c462b121032 @benedikt committed Jul 26, 2010
View
@@ -4,18 +4,18 @@ module Mongoid # :nodoc:
##
# = Mongoid::Tree
#
- # This module extends any Mongoid document with tree functionality.
- #
+ # This module extends any Mongoid document with tree functionality.
+ #
# == Usage
#
# Simply include the module in any Mongoid document:
- #
+ #
# class Node
# include Mongoid::Document
# include Mongoid::Tree
# end
#
- # === Using the tree structure
+ # === Using the tree structure
#
# Each document references many children. You can access them using the <tt>#children</tt> method.
#
@@ -29,129 +29,129 @@ module Mongoid # :nodoc:
# node.parent # => nil
# node.children.create
# node.children.first.parent # => node
- #
+ #
module Tree
extend ActiveSupport::Concern
include Traversal
-
+
included do
references_many :children, :class_name => self.name, :foreign_key => :parent_id, :inverse_of => :parent
referenced_in :parent, :class_name => self.name, :inverse_of => :children
-
+
field :parent_ids, :type => Array, :default => []
-
+
set_callback :validation, :before, :rearrange
set_callback :save, :after, :rearrange_children, :if => :rearrange_children?
end
-
+
##
# :method: children
# Returns a list of the document's children. It's a <tt>references_many</tt> association.
# (Generated by Mongoid)
-
+
##
# :method: parent
# Returns the document's parent (unless it's a root document). It's a <tt>referenced_in</tt> association.
# (Generated by Mongoid)
-
+
##
# :method: parent_ids
# Returns a list of the document's parent_ids, starting with the root node.
# (Generated by Mongoid)
-
+
##
- # Is this document a root node (has no parent)?
+ # Is this document a root node (has no parent)?
def root?
parent_id.nil?
end
-
+
##
# Is this document a leaf node (has no children)?
def leaf?
children.empty?
end
-
+
##
# Returns this document's root node
def root
self.class.find(parent_ids.first)
end
-
+
##
# Returns this document's ancestors
def ancestors
self.class.find(:conditions => { :_id.in => parent_ids })
end
-
+
##
# Returns this document's ancestors and itself
def ancestors_and_self
ancestors + [self]
end
-
+
##
# Is this document an ancestor of the other document?
def ancestor_of?(other)
other.parent_ids.include?(self.id)
- end
-
+ end
+
##
# Returns this document's descendants
def descendants
self.class.find(:conditions => { :parent_ids => self.id })
end
-
+
##
# Returns this document's descendants and itself
def descendants_and_self
[self] + descendants
end
-
+
##
# Is this document a descendant of the other document?
def descendant_of?(other)
self.parent_ids.include?(other.id)
end
-
+
##
# Returns this document's siblings
def siblings
siblings_and_self - [self]
end
-
- ##
+
+ ##
# Returns this document's siblings and itself
def siblings_and_self
self.class.find(:conditions => { :parent_id => self.parent_id })
end
-
+
##
# Forces rearranging of all children after next save
def rearrange_children!
@rearrange_children = true
end
-
+
##
# Will the children be rearranged after next save?
def rearrange_children?
!!@rearrange_children
end
-
+
private
-
+
def rearrange
if self.parent_id
self.parent_ids = self.class.find(self.parent_id).parent_ids + [self.parent_id]
end
-
+
rearrange_children! if self.parent_ids_changed?
return true
end
-
+
def rearrange_children
@rearrange_children = false
self.children.find(:all).each { |c| c.save }
end
end
-end
+end
@@ -12,8 +12,8 @@ module Tree
# == Depth First Traversal
#
# See http://en.wikipedia.org/wiki/Depth-first_search for a proper description.
- #
- # Given a tree like:
+ #
+ # Given a tree like:
#
# node1:
# - node2:
@@ -46,7 +46,7 @@ module Tree
# node1, node2, node3, node4, node5, node6, node7
#
module Traversal
-
+
##
# Traverses the tree using the given traversal method (Default is :depth_first)
# and passes each document node to the block.
@@ -61,16 +61,16 @@ module Traversal
# end
def traverse(type = :depth_first, &block)
raise "No block given" unless block_given?
- send("#{type}_traversal", &block)
- end
-
+ send("#{type}_traversal", &block)
+ end
+
private
-
+
def depth_first_traversal(&block)
block.call(self)
self.children.each { |c| c.send(:depth_first_traversal, &block) }
end
-
+
def breadth_first_traversal(&block)
queue = [self]
while queue.any? do
@@ -79,7 +79,7 @@ def breadth_first_traversal(&block)
queue += node.children
end
end
-
+
end
end
-end
+end
@@ -1,34 +1,34 @@
require 'spec_helper'
describe Mongoid::Tree::Traversal do
-
+
describe '#traverse' do
-
+
subject { Node.new }
-
+
it "should require a block" do
expect { subject.traverse }.to raise_error(/No block given/)
end
-
+
[:depth_first].each do |method|
it "should support #{method} traversal" do
expect { subject.traverse(method) {} }.to_not raise_error
end
end
-
+
it "should complain about unsupported traversal methods" do
expect { subject.traverse('non_existing') {} }.to raise_error
end
-
+
it "should default to depth_first traversal" do
subject.should_receive(:depth_first_traversal)
subject.traverse {}
end
-
+
end
-
+
describe 'depth first traversal' do
-
+
it "should traverse correctly" do
setup_tree <<-ENDTREE
node1:
@@ -39,38 +39,38 @@
- node6
- node7
ENDTREE
-
+
result = []
node(:node1).traverse(:depth_first) { |node| result << node }
result.collect { |n| n.name.to_sym }.should == [:node1, :node2, :node3, :node4, :node5, :node6, :node7]
end
-
+
it "should traverse correctly on merged trees" do
-
+
setup_tree <<-ENDTREE
- node4:
- node5
- node6:
- node7
-
+
- node1:
- node2:
- node3
ENDTREE
-
-
+
+
node(:node1).children << node(:node4)
-
-
+
+
result = []
node(:node1).traverse(:depth_first) { |node| result << node }
result.collect { |n| n.name.to_sym }.should == [:node1, :node2, :node3, :node4, :node5, :node6, :node7]
end
-
+
end
-
+
describe 'breadth first traversal' do
-
+
it "should traverse correctly" do
tree = setup_tree <<-ENDTREE
node1:
@@ -81,12 +81,12 @@
- node7
- node4
ENDTREE
-
+
result = []
node(:node1).traverse(:breadth_first) { |n| result << n }
result.collect { |n| n.name.to_sym }.should == [:node1, :node2, :node3, :node4, :node5, :node6, :node7]
end
-
+
end
-
-end
+
+end
Oops, something went wrong.

0 comments on commit 5cdd156

Please sign in to comment.