Skip to content

Commit

Permalink
Refactor Relationship Properties
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent a7fa348 commit 6afc11a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 104 deletions.
21 changes: 1 addition & 20 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -47,6 +46,7 @@ class Rest
include Nodes
include NodeProperties
include Relationships
include RelationshipProperties
extend Forwardable

attr_reader :connection
Expand All @@ -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)

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 0 additions & 56 deletions lib/neography/rest/properties.rb

This file was deleted.

49 changes: 45 additions & 4 deletions lib/neography/rest/relationship_properties.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
45 changes: 22 additions & 23 deletions spec/unit/rest/relationship_properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -16,61 +15,61 @@ 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
options = {
: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

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
Expand Down

0 comments on commit 6afc11a

Please sign in to comment.