Skip to content

Commit

Permalink
Refactor Node Properties
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent 7bac0c7 commit ad95b7c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 48 deletions.
19 changes: 1 addition & 18 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Rest
include Constraints
include Transactions
include Nodes
include NodeProperties
extend Forwardable

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

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

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

0 comments on commit ad95b7c

Please sign in to comment.