diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 6240cc9..ae319c8 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -3,7 +3,6 @@ require 'neography/rest/helpers' require 'neography/rest/paths' -require 'neography/rest/properties' require 'neography/rest/indexes' require 'neography/rest/auto_indexes' require 'neography/rest/schema_indexes' @@ -47,6 +46,7 @@ class Rest include Nodes include NodeProperties include Relationships + include RelationshipProperties extend Forwardable attr_reader :connection @@ -63,7 +63,6 @@ def initialize(options = ENV['NEO4J_URL'] || {}) @node_traversal ||= NodeTraversal.new(@connection) @node_paths ||= NodePaths.new(@connection) - @relationship_properties ||= RelationshipProperties.new(@connection) @relationship_indexes ||= RelationshipIndexes.new(@connection) @relationship_auto_indexes ||= RelationshipAutoIndexes.new(@connection) @@ -101,24 +100,6 @@ def get_relationship_end_node(rel) get_node(rel["end"]) end - - # relationship properties - - def get_relationship_properties(id, *properties) - @relationship_properties.get(id, *properties.flatten) - end - - def set_relationship_properties(id, properties) - @relationship_properties.set(id, properties) - end - - def reset_relationship_properties(id, properties) - @relationship_properties.reset(id, properties) - end - - def remove_relationship_properties(id, *properties) - @relationship_properties.remove(id, *properties.flatten) - end # node relationships diff --git a/lib/neography/rest/batch.rb b/lib/neography/rest/batch.rb index bd52cb8..dbf36e5 100644 --- a/lib/neography/rest/batch.rb +++ b/lib/neography/rest/batch.rb @@ -192,7 +192,7 @@ def remove_relationship_from_index(index, key_or_id, value_or_id = nil, id = nil # RelationshipProperties def set_relationship_property(id, property) - put RelationshipProperties.single_path(:id => get_id(id), :property => property.keys.first) do + put "/relationship/%{id}/properties/%{property}" % {:id => get_id(id), :property => property.keys.first} do property.values.first end end diff --git a/lib/neography/rest/properties.rb b/lib/neography/rest/properties.rb deleted file mode 100644 index 896c444..0000000 --- a/lib/neography/rest/properties.rb +++ /dev/null @@ -1,56 +0,0 @@ -module Neography - class Rest - class Properties - include Neography::Rest::Helpers - - def initialize(connection) - @connection ||= connection - end - - def set(id, properties) - properties.each do |property, value| - options = { :body => value.to_json, :headers => json_content_type } - @connection.put(single_path(:id => get_id(id), :property => property), options) - end - end - - def reset(id, properties) - options = { :body => properties.to_json, :headers => json_content_type } - @connection.put(all_path(:id => get_id(id)), options) - end - - def get(id, *properties) - if properties.none? - @connection.get(all_path(:id => get_id(id))) - else - get_each(id, *properties) - end - end - - def get_each(id, *properties) - retrieved_properties = properties.flatten.inject({}) do |memo, property| - value = @connection.get(single_path(: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(id, *properties) - if properties.none? - @connection.delete(all_path(:id => get_id(id))) - else - remove_each(id, *properties) - end - end - - def remove_each(id, *properties) - properties.flatten.each do |property| - @connection.delete(single_path(:id => get_id(id), :property => property)) - end - end - - end - end -end diff --git a/lib/neography/rest/relationship_properties.rb b/lib/neography/rest/relationship_properties.rb index c45b979..9f43869 100644 --- a/lib/neography/rest/relationship_properties.rb +++ b/lib/neography/rest/relationship_properties.rb @@ -1,10 +1,51 @@ module Neography class Rest - class RelationshipProperties < Properties - extend Neography::Rest::Paths + module RelationshipProperties + + def set_relationship_properties(id, properties) + properties.each do |property, value| + options = { :body => value.to_json, :headers => json_content_type } + @connection.put("/relationship/%{id}/properties/%{property}" % {:id => get_id(id), :property => property}, options) + end + end + + def reset_relationship_properties(id, properties) + options = { :body => properties.to_json, :headers => json_content_type } + @connection.put("/relationship/%{id}/properties" % {:id => get_id(id)}, options) + end + + def get_relationship_properties(id, *properties) + if properties.none? + @connection.get("/relationship/%{id}/properties" % {:id => get_id(id)}) + else + get_each_relationship_properties(id, *properties) + end + end + + def get_each_relationship_properties(id, *properties) + retrieved_properties = properties.flatten.inject({}) do |memo, property| + value = @connection.get("/relationship/%{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_relationship_properties(id, *properties) + if properties.none? + @connection.delete("/relationship/%{id}/properties" % {:id => get_id(id)}) + else + remove_each_relationship_properties(id, *properties) + end + end + + def remove_each_relationship_properties(id, *properties) + properties.flatten.each do |property| + @connection.delete("/relationship/%{id}/properties/%{property}" % {:id => get_id(id), :property => property}) + end + end - add_path :all, "/relationship/:id/properties" - add_path :single, "/relationship/:id/properties/:property" end end diff --git a/spec/unit/rest/relationship_properties_spec.rb b/spec/unit/rest/relationship_properties_spec.rb index 7ef6d66..194fc3b 100644 --- a/spec/unit/rest/relationship_properties_spec.rb +++ b/spec/unit/rest/relationship_properties_spec.rb @@ -4,8 +4,7 @@ module Neography class Rest describe RelationshipProperties do - let(:connection) { double } - subject { RelationshipProperties.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("/relationship/42/properties/foo", options1) - connection.should_receive(:put).with("/relationship/42/properties/baz", options2) - subject.set("42", {:foo => "bar", :baz => "qux"}) + subject.connection.should_receive(:put).with("/relationship/42/properties/foo", options1) + subject.connection.should_receive(:put).with("/relationship/42/properties/baz", options2) + subject.set_relationship_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("/relationship/42/properties", options) - subject.reset("42", {:foo => "bar"}) + subject.connection.should_receive(:put).with("/relationship/42/properties", options) + subject.reset_relationship_properties("42", {:foo => "bar"}) end context "getting properties" do it "gets all properties" do - connection.should_receive(:get).with("/relationship/42/properties") - subject.get("42") + subject.connection.should_receive(:get).with("/relationship/42/properties") + subject.get_relationship_properties("42") end it "gets multiple properties" do - connection.should_receive(:get).with("/relationship/42/properties/foo") - connection.should_receive(:get).with("/relationship/42/properties/bar") - subject.get("42", "foo", "bar") + subject.connection.should_receive(:get).with("/relationship/42/properties/foo") + subject.connection.should_receive(:get).with("/relationship/42/properties/bar") + subject.get_relationship_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_relationship_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_relationship_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_relationship_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("/relationship/42/properties") - subject.remove("42") + subject.connection.should_receive(:delete).with("/relationship/42/properties") + subject.remove_relationship_properties("42") end it "removes multiple properties" do - connection.should_receive(:delete).with("/relationship/42/properties/foo") - connection.should_receive(:delete).with("/relationship/42/properties/bar") - subject.remove("42", "foo", "bar") + subject.connection.should_receive(:delete).with("/relationship/42/properties/foo") + subject.connection.should_receive(:delete).with("/relationship/42/properties/bar") + subject.remove_relationship_properties("42", "foo", "bar") end end