Skip to content

Commit

Permalink
Phase 2: Starting on Relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Dec 6, 2010
1 parent 7182516 commit 4e0087d
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ The Neo4j ID is available by using node.neo_id .
my_node[:name] = nil # Delete a node property using [:key] = nil
my_node.name = nil # Delete a node property by setting it to nil

node2 = Neography::Node.create
new_rel = Neography::Relationship.create(:family, my_node, node2) # Create a relationship from my_node to node2


See Neo4j API for:
* {Order}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/Traverser.Order.html]
Expand Down
11 changes: 11 additions & 0 deletions lib/neography/node_relationship.rb
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
51 changes: 46 additions & 5 deletions lib/neography/relationship.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
module Neography
class Relationship < PropertyContainer
attr_reader :start, :end, :type
include Neography::Equal
include Neography::Property

def initialize(hash=nil)
attr_accessor :start_node, :end_node, :rel_type

class << self

def create(type, from_node, to_node, props=nil)
rel = Neography::Relationship.new(from_node.neo_server.create_relationship(type, from_node, to_node, props))
rel.start_node = from_node
rel.end_node = to_node
rel.rel_type = type
rel
end

def load(*args)
# the first argument can be an hash of properties to set
rel = !args[0].is_a?(Neography::Rest) && args[0] || args[1]

# a db instance can be given, it is the first argument or the second
db = (args[0].is_a?(Neography::Rest) && args[0]) || args[1] || Neography::Rest.new
rel = db.get_relationship(rel)
unless rel.nil?
rel = Neography::Relationship.new(rel)
rel.start_node = Neography::Node.load(rel.start_node)
rel.end_node = Neography::Node.load(rel.end_node)
end
rel
end
end

def initialize(hash=nil, neo_server=nil)
super(hash)
@start = hash["start"].split('/').last
@end = hash["end"].split('/').last
@type = hash["type"]
@start_node = hash["start"].split('/').last
@end_node = hash["end"].split('/').last
@rel_type = hash["type"]
end

def del
self.start_node.delete_relationship(self.neo_id)
end

def other_node(node)
if node = @start_node
@end_node
else
@start_node
end
end

end
Expand Down
59 changes: 59 additions & 0 deletions lib/neography/relationship_traverser.rb
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
2 changes: 1 addition & 1 deletion lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def get_id(id)
id["self"].split('/').last
when String
id.split('/').last
when Neography::Node
when Neography::Node || Neography::Relationship
id.neo_id
else
id
Expand Down
36 changes: 36 additions & 0 deletions spec/integration/relationship_spec.rb
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

0 comments on commit 4e0087d

Please sign in to comment.