Skip to content

Commit

Permalink
more phase 2: adding del and equal to node
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Dec 3, 2010
1 parent 7b90ece commit 54ddb73
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 15 deletions.
15 changes: 5 additions & 10 deletions lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,14 @@ def find_and_require_user_defined_code

require 'neography/config'
require 'neography/rest'
require 'neography/neography'

require 'neography/property_container'
require 'neography/node_relationship'
require 'neography/equal'

require 'neography/node'
require 'neography/relationship'

find_and_require_user_defined_code

module Neography

class << self

def ref_node(this_db = Neography::Rest.new)
this_db.get_root
end

end
end
21 changes: 21 additions & 0 deletions lib/neography/equal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Neography

# == This mixin is used for both nodes and relationships to decide if two entities are equal or not.
#
module Equal
def equal?(o)
eql?(o)
end

def eql?(o)
return false unless o.respond_to?(:neo_id)
o.neo_id == neo_id
end

def ==(o)
eql?(o)
end

end

end
10 changes: 10 additions & 0 deletions lib/neography/neography.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Neography

class << self

def ref_node(this_db = Neography::Rest.new)
this_db.get_root
end

end
end
17 changes: 16 additions & 1 deletion lib/neography/node.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
module Neography
class Node < PropertyContainer
include Neography::NodeRelationship
include Neography::Equal

attr_accessor :neo_server

class << self
def create(*args)
# the arguments can be an hash of properties to set or a rest instance
props = (args[0].respond_to?(:each_pair) && args[0]) || args[1]
db = (args[0].is_a?(Neography::Rest) && args[0]) || args[1] || Neography::Rest.new
Neography::Node.new(db.create_node(props))
node = Neography::Node.new(db.create_node(props))
node.neo_server = db
node
end

def load(*args)
Expand All @@ -17,11 +23,20 @@ def load(*args)
db = (args[0].is_a?(Neography::Rest) && args[0]) || args[1] || Neography::Rest.new
node = db.get_node(node)
node = Neography::Node.new(node) unless node.nil?
node.neo_server = db unless node.nil?
node
end

#alias_method :new, :create
end

def del
self.neo_server.delete_node!(self.neo_id)
end

def exist?
!self.neo_server.get_node(self.neo_id).nil?
end

end
end
6 changes: 6 additions & 0 deletions lib/neography/node_relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Neography
module NodeRelationship


end
end
52 changes: 48 additions & 4 deletions spec/integration/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
end


describe "get_node" do
describe "load" do
it "can get a node that exists" do
new_node = Neography::Node.create
existing_node = Neography::Node.load(new_node)
Expand All @@ -44,14 +44,14 @@
existing_node.neo_id.should == new_node.neo_id
end

it "returns nil if it tries to get a node that does not exist" do
it "returns nil if it tries to load a node that does not exist" do
new_node = Neography::Node.create
fake_node = new_node.neo_id.to_i + 1000
existing_node = Neography::Node.load(fake_node)
existing_node.should be_nil
end

it "can get a node that exists not on the default rest server" do
it "can load a node that exists not on the default rest server" do
@neo = Neography::Rest.new
new_node = Neography::Node.create(@neo)
existing_node = Neography::Node.load(new_node, @neo)
Expand All @@ -60,16 +60,60 @@
existing_node.neo_id.should == new_node.neo_id
end

it "can get a node that exists not on the default rest server the other way" do
it "can load a node that exists not on the default rest server the other way" do
@neo = Neography::Rest.new
new_node = Neography::Node.create(@neo)
existing_node = Neography::Node.load(@neo, new_node)
existing_node.should_not be_nil
existing_node.neo_id.should_not be_nil
existing_node.neo_id.should == new_node.neo_id
end
end

describe "del" do
it "can delete itself" do
new_node = Neography::Node.create
node_id = new_node.neo_id
new_node.del
deleted_node = Neography::Node.load(node_id)
deleted_node.should be_nil
end
end

describe "exists?" do
it "can tell if it exists" do
new_node = Neography::Node.create
new_node.exist?.should be_true
end

it "can tell if does not exists" do
new_node = Neography::Node.create
new_node.del
new_node.exist?.should be_false
end
end

describe "equality" do
it "can tell two nodes are the same with equal?" do
new_node = Neography::Node.create
another_node = Neography::Node.load(new_node)
new_node.equal?(another_node).should be_true
end

it "can tell two nodes are the same with eql?" do
new_node = Neography::Node.create
another_node = Neography::Node.load(new_node)
new_node.eql?(another_node).should be_true
end

it "can tell two nodes are the same with ==" do
new_node = Neography::Node.create
another_node = Neography::Node.load(new_node)
(new_node == another_node).should be_true
end


end


end

0 comments on commit 54ddb73

Please sign in to comment.