From 8d3f2a79056bc991196fb5f2694abd45f39741b5 Mon Sep 17 00:00:00 2001 From: Roel van Dijk Date: Mon, 10 Sep 2012 18:12:02 +0200 Subject: [PATCH] Remove remaining logic to separate Rest classes. --- lib/neography/rest.rb | 52 +++++-------------- lib/neography/rest/auto_indexes.rb | 10 ++++ lib/neography/rest/indexes.rb | 23 +++++++- spec/unit/rest/node_auto_indexes_spec.rb | 10 ++++ spec/unit/rest/node_indexes_spec.rb | 27 +++++++++- ...s.rb => relationship_auto_indexes_spec.rb} | 10 ++++ spec/unit/rest/relationship_indexes_spec.rb | 27 +++++++++- 7 files changed, 116 insertions(+), 43 deletions(-) rename spec/unit/rest/{relationship_auto_indexes.rb => relationship_auto_indexes_spec.rb} (80%) diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index e6990fe..aad0297 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -167,12 +167,8 @@ def create_unique_node(index, key, value, props={}) @node_indexes.create_unique(index, key, value, props) end - def remove_node_from_index(*args) - case args.size - when 4 then @node_indexes.remove_by_value(args[0], args[3], args[1], args[2]) - when 3 then @node_indexes.remove_by_key(args[0], args[2], args[1]) - when 2 then @node_indexes.remove(args[0], args[1]) - end + def remove_node_from_index(index, id_or_key, id_or_value = nil, id = nil) + @node_indexes.remove(index, id_or_key, id_or_value, id) end alias_method :remove_from_index, :remove_node_from_index @@ -181,13 +177,8 @@ def get_node_index(index, key, value) end alias_method :get_index, :get_node_index - def find_node_index(*args) - case args.size - when 3 then index = @node_indexes.find_by_key_value(args[0], args[1], args[2]) - when 2 then index = @node_indexes.find_by_query(args[0], args[1]) - end - return nil if index.empty? - index + def find_node_index(index, key_or_query, value = nil) + @node_indexes.find(index, key_or_query, value) end # auto node indexes @@ -196,13 +187,8 @@ def get_node_auto_index(key, value) @node_auto_indexes.get(key, value) end - def find_node_auto_index(*args) - case args.size - when 2 then index = @node_auto_indexes.find(args[0], args[1]) - when 1 then index = @node_auto_indexes.query(args[0]) - end - return nil if index.empty? - index + 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 @@ -247,25 +233,16 @@ def add_relationship_to_index(index, key, value, id) @relationship_indexes.add(index, key, value, id) end - def remove_relationship_from_index(*args) - case args.size - when 4 then @relationship_indexes.remove_by_value(args[0], get_id(args[3]), args[1], args[2]) - when 3 then @relationship_indexes.remove_by_key(args[0], get_id(args[2]), args[1]) - when 2 then @relationship_indexes.remove(args[0], get_id(args[1])) - end + def remove_relationship_from_index(index, id_or_key, id_or_value = nil, id = nil) + @relationship_indexes.remove(index, id_or_key, id_or_value, id) end def get_relationship_index(index, key, value) @relationship_indexes.get(index, key, value) end - def find_relationship_index(*args) - case args.size - when 3 then index = @relationship_indexes.find_by_key_value(args[0], args[1], args[2]) - when 2 then index = @relationship_indexes.find_by_query(args[0], args[1]) - end - return nil if index.empty? - index + def find_relationship_index(index, key_or_query, value = nil) + @relationship_indexes.find(index, key_or_query, value) end # relationship auto indexes @@ -274,13 +251,8 @@ def get_relationship_auto_index(key, value) @relationship_auto_indexes.get(key, value) end - def find_relationship_auto_index(*args) - case args.size - when 2 then index = @relationship_auto_indexes.find(args[0], args[1]) - when 1 then index = @relationship_auto_indexes.query(args[0]) - end - return nil if index.empty? - index + def find_relationship_auto_index(key_or_query, value = nil) + @relationship_auto_indexes.find_or_query(key_or_query, value) end def get_relationship_auto_index_status diff --git a/lib/neography/rest/auto_indexes.rb b/lib/neography/rest/auto_indexes.rb index 891b1b8..bd2735e 100644 --- a/lib/neography/rest/auto_indexes.rb +++ b/lib/neography/rest/auto_indexes.rb @@ -13,6 +13,16 @@ def get(key, value) index end + def find_or_query(key_or_query, value = nil) + if value + index = find(key_or_query, value) + else + index = query(key_or_query) + end + return nil if index.empty? + index + end + def find(key, value) @connection.get(key_value_path(:key => key, :value => value)) || [] end diff --git a/lib/neography/rest/indexes.rb b/lib/neography/rest/indexes.rb index 8cd5664..4593f8a 100644 --- a/lib/neography/rest/indexes.rb +++ b/lib/neography/rest/indexes.rb @@ -51,6 +51,16 @@ def get(index, key, value) index end + def find(index, key_or_query, value = nil) + if value + index = find_by_key_value(index, key_or_query, value) + else + index = find_by_query(index, key_or_query) + end + return nil if index.empty? + index + end + def find_by_key_value(index, key, value) @connection.get(key_value_path(:index => index, :key => key, :value => value)) || [] end @@ -59,7 +69,18 @@ def find_by_query(index, query) @connection.get(query_path(:index => index, :query => query)) || [] end - def remove(index, id) + # Mimick original neography API in Rest class. + def remove(index, id_or_key, id_or_value = nil, id = nil) + if id + remove_by_value(index, id, id_or_key, id_or_value) + elsif id_or_value + remove_by_key(index, id_or_value, id_or_key) + else + remove_by_id(index, id_or_key) + end + end + + def remove_by_id(index, id) @connection.delete(id_path(:index => index, :id => get_id(id))) end diff --git a/spec/unit/rest/node_auto_indexes_spec.rb b/spec/unit/rest/node_auto_indexes_spec.rb index 8bd623e..3b76ed9 100644 --- a/spec/unit/rest/node_auto_indexes_spec.rb +++ b/spec/unit/rest/node_auto_indexes_spec.rb @@ -17,6 +17,16 @@ class Rest subject.get("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") + 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") + 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") diff --git a/spec/unit/rest/node_indexes_spec.rb b/spec/unit/rest/node_indexes_spec.rb index f50a0c1..2b5cc23 100644 --- a/spec/unit/rest/node_indexes_spec.rb +++ b/spec/unit/rest/node_indexes_spec.rb @@ -71,6 +71,16 @@ class Rest subject.get("some_index", "some_key", "some_value").should be_nil end + it "finds by key and value if both passed to #find" do + connection.should_receive(:get).with("/index/node/some_index/some_key/some_value") + subject.find("some_index", "some_key", "some_value") + end + + it "finds by query if no value passed to #find" do + connection.should_receive(:get).with("/index/node/some_index?query=some_query") + subject.find("some_index", "some_query") + end + it "finds by key and value" do connection.should_receive(:get).with("/index/node/some_index/some_key/some_value") subject.find_by_key_value("some_index", "some_key", "some_value") @@ -81,11 +91,26 @@ class Rest subject.find_by_query("some_index", "some_query") end - it "removes a node from an index" do + it "removes a node from an index by id for #remove with two arguments" do connection.should_receive(:delete).with("/index/node/some_index/42") subject.remove("some_index", "42") end + it "removes a node from an index by key for #remove with three arguments" do + connection.should_receive(:delete).with("/index/node/some_index/some_key/42") + subject.remove("some_index", "some_key", "42") + end + + it "removes a node from an index by key and value for #remove with four arguments" do + connection.should_receive(:delete).with("/index/node/some_index/some_key/some_value/42") + subject.remove("some_index", "some_key", "some_value", "42") + end + + it "removes a node from an index" do + connection.should_receive(:delete).with("/index/node/some_index/42") + subject.remove_by_id("some_index", "42") + end + it "removes a node from an index by key" do connection.should_receive(:delete).with("/index/node/some_index/some_key/42") subject.remove_by_key("some_index", "42", "some_key") diff --git a/spec/unit/rest/relationship_auto_indexes.rb b/spec/unit/rest/relationship_auto_indexes_spec.rb similarity index 80% rename from spec/unit/rest/relationship_auto_indexes.rb rename to spec/unit/rest/relationship_auto_indexes_spec.rb index 74528af..85158e4 100644 --- a/spec/unit/rest/relationship_auto_indexes.rb +++ b/spec/unit/rest/relationship_auto_indexes_spec.rb @@ -17,6 +17,16 @@ class Rest subject.get("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/relationship/some_key/some_value") + subject.find_or_query("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/relationship/?query=some_query") + subject.find_or_query("some_query") + end + it "finds by key and value" do connection.should_receive(:get).with("/index/auto/relationship/some_key/some_value") subject.find("some_key", "some_value") diff --git a/spec/unit/rest/relationship_indexes_spec.rb b/spec/unit/rest/relationship_indexes_spec.rb index 4db7988..2cfb548 100644 --- a/spec/unit/rest/relationship_indexes_spec.rb +++ b/spec/unit/rest/relationship_indexes_spec.rb @@ -73,6 +73,16 @@ class Rest subject.get("some_index", "some_key", "some_value").should be_nil end + it "finds by key and value if both passed to #find" do + connection.should_receive(:get).with("/index/relationship/some_index/some_key/some_value") + subject.find("some_index", "some_key", "some_value") + end + + it "finds by query if no value passed to #find" do + connection.should_receive(:get).with("/index/relationship/some_index?query=some_query") + subject.find("some_index", "some_query") + end + it "finds by key query" do connection.should_receive(:get).with("/index/relationship/some_index/some_key/some_value") subject.find_by_key_value("some_index", "some_key", "some_value") @@ -83,11 +93,26 @@ class Rest subject.find_by_query("some_index", "some_query") end - it "removes a relationship from an index" do + it "removes a relationship from an index for #remove with two arguments" do connection.should_receive(:delete).with("/index/relationship/some_index/42") subject.remove("some_index", "42") end + it "removes a relationship from an index by key for #remove with three arguments" do + connection.should_receive(:delete).with("/index/relationship/some_index/some_key/42") + subject.remove("some_index", "some_key", "42") + end + + it "removes a relationship from an index by key and value for #remove with four arguments" do + connection.should_receive(:delete).with("/index/relationship/some_index/some_key/some_value/42") + subject.remove("some_index", "some_key", "some_value", "42") + end + + it "removes a relationship from an index" do + connection.should_receive(:delete).with("/index/relationship/some_index/42") + subject.remove_by_id("some_index", "42") + end + it "removes a relationship from an index by key" do connection.should_receive(:delete).with("/index/relationship/some_index/some_key/42") subject.remove_by_key("some_index", "42", "some_key")