From 0fa3422304d249ffb7e96c164e522e8b8e48fd90 Mon Sep 17 00:00:00 2001 From: Andreas Ronge Date: Sun, 28 Dec 2008 21:29:29 +0100 Subject: [PATCH] impl pending specs for traversal of any depth [#13] --- lib/neo4j/relations/has_n_relations.rb | 27 ++++++++++++++++++++++- test/neo4j/relation_spec.rb | 30 +++++++++++++++++++++----- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/lib/neo4j/relations/has_n_relations.rb b/lib/neo4j/relations/has_n_relations.rb index ea20e6b85..e40c639e4 100644 --- a/lib/neo4j/relations/has_n_relations.rb +++ b/lib/neo4j/relations/has_n_relations.rb @@ -27,7 +27,24 @@ def initialize(node, type, &filter) end end - + # Sets the depth of the traversal. + # Default is 1 if not specified. + # + # ==== Example + # morpheus.friends.depth(:all).each { ... } + # morpheus.friends.depth(3).each { ... } + # + # ==== Arguments + # d:: the depth or :all if traversing to the end of the network. + # ==== Return + # self + # + # :api: public + def depth(d) + @depth = d + self + end + def each stop = DepthStopEvaluator.new(@depth) traverser = @node.internal_node.traverse(org.neo4j.api.core.Traverser::Order::BREADTH_FIRST, @@ -98,9 +115,17 @@ class DepthStopEvaluator include org.neo4j.api.core.StopEvaluator def initialize(depth) +# puts "DEPTH = #{depth} #{depth.class.to_s}" @depth = depth end +# def self.new(depth) +# if depth.to_sym == :all +# return org.neo4j.api.core.StopEvaluator::END_OF_GRAPH +# end +# super depth +# end + def isStopNode(pos) pos.depth >= @depth end diff --git a/test/neo4j/relation_spec.rb b/test/neo4j/relation_spec.rb index 0506c9c41..f8c94bb22 100644 --- a/test/neo4j/relation_spec.rb +++ b/test/neo4j/relation_spec.rb @@ -192,7 +192,7 @@ class TestNode end end - describe "node#relations.outgoing.levels" do + describe "traversing nodes of any depth" do before(:all) do undefine_class :PersonNode class PersonNode @@ -208,15 +208,35 @@ class PersonNode @n12 = PersonNode.new @n112 = PersonNode.new @n1121 = PersonNode.new - @n0.friends << n1 << n12 - @n1.friends << n11 << n12 - @n11.friends << n111 << n112 - @n112.friends << n1121 + @n0.friends << @n1 << @n12 + @n1.friends << @n11 << @n12 + @n11.friends << @n111 << @n112 + @n112.friends << @n1121 + end + + it "should be possible with node.friends.depth(2).each" do + nodes = @n1.friends.depth(2) + nodes.should include(@n11,@n12,@n112) + nodes.should_not include(@n0,@n1,@n1121) + end + + it "should be possible with node.friends.depth(3).each" do + nodes = @n1.friends.depth(3) + nodes.should include(@n11,@n12,@n112, @n1121) + nodes.should_not include(@n0,@n1) + end + + it "should be possible with node.friends.depth(:all).each" do + pending + nodes = @n1.friends.depth(:all) + nodes.should include(@n11,@n12,@n112, @n1121) + nodes.should_not include(@n0,@n1) end it "should get all nodes two levels deep (for levels(2))" do pending nodes = @n1.relations.outgoing(:friends).levels(2) + @n1.friends.levels nodes.should include(@n11,@n12,@n112) nodes.should_not include(@n0,@n1,@n1121) end