Skip to content

Commit

Permalink
Refactor Node Auto Indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent 790ff76 commit c2b5fb3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 63 deletions.
32 changes: 1 addition & 31 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Rest
include NodeRelationships
include OtherNodeRelationships
include NodeIndexes
include NodeAutoIndexes
extend Forwardable

attr_reader :connection
Expand All @@ -59,7 +60,6 @@ class Rest
def initialize(options = ENV['NEO4J_URL'] || {})
@connection = Connection.new(options)

@node_auto_indexes ||= NodeAutoIndexes.new(@connection)
@node_traversal ||= NodeTraversal.new(@connection)
@node_paths ||= NodePaths.new(@connection)

Expand Down Expand Up @@ -105,36 +105,6 @@ def get_relationship_end_node(rel)
get_node(rel["end"])
end

# auto node indexes

def get_node_auto_index(key, value)
@node_auto_indexes.get(key, value)
end

def find_node_auto_index(key_or_query, value = nil)
@node_auto_indexes.find_or_query(key_or_query, value)
end

def get_node_auto_index_status
@node_auto_indexes.status
end

def set_node_auto_index_status(change_to = true)
@node_auto_indexes.status = change_to
end

def get_node_auto_index_properties
@node_auto_indexes.properties
end

def add_node_auto_index_property(property)
@node_auto_indexes.add_property(property)
end

def remove_node_auto_index_property(property)
@node_auto_indexes.remove_property(property)
end

# relationship indexes

def list_relationship_indexes
Expand Down
62 changes: 54 additions & 8 deletions lib/neography/rest/node_auto_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,59 @@
module Neography
class Rest
class NodeAutoIndexes < AutoIndexes
extend Neography::Rest::Paths

add_path :key_value, "/index/auto/node/:key/:value"
add_path :query_index, "/index/auto/node/?query=:query"
add_path :index_status, "/index/auto/node/status"
add_path :index_properties, "/index/auto/node/properties"
add_path :index_property, "/index/auto/node/properties/:property"
module NodeAutoIndexes

def get_node_auto_index(key, value)
index = @connection.get("/index/auto/node/%{key}/%{value}" % {:key => key, :value => encode(value)}) || []
return nil if index.empty?
index
end

def find_node_auto_index(key_or_query, value = nil)
if value
index = find_node_auto_index_by_value(key_or_query, value)
else
index = query_node_auto_index(key_or_query)
end
return nil if index.empty?
index
end

def find_node_auto_index_by_value(key, value)
@connection.get("/index/auto/node/%{key}/%{value}" % {:key => key, :value => encode(value)}) || []
end

def query_node_auto_index(query_expression)
@connection.get("/index/auto/node/?query=%{query}" % {:query => query_expression}) || []
end

def get_node_auto_index_status
@connection.get("/index/auto/node/status")
end

def set_node_auto_index_status(value = true)
options = {
:body => value.to_json,
:headers => json_content_type
}
@connection.put("/index/auto/node/status", options)
end

def get_node_auto_index_properties
@connection.get("/index/auto/node/properties")
end

def add_node_auto_index_property(property)
options = {
:body => property,
:headers => json_content_type
}
@connection.post("/index/auto/node/properties", options)
end

def remove_node_auto_index_property(property)
@connection.delete("/index/auto/node/properties/%{property}" % {:property => property})
end


end
end
Expand Down
47 changes: 23 additions & 24 deletions spec/unit/rest/node_auto_indexes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,61 @@ module Neography
class Rest
describe NodeAutoIndexes do

let(:connection) { double }
subject { NodeAutoIndexes.new(connection) }
subject { Neography::Rest.new }

it "gets a node from an auto index" do
connection.should_receive(:get).with("/index/auto/node/some_key/some_value")
subject.get("some_key", "some_value")
subject.connection.should_receive(:get).with("/index/auto/node/some_key/some_value")
subject.get_node_auto_index("some_key", "some_value")
end

it "returns nil if nothing was found in the auto index" do
connection.stub(:get).and_return(nil)
subject.get("some_key", "some_value").should be_nil
subject.connection.stub(:get).and_return(nil)
subject.get_node_auto_index("some_key", "some_value").should be_nil
end

it "finds by key and value if value passed to #find_or_query" do
connection.should_receive(:get).with("/index/auto/node/some_key/some_value")
subject.find_or_query("some_key", "some_value")
subject.connection.should_receive(:get).with("/index/auto/node/some_key/some_value")
subject.find_node_auto_index("some_key", "some_value")
end

it "finds by query if no value passed to #find_or_query" do
connection.should_receive(:get).with("/index/auto/node/?query=some_query")
subject.find_or_query("some_query")
subject.connection.should_receive(:get).with("/index/auto/node/?query=some_query")
subject.find_node_auto_index("some_query")
end

it "finds by key and value" do
connection.should_receive(:get).with("/index/auto/node/some_key/some_value")
subject.find("some_key", "some_value")
subject.connection.should_receive(:get).with("/index/auto/node/some_key/some_value")
subject.find_node_auto_index("some_key", "some_value")
end

it "finds by query" do
connection.should_receive(:get).with("/index/auto/node/?query=some_query")
subject.query("some_query")
subject.connection.should_receive(:get).with("/index/auto/node/?query=some_query")
subject.find_node_auto_index("some_query")
end

it "gets the status" do
connection.should_receive(:get).with("/index/auto/node/status")
subject.status
subject.connection.should_receive(:get).with("/index/auto/node/status")
subject.get_node_auto_index_status
end

it "sets the status" do
connection.should_receive(:put).with("/index/auto/node/status", hash_match(:body, '"foo"'))
subject.status = "foo"
subject.connection.should_receive(:put).with("/index/auto/node/status", hash_match(:body, '"foo"'))
subject.set_node_auto_index_status("foo")
end

it "gets auto index properties" do
connection.should_receive(:get).with("/index/auto/node/properties")
subject.properties
subject.connection.should_receive(:get).with("/index/auto/node/properties")
subject.get_node_auto_index_properties
end

it "adds a property to an auto index" do
connection.should_receive(:post).with("/index/auto/node/properties", hash_match(:body, "foo"))
subject.add_property("foo")
subject.connection.should_receive(:post).with("/index/auto/node/properties", hash_match(:body, "foo"))
subject.add_node_auto_index_property("foo")
end

it "removes a property from an auto index" do
connection.should_receive(:delete).with("/index/auto/node/properties/foo")
subject.remove_property("foo")
subject.connection.should_receive(:delete).with("/index/auto/node/properties/foo")
subject.remove_node_auto_index_property("foo")
end

end
Expand Down

0 comments on commit c2b5fb3

Please sign in to comment.