Permalink
Browse files

Do not require a block for traversal, return an array of nodes instead

So that you can append other enumerable methods: Node.traverse.map(&:name)
  • Loading branch information...
1 parent 1029970 commit baeb680034935b653f1ae8ab0a33adbdb000136e @semaperepelitsa committed Feb 6, 2012
Showing with 22 additions and 32 deletions.
  1. +6 −4 lib/mongoid/tree/traversal.rb
  2. +16 −28 spec/mongoid/tree/traversal_spec.rb
@@ -76,9 +76,9 @@ module Traversal
# They're extended into the base class automatically.
module ClassMethods # :nodoc:
def traverse(type = :depth_first, &block)
- raise ArgumentError, "No block given" unless block_given?
- roots.each { |root| root.traverse(type, &block) }
- nil
+ res = []
+ roots.each { |root| res.concat root.traverse(type, &block) }
+ res
end
end
@@ -95,8 +95,10 @@ def traverse(type = :depth_first, &block)
# results << node
# end
def traverse(type = :depth_first, &block)
- raise ArgumentError, "No block given" unless block_given?
+ res = []
+ block ||= lambda { |node| res << node }
send("#{type}_traversal", &block)
+ res
end
private
@@ -8,10 +8,6 @@
subject { Node.new }
- it "should require a block" do
- expect { subject.traverse }.to raise_error(/No block given/)
- end
-
[:depth_first, :breadth_first].each do |method|
it "should support #{method} traversal" do
expect { subject.traverse(method) {} }.to_not raise_error
@@ -86,12 +82,17 @@
node(:node5).move_above(node(:node6))
end
- it 'should return the nodes in the correct order' do
+ it 'should iterate through the nodes in the correct order' do
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 return the nodes in the correct order' do
+ result = node(:node1).traverse(:depth_first)
+ result.collect { |n| n.name.to_sym }.should == [:node1, :node2, :node3, :node4, :node5, :node6, :node7]
+ end
+
end
end
@@ -117,21 +118,12 @@
end
describe '.traverse' do
-
- describe 'when not given a block' do
-
- it 'raises an error' do
- expect {Node.traverse}.to raise_error ArgumentError, 'No block given'
- end
- end
-
before :each do
setup_tree <<-ENDTREE
- root1
- root2
ENDTREE
- @block = Proc.new {}
@root1 = node(:root1)
@root2 = node(:root2)
@@ -141,35 +133,31 @@
it 'grabs each root' do
Node.should_receive(:roots).and_return []
- Node.traverse &@block
+ Node.traverse.should == []
end
it 'defaults the "type" arg to :depth_first' do
- @root1.should_receive(:traverse).with(:depth_first)
- @root2.should_receive(:traverse).with(:depth_first)
+ @root1.should_receive(:traverse).with(:depth_first).and_return([])
+ @root2.should_receive(:traverse).with(:depth_first).and_return([])
- Node.traverse &@block
+ Node.traverse.should == []
end
it 'traverses each root' do
- @root1.should_receive(:traverse)
- @root2.should_receive(:traverse)
+ @root1.should_receive(:traverse).and_return([1, 2])
+ @root2.should_receive(:traverse).and_return([3, 4])
- Node.traverse &@block
+ Node.traverse.should == [1, 2, 3, 4]
end
describe 'when the "type" arg is :breadth_first' do
it 'traverses breadth-first' do
- @root1.should_receive(:traverse).with(:breadth_first)
- @root2.should_receive(:traverse).with(:breadth_first)
+ @root1.should_receive(:traverse).with(:breadth_first).and_return([])
+ @root2.should_receive(:traverse).with(:breadth_first).and_return([])
- Node.traverse :breadth_first, &@block
+ Node.traverse :breadth_first
end
end
-
- it 'returns nil' do
- Node.traverse(&@block).should be nil
- end
end
end

0 comments on commit baeb680

Please sign in to comment.