Skip to content

Commit ad95b7c

Browse files
committed
Refactor Node Properties
1 parent 7bac0c7 commit ad95b7c

File tree

4 files changed

+71
-48
lines changed

4 files changed

+71
-48
lines changed

lib/neography/rest.rb

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Rest
4545
include Constraints
4646
include Transactions
4747
include Nodes
48+
include NodeProperties
4849
extend Forwardable
4950

5051
attr_reader :connection
@@ -54,7 +55,6 @@ class Rest
5455
def initialize(options = ENV['NEO4J_URL'] || {})
5556
@connection = Connection.new(options)
5657

57-
@node_properties ||= NodeProperties.new(@connection)
5858
@node_relationships ||= NodeRelationships.new(@connection)
5959
@other_node_relationships ||= OtherNodeRelationships.new(@connection)
6060
@node_indexes ||= NodeIndexes.new(@connection)
@@ -91,23 +91,6 @@ def delete_node!(id)
9191
# get("/nodes/")
9292
# end
9393

94-
# node properties
95-
96-
def get_node_properties(id, *properties)
97-
@node_properties.get(id, *properties.flatten)
98-
end
99-
100-
def set_node_properties(id, properties)
101-
@node_properties.set(id, properties)
102-
end
103-
104-
def reset_node_properties(id, properties)
105-
@node_properties.reset(id, properties)
106-
end
107-
108-
def remove_node_properties(id, *properties)
109-
@node_properties.remove(id, *properties.flatten)
110-
end
11194

11295
# relationships
11396

lib/neography/rest/batch.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,19 @@ def remove_node_from_index(index, key_or_id, value_or_id = nil, id = nil)
103103
# NodeProperties
104104

105105
def set_node_property(id, property)
106-
put NodeProperties.single_path(:id => get_id(id), :property => property.keys.first) do
106+
put "/node/%{id}/properties/%{property}" % {:id => get_id(id), :property => property.keys.first} do
107107
property.values.first
108108
end
109109
end
110110

111111
def reset_node_properties(id, body)
112-
put NodeProperties.all_path(:id => get_id(id)) do
112+
put "/node/%{id}/properties" % {:id => get_id(id)} do
113113
body
114114
end
115115
end
116116

117117
def remove_node_property(id, property)
118-
delete NodeProperties.single_path(:id => get_id(id), :property => property)
118+
delete "/node/%{id}/properties/%{property}" % {:id => get_id(id), :property => property}
119119
end
120120

121121
# NodeLabel

lib/neography/rest/node_properties.rb

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
module Neography
22
class Rest
3-
class NodeProperties < Properties
4-
extend Neography::Rest::Paths
3+
module NodeProperties
4+
5+
def set_node_properties(id, properties)
6+
properties.each do |property, value|
7+
options = { :body => value.to_json, :headers => json_content_type }
8+
@connection.put("/node/%{id}/properties/%{property}" % {:id => get_id(id), :property => property}, options)
9+
end
10+
end
11+
12+
def reset_node_properties(id, properties)
13+
options = { :body => properties.to_json, :headers => json_content_type }
14+
@connection.put("/node/%{id}/properties" % {:id => get_id(id)}, options)
15+
end
16+
17+
def get_node_properties(id, *properties)
18+
if properties.none?
19+
@connection.get("/node/%{id}/properties" % {:id => get_id(id)})
20+
else
21+
get_each_node_properties(id, *properties)
22+
end
23+
end
24+
25+
def get_each_node_properties(id, *properties)
26+
retrieved_properties = properties.flatten.inject({}) do |memo, property|
27+
value = @connection.get("/node/%{id}/properties/%{property}" % {:id => get_id(id), :property => property})
28+
memo[property] = value unless value.nil?
29+
memo
30+
end
31+
return nil if retrieved_properties.empty?
32+
retrieved_properties
33+
end
34+
35+
def remove_node_properties(id, *properties)
36+
if properties.none?
37+
@connection.delete("/node/%{id}/properties" % {:id => get_id(id)})
38+
else
39+
remove_each_node_properties(id, *properties)
40+
end
41+
end
42+
43+
def remove_each_node_properties(id, *properties)
44+
properties.flatten.each do |property|
45+
@connection.delete("/node/%{id}/properties/%{property}" % {:id => get_id(id), :property => property})
46+
end
47+
end
548

