Skip to content

Commit

Permalink
Merge pull request #24 from joeleaver/master
Browse files Browse the repository at this point in the history
:first and :all finders, plus find rels for a node
  • Loading branch information
andreasronge committed May 5, 2011
2 parents 83a91ca + 393e582 commit 278cb89
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 7 deletions.
26 changes: 25 additions & 1 deletion lib/neo4j/rails/relationships/node_dsl.rb
Expand Up @@ -56,17 +56,40 @@ def find(*args, &block)
return super(*args, &block) if block

case args.first
when :all, :first
kind = args.shift
send(kind, *args)
when "0", 0
nil
else
if ((args.first.is_a?(Integer) || args.first.is_a?(String)) && args.first.to_i > 0)
find_by_id(*args)
else
enum = Enumerator.new(@storage, :each_node, @dir).find{|n| n == args.first}
first(*args)
end
end
end

def all(*args)
unless args.empty?
enum = Enumerator.new(@storage, :each_node, @dir).find{|n| n == args.first}
else
enum = Enumerator.new(@storage, :each_node, @dir)
end
end

def first(*args)
if result = all(*args)
if result.respond_to?(:collect) #if it's enumerable, get the first result
result.min{|a,b| a.id <=> b.id}
else
result
end
else
nil
end
end

def destroy_all
each {|n| n.destroy}
end
Expand Down Expand Up @@ -103,6 +126,7 @@ def to_s

protected


def find_by_id(*args)
result = Enumerator.new(@storage, :each_node, @dir).find{|n| n.id.to_i == args.first.to_i}
end
Expand Down
58 changes: 53 additions & 5 deletions lib/neo4j/rails/relationships/rels_dsl.rb
Expand Up @@ -61,14 +61,47 @@ def delete_all

def find(*args, &block)
return super(*args, &block) if block
node = args.first
if @dir == :incoming
find{|r| r.start_node == node}

case args.first
when :all, :first
kind = args.shift
send(kind, *args)
when "0", 0
nil
else
if ((args.first.is_a?(Integer) || args.first.is_a?(String)) && args.first.to_i > 0)
find_by_id(*args)
else
first(*args)
end
end
end

def all(*args)
if args.first.class == Neo4j::Rails::Relationship #arg is a relationship
find{|r| r == args.first}
elsif ((args.first.is_a?(Integer) || args.first.is_a?(String)) && args.first.to_i > 0) #arg is an int
find{|r| r.start_node.id.to_i == args.first.to_i || r.end_node.id.to_i == args.first.to_i}
elsif node_in?(*args) #arg is a node
find{|r| r.start_node == args.first || r.end_node == args.first}
else #there either aren't any args, or we don't understand them
self
end
end

def first(*args)
if result = all(*args)
if result.respond_to?(:collect) #if it's enumerable, get the first result
result.min{|a,b| a.id <=> b.id}
else
result
end
else
find{|r| r.end_node == node}
nil
end
end


def [](index)
i = 0
each{|x| return x if i == index; i += 1}
Expand All @@ -81,11 +114,26 @@ def is_a?(type)
super
end


def to_s
"Rels dir: #{@dir}, #{@storage}"
end

protected

def node_in?(*args)
# does it contain an string, which will be treated like a condition ?
if args.find { |a| a.class.superclass == Neo4j::Rails::Model }
return true
else
return false
end
end

def find_by_id(*args)
find{|r| r.id.to_i == args.first.to_i}
end


end
end
end
Expand Down
45 changes: 44 additions & 1 deletion spec/rails/active_record_style_relationships_spec.rb
Expand Up @@ -86,6 +86,15 @@ class ModelRelationship1 < Neo4j::Rails::Relationship
end

describe "find nodes in relationship" do

it "find all child nodes" do
@actor.acted_in.find(:all).should_not be_nil
end

it "find first child node" do
@actor.acted_in.find(:first).should_not be_nil
end

it "find a child node by node" do
@actor.acted_in.find(@movie_1).should_not be_nil
end
Expand All @@ -97,8 +106,42 @@ class ModelRelationship1 < Neo4j::Rails::Relationship
it "find a child node by delegate to Enumerable#find" do
@actor.acted_in.find{|n| n.title == 'movie_1'}.should_not be_nil
end
end

describe "find rels by node or id" do
it "find all rels" do
@actor.acted_in_rels.find(:all).should_not be_nil
end

it "find first rel" do
@actor.acted_in_rels.find(:all).should_not be_nil
end

it "find rels for a node, by node" do
@actor.acted_in_rels.find(@movie_1).should_not be_nil
end

it "find rels by id" do
relid = @actor.acted_in_rels.find(@movie_1).id
@actor.acted_in_rels.find(relid).should_not be_nil
end

it "find all rels for a node, by node" do
@actor.acted_in_rels.find(:all, @movie_1).should_not be_nil
end

it "find all rels for a node, by node id" do
@actor.acted_in_rels.find(:all, @movie_1.id).should_not be_nil
end

it "find first rels for a node, by node" do
@actor.acted_in_rels.find(:first, @movie_1).should_not be_nil
end

it "find first rels for a node, by node id" do
@actor.acted_in_rels.find(:first, @movie_1.id).should_not be_nil
end


end

describe "build outgoing on rel" do
Expand Down

0 comments on commit 278cb89

Please sign in to comment.