Skip to content

Commit

Permalink
impl pending specs for traversal of any depth [#13]
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasronge committed Dec 28, 2008
1 parent 14bdb15 commit 0fa3422
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
27 changes: 26 additions & 1 deletion lib/neo4j/relations/has_n_relations.rb
Expand Up @@ -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<Fixnum,Symbol>:: 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,
Expand Down Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions test/neo4j/relation_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0fa3422

Please sign in to comment.