Skip to content

Commit

Permalink
adding setter and getter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Dec 3, 2010
1 parent 54ddb73 commit 9e3b38c
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ The Neo4j ID is available by using node.neo_id .
Neography::Node.load(@neo2, 5) # Get a node on the server defined in @neo2
Neography::Node.load(5, @neo2) # Same as above, but different order

my_node = Node.create
my_node.del # Deletes the node
my_node.exist? # returns true/false if node exists in Neo4j

my_node = Node.create("age" => 31, "name" => "Max")
my_node[:age] #returns 31 # Get a node's property using [:key]
my_node.name #returns "Max" # Get a node's property as a method
my_node[:age] = 24 #changes age to 24 # Set a node's property using [:key] =
my_node.name = "Alex" #changes name to "Alex" # Set a node's property as a method


See Neo4j API for:
* {Order}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/Traverser.Order.html]
Expand Down
2 changes: 2 additions & 0 deletions lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ def find_and_require_user_defined_code
require 'neography/neography'

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


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

Expand Down
1 change: 1 addition & 0 deletions lib/neography/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Neography
class Node < PropertyContainer
include Neography::NodeRelationship
include Neography::Equal
include Neography::Property

attr_accessor :neo_server

Expand Down
53 changes: 53 additions & 0 deletions lib/neography/property.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Neography
module Property


def [](key)
return unless respond_to?(key)
@table[key]
end

def []=(key, value)
k = key.to_s
if value.nil?
if self.is_a? Neography::Node
neo_server.remove_node_properties(self.neo_id, {k => value})
else
neo_server.remove_relationship_properties(self.neo_id, {k => value})
end
else
if self.is_a? Neography::Node
neo_server.set_node_properties(self.neo_id, {k => value})
else
neo_server.set_relationship_properties(self.neo_id, {k => value})
end

new_ostruct_member(k) unless self.respond_to?(key)

end
end


def new_ostruct_member(name)
name = name.to_sym
unless self.respond_to?(name)
meta = class << self; self; end
meta.send(:define_method, name) { @table[name] }
meta.send(:define_method, "#{name}=") do |x|
@table[name] = x
self[name.to_sym] = x
end
end
end


def self.method_missing(method_sym, *arguments, &block)
if (method_sym.to_s =~ /$=/) != nil
new_ostruct_member(method_sym.to_s.chomp("="))
else
super
end
end

end
end
43 changes: 42 additions & 1 deletion spec/integration/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,50 @@
another_node = Neography::Node.load(new_node)
(new_node == another_node).should be_true
end
end

describe "set properties" do
it "can change a node's properties that already exist using []=" do
new_node = Neography::Node.create("weight" => 150, "eyes" => "green")

end
new_node[:weight] = 200
new_node[:eyes] = "brown"

existing_node = Neography::Node.load(new_node)
existing_node.weight.should == 200
existing_node.eyes.should == "brown"
end

it "can change a node's properties that already exist" do
new_node = Neography::Node.create("weight" => 150, "eyes" => "green")

new_node.weight = 200
new_node.eyes = "brown"

existing_node = Neography::Node.load(new_node)
existing_node.weight.should == 200
existing_node.eyes.should == "brown"
end

it "can change a node's properties that does not already exist" do
new_node = Neography::Node.create("weight" => 150, "eyes" => "green")

new_node.weight = 200
new_node.eyes = "brown"
new_node[:hair] = "black"

existing_node = Neography::Node.load(new_node)
existing_node.weight.should == 200
existing_node.eyes.should == "brown"
existing_node.hair.should == "black"
end
end

describe "get node properties" do
it "can get node properties using []" do
new_node = Neography::Node.create("weight" => 150, "eyes" => "green")
new_node[:weight].should == 150
new_node[:eyes].should == "green"
end
end
end

0 comments on commit 9e3b38c

Please sign in to comment.