From 9e3b38cc7ca11a4f162ac09683414a32182921ac Mon Sep 17 00:00:00 2001 From: maxdemarzi Date: Fri, 3 Dec 2010 14:19:51 -0800 Subject: [PATCH] adding setter and getter methods --- README.rdoc | 10 +++++++ lib/neography.rb | 2 ++ lib/neography/node.rb | 1 + lib/neography/property.rb | 53 +++++++++++++++++++++++++++++++++++ spec/integration/node_spec.rb | 43 +++++++++++++++++++++++++++- 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 lib/neography/property.rb diff --git a/README.rdoc b/README.rdoc index bb10978..bc7dfce 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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] diff --git a/lib/neography.rb b/lib/neography.rb index d7cd47b..c2e8f52 100644 --- a/lib/neography.rb +++ b/lib/neography.rb @@ -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' diff --git a/lib/neography/node.rb b/lib/neography/node.rb index 5370510..602a83f 100644 --- a/lib/neography/node.rb +++ b/lib/neography/node.rb @@ -2,6 +2,7 @@ module Neography class Node < PropertyContainer include Neography::NodeRelationship include Neography::Equal + include Neography::Property attr_accessor :neo_server diff --git a/lib/neography/property.rb b/lib/neography/property.rb new file mode 100644 index 0000000..a13c587 --- /dev/null +++ b/lib/neography/property.rb @@ -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 \ No newline at end of file diff --git a/spec/integration/node_spec.rb b/spec/integration/node_spec.rb index 02b0500..74d3a0a 100644 --- a/spec/integration/node_spec.rb +++ b/spec/integration/node_spec.rb @@ -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 \ No newline at end of file