6-
add_path :all, "/node/:id/properties"
7-
add_path :single, "/node/:id/properties/:property"
849

950
end
1051
end

spec/unit/rest/node_properties_spec.rb

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ module Neography
44
class Rest
55
describe NodeProperties do
66

7-
let(:connection) { double }
8-
subject { NodeProperties.new(connection) }
7+
subject { Neography::Rest.new }
98

109
it "sets properties" do
1110
options1 = {
@@ -16,61 +15,61 @@ class Rest
1615
:body => '"qux"',
1716
:headers => json_content_type
1817
}
19-
connection.should_receive(:put).with("/node/42/properties/foo", options1)
20-
connection.should_receive(:put).with("/node/42/properties/baz", options2)
21-
subject.set("42", {:foo => "bar", :baz => "qux"})
18+
subject.connection.should_receive(:put).with("/node/42/properties/foo", options1)
19+
subject.connection.should_receive(:put).with("/node/42/properties/baz", options2)
20+
subject.set_node_properties("42", {:foo => "bar", :baz => "qux"})
2221
end
2322

2423
it "resets properties" do
2524
options = {
2625
:body => '{"foo":"bar"}',
2726
:headers => json_content_type
2827
}
29-
connection.should_receive(:put).with("/node/42/properties", options)
30-
subject.reset("42", {:foo => "bar"})
28+
subject.connection.should_receive(:put).with("/node/42/properties", options)
29+
subject.reset_node_properties("42", {:foo => "bar"})
3130
end
3231

3332
context "getting properties" do
3433

3534
it "gets all properties" do
36-
connection.should_receive(:get).with("/node/42/properties")
37-
subject.get("42")
35+
subject.connection.should_receive(:get).with("/node/42/properties")
36+
subject.get_node_properties("42")
3837
end
3938

4039
it "gets multiple properties" do
41-
connection.should_receive(:get).with("/node/42/properties/foo")
42-
connection.should_receive(:get).with("/node/42/properties/bar")
43-
subject.get("42", "foo", "bar")
40+
subject.connection.should_receive(:get).with("/node/42/properties/foo")
41+
subject.connection.should_receive(:get).with("/node/42/properties/bar")
42+
subject.get_node_properties("42", "foo", "bar")
4443
end
4544

4645
it "returns multiple properties as a hash" do
47-
connection.stub(:get).and_return("baz", "qux")
48-
subject.get("42", "foo", "bar").should == { "foo" => "baz", "bar" => "qux" }
46+
subject.connection.stub(:get).and_return("baz", "qux")
47+
subject.get_node_properties("42", "foo", "bar").should == { "foo" => "baz", "bar" => "qux" }
4948
end
5049

5150
it "returns nil if no properties were found" do
52-
connection.stub(:get).and_return(nil, nil)
53-
subject.get("42", "foo", "bar").should be_nil
51+
subject.connection.stub(:get).and_return(nil, nil)
52+
subject.get_node_properties("42", "foo", "bar").should be_nil
5453
end
5554

5655
it "returns hash without nil return values" do
57-
connection.stub(:get).and_return("baz", nil)
58-
subject.get("42", "foo", "bar").should == { "foo" => "baz" }
56+
subject.connection.stub(:get).and_return("baz", nil)
57+
subject.get_node_properties("42", "foo", "bar").should == { "foo" => "baz" }
5958
end
6059

6160
end
6261

6362
context "removing properties" do
6463

6564
it "removes all properties" do
66-
connection.should_receive(:delete).with("/node/42/properties")
67-
subject.remove("42")
65+
subject.connection.should_receive(:delete).with("/node/42/properties")
66+
subject.remove_node_properties("42")
6867
end
6968

7069
it "removes multiple properties" do
71-
connection.should_receive(:delete).with("/node/42/properties/foo")
72-
connection.should_receive(:delete).with("/node/42/properties/bar")
73-
subject.remove("42", "foo", "bar")
70+
subject.connection.should_receive(:delete).with("/node/42/properties/foo")
71+
subject.connection.should_receive(:delete).with("/node/42/properties/bar")
72+
subject.remove_node_properties("42", "foo", "bar")
7473
end
7574

7675
end

0 commit comments

Comments
 (0)