From c2b5fb32427292740ecfdb2c8722c008561ee6f2 Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Thu, 27 Mar 2014 03:46:36 -0500 Subject: [PATCH] Refactor Node Auto Indexes --- lib/neography/rest.rb | 32 +----------- lib/neography/rest/node_auto_indexes.rb | 62 +++++++++++++++++++++--- spec/unit/rest/node_auto_indexes_spec.rb | 47 +++++++++--------- 3 files changed, 78 insertions(+), 63 deletions(-) diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 13b6673..946f6ed 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -50,6 +50,7 @@ class Rest include NodeRelationships include OtherNodeRelationships include NodeIndexes + include NodeAutoIndexes extend Forwardable attr_reader :connection @@ -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) @@ -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 diff --git a/lib/neography/rest/node_auto_indexes.rb b/lib/neography/rest/node_auto_indexes.rb index 1bb387a..ae7ec85 100644 --- a/lib/neography/rest/node_auto_indexes.rb +++ b/lib/neography/rest/node_auto_indexes.rb @@ -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 diff --git a/spec/unit/rest/node_auto_indexes_spec.rb b/spec/unit/rest/node_auto_indexes_spec.rb index c4d3498..8a5f8fa 100644 --- a/spec/unit/rest/node_auto_indexes_spec.rb +++ b/spec/unit/rest/node_auto_indexes_spec.rb @@ -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