From 24dd56458684287a4d2629795001899a1debc8fc Mon Sep 17 00:00:00 2001 From: Roel van Dijk Date: Tue, 18 Sep 2012 17:15:54 +0200 Subject: [PATCH] Remove delegation (less magic is cleaner). --- Gemfile.lock | 2 +- lib/neography.rb | 1 - lib/neography/rest.rb | 382 ++++++++++++++++++++++--------- lib/neography/rest/delegation.rb | 15 -- 4 files changed, 272 insertions(+), 128 deletions(-) delete mode 100644 lib/neography/rest/delegation.rb diff --git a/Gemfile.lock b/Gemfile.lock index db63124..d62e395 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - neography (0.0.30) + neography (0.0.31) httparty (>= 0.8.1) json multi_json diff --git a/lib/neography.rb b/lib/neography.rb index 7dd6cc6..7661e95 100644 --- a/lib/neography.rb +++ b/lib/neography.rb @@ -52,7 +52,6 @@ def find_and_require_user_defined_code require 'neography/rest/gremlin' require 'neography/rest/batch' require 'neography/rest/clean' -require 'neography/rest/delegation' require 'neography/connection' require 'neography/rest' diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index bd428e2..31ac33f 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -5,7 +5,6 @@ module Neography class Rest include HTTParty include Helpers - extend Delegation extend Forwardable attr_reader :connection @@ -16,116 +15,34 @@ def initialize(options = ENV['NEO4J_URL'] || {}) @connection = Connection.new(options) end - def_rest_delegations :nodes => { - :get_root => :root, - :create_node => :create, - :create_nodes => :create_multiple, - :create_nodes_threaded => :create_multiple_threaded, - :get_node => :get, - :get_nodes => :get_each, - :delete_node => :delete, - } - - def_rest_delegations :node_properties => { - :reset_node_properties => :reset, - :get_node_properties => :get, - :remove_node_properties => :remove, - :set_node_properties => :set - } - - def_rest_delegations :relationships => { - :get_relationship => :get, - :delete_relationship => :delete - } - - def_rest_delegations :relationship_properties => { - :reset_relationship_properties => :reset, - :get_relationship_properties => :get, - :remove_relationship_properties => :remove, - :set_relationship_properties => :set - } - - def_rest_delegations :node_relationships => { - :create_relationship => :create, - :get_node_relationships => :get - } - - def_rest_delegations :node_indexes => { - :list_node_indexes => :list, - :create_node_index => :create, - :create_node_auto_index => :create_auto, - :create_unique_node => :create_unique, - :add_node_to_index => :add, - :remove_node_from_index => :remove, - :get_node_index => :get, - :find_node_index => :find - } + # nodes - alias_method :list_indexes, :list_node_indexes - alias_method :add_to_index, :add_node_to_index - alias_method :remove_from_index, :remove_node_from_index - alias_method :get_index, :get_node_index + def get_root + nodes.root + end - def_rest_delegations :node_auto_indexes => { - :get_node_auto_index => :get, - :find_node_auto_index => :find_or_query, - :get_node_auto_index_status => :status, - :set_node_auto_index_status => :status=, - :get_node_auto_index_properties => :properties, - :add_node_auto_index_property => :add_property, - :remove_node_auto_index_property => :remove_property - } - - def_rest_delegations :relationship_indexes => { - :list_relationship_indexes => :list, - :create_relationship_index => :create, - :create_relationship_auto_index => :create_auto, - :create_unique_relationship => :create_unique, - :add_relationship_to_index => :add, - :remove_relationship_from_index => :remove, - :get_relationship_index => :get, - :find_relationship_index => :find - } - - def_rest_delegations :relationship_auto_indexes => { - :get_relationship_auto_index => :get, - :find_relationship_auto_index => :find_or_query, - :get_relationship_auto_index_status => :status, - :set_relationship_auto_index_status => :status=, - :get_relationship_auto_index_properties => :properties, - :add_relationship_auto_index_property => :add_property, - :remove_relationship_auto_index_property => :remove_property - } - - def_rest_delegations :node_paths => { - :get_path => :get, - :get_paths => :get_all, - :get_shortest_weighted_path => :shortest_weighted - } - - def_rest_delegations :batch_rest => { - :batch => :execute, - :batch_not_streaming => :not_streaming - } + def get_node(id) + nodes.get(id) + end - # traversal - def_delegator :node_traversal, :traverse, :traverse + def get_nodes(*args) + nodes.get_each(*args) + end - # cypher query - def_delegator :cypher, :query, :execute_query + def create_node(*args) + nodes.create(*args) + end - # gremlin script - def_delegator :gremlin, :execute, :execute_script + def create_nodes(args) + nodes.create_multiple(args) + end - # For testing (use a separate neo4j instance) - # call this before each test or spec - def clean_database(sanity_check = "not_really") - if sanity_check == "yes_i_really_want_to_clean_the_database" - @clean.execute - true - else - false - end + def create_nodes_threaded(args) + nodes.create_multiple_threaded(args) + end + + def delete_node(id) + nodes.delete(id) end def delete_node!(id) @@ -137,6 +54,41 @@ def delete_node!(id) delete_node(id) end + # This is not yet implemented in the REST API + # + # def get_all_node + # puts "get all nodes" + # 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 + + def get_relationship(id) + relationships.get(id) + end + + def delete_relationship(id) + relationships.delete(id) + end + def get_relationship_start_node(rel) get_node(rel["start"]) end @@ -145,12 +97,220 @@ def get_relationship_end_node(rel) get_node(rel["end"]) end - # This is not yet implemented in the REST API - # - # def get_all_node - # puts "get all nodes" - # get("/nodes/") - # end + # relationship properties + + def get_relationship_properties(id, *properties) + relationship_properties.get(id, *properties.flatten) + end + + def set_relationship_properties(id, properties) + relationship_properties.set(id, properties) + end + + def reset_relationship_properties(id, properties) + relationship_properties.reset(id, properties) + end + + def remove_relationship_properties(id, *properties) + relationship_properties.remove(id, *properties.flatten) + end + + # node relationships + + def get_node_relationships(id, dir = nil, types = nil) + node_relationships.get(id, dir, types) + end + + def create_relationship(type, from, to, props = nil) + node_relationships.create(type, from, to, props) + end + + # node indexes + + def list_node_indexes + node_indexes.list + end + alias_method :list_indexes, :list_node_indexes + + def create_node_index(name, type = "exact", provider = "lucene") + node_indexes.create(name, type, provider) + end + + def create_node_auto_index(type = "exact", provider = "lucene") + node_indexes.create_auto(type, provider) + end + + def create_unique_node(index, key, value, props={}) + node_indexes.create_unique(index, key, value, props) + end + + def add_node_to_index(index, key, value, id) + node_indexes.add(index, key, value, id) + end + alias_method :add_to_index, :add_node_to_index + + 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 + + def get_node_index(index, key, value) + node_indexes.get(index, key, value) + end + alias_method :get_index, :get_node_index + + def find_node_index(index, key_or_query, value = nil) + node_indexes.find(index, key_or_query, value) + 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 + relationship_indexes.list + end + + def create_relationship_index(name, type = "exact", provider = "lucene") + relationship_indexes.create(name, type, provider) + end + + def create_relationship_auto_index(type = "exact", provider = "lucene") + relationship_indexes.create_auto(type, provider) + end + + def create_unique_relationship(index, key, value, type, from, to) + relationship_indexes.create_unique(index, key, value, type, from, to) + end + + def add_relationship_to_index(index, key, value, id) + relationship_indexes.add(index, key, value, id) + 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(index, key_or_query, value = nil) + relationship_indexes.find(index, key_or_query, value) + end + + # relationship auto indexes + + def get_relationship_auto_index(key, value) + relationship_auto_indexes.get(key, value) + end + + 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 + relationship_auto_indexes.status + end + + def set_relationship_auto_index_status(change_to = true) + relationship_auto_indexes.status = change_to + end + + def get_relationship_auto_index_properties + relationship_auto_indexes.properties + end + + def add_relationship_auto_index_property(property) + relationship_auto_indexes.add_property(property) + end + + def remove_relationship_auto_index_property(property) + relationship_auto_indexes.remove_property(property) + end + + # traversal + + def traverse(id, return_type, description) + node_traversal.traverse(id, return_type, description) + end + + # paths + + def get_path(from, to, relationships, depth = 1, algorithm = "shortestPath") + node_paths.get(from, to, relationships, depth, algorithm) + end + + def get_paths(from, to, relationships, depth = 1, algorithm = "allPaths") + node_paths.get_all(from, to, relationships, depth, algorithm) + end + + def get_shortest_weighted_path(from, to, relationships, weight_attr = "weight", depth = 1, algorithm = "dijkstra") + node_paths.shortest_weighted(from, to, relationships, weight_attr, depth, algorithm) + end + + # cypher query + + def execute_query(query, params = {}) + cypher.query(query, params) + end + + # gremlin script + + def execute_script(script, params = {}) + gremlin.execute(script, params) + end + + # batch + + def batch(*args) + batch_rest.execute(*args) + end + + def batch_not_streaming(*args) + batch_rest.not_streaming(*args) + end + + # clean database + + # For testing (use a separate neo4j instance) + # call this before each test or spec + def clean_database(sanity_check = "not_really") + if sanity_check == "yes_i_really_want_to_clean_the_database" + clean.execute + true + else + false + end + end private diff --git a/lib/neography/rest/delegation.rb b/lib/neography/rest/delegation.rb deleted file mode 100644 index 1596132..0000000 --- a/lib/neography/rest/delegation.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Neography - class Rest - module Delegation - - def def_rest_delegations(options) - options.each do |target_class, delegations| - delegations.each do |from_method, to_method| - def_delegator target_class, to_method, from_method - end - end - end - - end - end -end