-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7182516
commit 4e0087d
Showing
6 changed files
with
156 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
module Neography | ||
module NodeRelationship | ||
|
||
def rels(*type) | ||
RelationshipTraverser.new(self, type, :both) | ||
end | ||
|
||
def rel(dir, type) | ||
RelationshipTraverser.new(self, type, dir) | ||
end | ||
|
||
def rel? (type=nil, dir=:both) | ||
self.neo_server.get_node_relationships(self, dir, type).empty? | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Neography | ||
class RelationshipTraverser | ||
include Enumerable | ||
|
||
def initialize(node, types, direction) | ||
@node = node | ||
@types = [types] | ||
@direction = direction | ||
end | ||
|
||
def to_s | ||
if @types.size == 1 && !@types.empty? | ||
"#{self.class} [type: #{@type} dir:#{@direction}]" | ||
elsif !@types.empty? | ||
"#{self.class} [types: #{@types.join(',')} dir:#{@direction}]" | ||
else | ||
"#{self.class} [types: ANY dir:#{@direction}]" | ||
end | ||
end | ||
|
||
def each | ||
iterator.each { |i| yield Neography::Relationship.new(i, @node.neo_server) } | ||
end | ||
|
||
def empty? | ||
first == nil | ||
end | ||
|
||
def iterator | ||
@node.neo_server.get_node_relationships(@node, @direction, @types) | ||
end | ||
|
||
def del | ||
each { |rel| @node.neo_server.delete_relationship(rel) } | ||
end | ||
|
||
def size | ||
[*self].size | ||
end | ||
|
||
def both | ||
@direction = :both | ||
self | ||
end | ||
|
||
def incoming | ||
raise "Not allowed calling incoming when finding several relationships types" if @types | ||
@direction = :incoming | ||
self | ||
end | ||
|
||
def outgoing | ||
raise "Not allowed calling outgoing when finding several relationships types" if @types | ||
@direction = :outgoing | ||
self | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require File.join(File.dirname(__FILE__), '..', 'spec_helper') | ||
|
||
describe Neography::Relationship do | ||
it "#new(:family, p1, p2) creates a new relationship between to nodes of given type" do | ||
p1 = Neography::Node.create | ||
p2 = Neography::Node.create | ||
|
||
new_rel = Neography::Relationship.create(:family, p1, p2) | ||
puts new_rel.inspect | ||
new_rel.start_node.should == p1 | ||
new_rel.end_node.should == p2 | ||
# p1.outgoing(:family).should include(p2) | ||
# p2.incoming(:family).should include(p1) | ||
end | ||
|
||
it "#new(:family, p1, p2, :since => '1998', :colour => 'blue') creates relationship and sets its properties" do | ||
p1 = Neography::Node.create | ||
p2 = Neography::Node.create | ||
|
||
rel = Neography::Relationship.create(:family, p1, p2, :since => 1998, :colour => 'blue') | ||
rel[:since].should == 1998 | ||
rel[:colour].should == 'blue' | ||
rel.since.should == 1998 | ||
rel.colourshould == 'blue' | ||
end | ||
|
||
it "#outgoing(:friends).new(other) creates a new relationship between self and other node" do | ||
p1 = Neography::Node.create | ||
p2 = Neography::Node.create | ||
|
||
rel = p1.outgoing(:foo).create(p2) | ||
p1.outgoing(:foo).first.should == p2 | ||
rel.should be_kind_of(Neography::Relationship) | ||
end | ||
|
||
end |