From ad95b7c2965804c2c0dc4635a5db2a24407b5b46 Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Thu, 27 Mar 2014 01:23:25 -0500 Subject: [PATCH] Refactor Node Properties --- lib/neography/rest.rb | 19 +--------- lib/neography/rest/batch.rb | 6 ++-- lib/neography/rest/node_properties.rb | 49 +++++++++++++++++++++++--- spec/unit/rest/node_properties_spec.rb | 45 ++++++++++++----------- 4 files changed, 71 insertions(+), 48 deletions(-) diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 4920717..33397e1 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -45,6 +45,7 @@ class Rest include Constraints include Transactions include Nodes + include NodeProperties extend Forwardable attr_reader :connection @@ -54,7 +55,6 @@ class Rest def initialize(options = ENV['NEO4J_URL'] || {}) @connection = Connection.new(options) - @node_properties ||= NodeProperties.new(@connection) @node_relationships ||= NodeRelationships.new(@connection) @other_node_relationships ||= OtherNodeRelationships.new(@connection) @node_indexes ||= NodeIndexes.new(@connection) @@ -91,23 +91,6 @@ def delete_node!(id) # get("/nodes/") # end - # node properties - - def get_node_properties(id, *properties) - @node_properties.get(id, *properties.flatten) - end - - def set_node_properties(id, properties) - @node_properties.set(id, properties) - end - - def reset_node_properties(id, properties) - @node_properties.reset(id, properties) - end - - def remove_node_properties(id, *properties) - @node_properties.remove(id, *properties.flatten) - end # relationships diff --git a/lib/neography/rest/batch.rb b/lib/neography/rest/batch.rb index 35d5c28..e9b933f 100644 --- a/lib/neography/rest/batch.rb +++ b/lib/neography/rest/batch.rb @@ -103,19 +103,19 @@ def remove_node_from_index(index, key_or_id, value_or_id = nil, id = nil) # NodeProperties def set_node_property(id, property) - put NodeProperties.single_path(:id => get_id(id), :property => property.keys.first) do + put "/node/%{id}/properties/%{property}" % {:id => get_id(id), :property => property.keys.first} do property.values.first end end def reset_node_properties(id, body) - put NodeProperties.all_path(:id => get_id(id)) do + put "/node/%{id}/properties" % {:id => get_id(id)} do body end end def remove_node_property(id, property) - delete NodeProperties.single_path(:id => get_id(id), :property => property) + delete "/node/%{id}/properties/%{property}" % {:id => get_id(id), :property => property} end # NodeLabel diff --git a/lib/neography/rest/node_properties.rb b/lib/neography/rest/node_properties.rb index e09dcc1..5a78592 100644 --- a/lib/neography/rest/node_properties.rb +++ b/lib/neography/rest/node_properties.rb @@ -1,10 +1,51 @@ module Neography class Rest - class NodeProperties < Properties - extend Neography::Rest::Paths + module NodeProperties + + def set_node_properties(id, properties) + properties.each do |property, value| + options = { :body => value.to_json, :headers => json_content_type } + @connection.put("/node/%{id}/properties/%{property}" % {:id => get_id(id), :property => property}, options) + end + end + + def reset_node_properties(id, properties) + options = { :body => properties.to_json, :headers => json_content_type } + @connection.put("/node/%{id}/properties" % {:id => get_id(id)}, options) + end + + def get_node_properties(id, *properties) + if properties.none? + @connection.get("/node/%{id}/properties" % {:id => get_id(id)}) + else + get_each_node_properties(id, *properties) + end + end + + def get_each_node_properties(id, *properties) + retrieved_properties = properties.flatten.inject({}) do |memo, property| + value = @connection.get("/node/%{id}/properties/%{property}" % {:id => get_id(id), :property => property}) + memo[property] = value unless value.nil? + memo + end + return nil if retrieved_properties.empty? + retrieved_properties + end + + def remove_node_properties(id, *properties) + if properties.none? + @connection.delete("/node/%{id}/properties" % {:id => get_id(id)}) + else + remove_each_node_properties(id, *properties) + end + end + + def remove_each_node_properties(id, *properties) + properties.flatten.each do |property| + @connection.delete("/node/%{id}/properties/%{property}" % {:id => get_id(id), :property => property}) + end + end - add_path :all, "/node/:id/properties" - add_path :single, "/node/:id/properties/:property" end end diff --git a/spec/unit/rest/node_properties_spec.rb b/spec/unit/rest/node_properties_spec.rb index cd71cab..15feb84 100644 --- a/spec/unit/rest/node_properties_spec.rb +++ b/spec/unit/rest/node_properties_spec.rb @@ -4,8 +4,7 @@ module Neography class Rest describe NodeProperties do - let(:connection) { double } - subject { NodeProperties.new(connection) } + subject { Neography::Rest.new } it "sets properties" do options1 = { @@ -16,9 +15,9 @@ class Rest :body => '"qux"', :headers => json_content_type } - connection.should_receive(:put).with("/node/42/properties/foo", options1) - connection.should_receive(:put).with("/node/42/properties/baz", options2) - subject.set("42", {:foo => "bar", :baz => "qux"}) + subject.connection.should_receive(:put).with("/node/42/properties/foo", options1) + subject.connection.should_receive(:put).with("/node/42/properties/baz", options2) + subject.set_node_properties("42", {:foo => "bar", :baz => "qux"}) end it "resets properties" do @@ -26,36 +25,36 @@ class Rest :body => '{"foo":"bar"}', :headers => json_content_type } - connection.should_receive(:put).with("/node/42/properties", options) - subject.reset("42", {:foo => "bar"}) + subject.connection.should_receive(:put).with("/node/42/properties", options) + subject.reset_node_properties("42", {:foo => "bar"}) end context "getting properties" do it "gets all properties" do - connection.should_receive(:get).with("/node/42/properties") - subject.get("42") + subject.connection.should_receive(:get).with("/node/42/properties") + subject.get_node_properties("42") end it "gets multiple properties" do - connection.should_receive(:get).with("/node/42/properties/foo") - connection.should_receive(:get).with("/node/42/properties/bar") - subject.get("42", "foo", "bar") + subject.connection.should_receive(:get).with("/node/42/properties/foo") + subject.connection.should_receive(:get).with("/node/42/properties/bar") + subject.get_node_properties("42", "foo", "bar") end it "returns multiple properties as a hash" do - connection.stub(:get).and_return("baz", "qux") - subject.get("42", "foo", "bar").should == { "foo" => "baz", "bar" => "qux" } + subject.connection.stub(:get).and_return("baz", "qux") + subject.get_node_properties("42", "foo", "bar").should == { "foo" => "baz", "bar" => "qux" } end it "returns nil if no properties were found" do - connection.stub(:get).and_return(nil, nil) - subject.get("42", "foo", "bar").should be_nil + subject.connection.stub(:get).and_return(nil, nil) + subject.get_node_properties("42", "foo", "bar").should be_nil end it "returns hash without nil return values" do - connection.stub(:get).and_return("baz", nil) - subject.get("42", "foo", "bar").should == { "foo" => "baz" } + subject.connection.stub(:get).and_return("baz", nil) + subject.get_node_properties("42", "foo", "bar").should == { "foo" => "baz" } end end @@ -63,14 +62,14 @@ class Rest context "removing properties" do it "removes all properties" do - connection.should_receive(:delete).with("/node/42/properties") - subject.remove("42") + subject.connection.should_receive(:delete).with("/node/42/properties") + subject.remove_node_properties("42") end it "removes multiple properties" do - connection.should_receive(:delete).with("/node/42/properties/foo") - connection.should_receive(:delete).with("/node/42/properties/bar") - subject.remove("42", "foo", "bar") + subject.connection.should_receive(:delete).with("/node/42/properties/foo") + subject.connection.should_receive(:delete).with("/node/42/properties/bar") + subject.remove_node_properties("42", "foo", "bar") end end