From bbe019c24def91a192a1e25375dd8452d23bddb5 Mon Sep 17 00:00:00 2001 From: Roel van Dijk Date: Sat, 8 Sep 2012 16:26:53 +0200 Subject: [PATCH] Rubified some code. --- lib/neography/connection.rb | 30 +++++++------ lib/neography/node_traverser.rb | 44 ++++++++++--------- lib/neography/rest.rb | 8 ++-- lib/neography/rest/node_auto_indexes.rb | 6 +-- lib/neography/rest/node_indexes.rb | 6 +-- lib/neography/rest/node_paths.rb | 6 +-- lib/neography/rest/node_relationships.rb | 4 +- lib/neography/rest/node_traversal.rb | 2 +- lib/neography/rest/nodes.rb | 6 +-- .../rest/relationship_auto_indexes.rb | 6 +-- lib/neography/rest/relationship_indexes.rb | 6 +-- lib/neography/rest/relationship_properties.rb | 42 +++++++++++------- 12 files changed, 90 insertions(+), 76 deletions(-) diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index fc33bfd..0b41639 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -11,14 +11,15 @@ class Connection :parser def initialize(options=ENV['NEO4J_URL'] || {}) - init = {:protocol => Neography::Config.protocol, - :server => Neography::Config.server, - :port => Neography::Config.port, + init = { + :protocol => Neography::Config.protocol, + :server => Neography::Config.server, + :port => Neography::Config.port, :directory => Neography::Config.directory, :cypher_path => Neography::Config.cypher_path, - :gremlin_path => Neography::Config.gremlin_path, - :log_file => Neography::Config.log_file, - :log_enabled => Neography::Config.log_enabled, + :gremlin_path => Neography::Config.gremlin_path, + :log_file => Neography::Config.log_file, + :log_enabled => Neography::Config.log_enabled, :max_threads => Neography::Config.max_threads, :authentication => Neography::Config.authentication, :username => Neography::Config.username, @@ -28,13 +29,14 @@ def initialize(options=ENV['NEO4J_URL'] || {}) unless options.respond_to?(:each_pair) url = URI.parse(options) - options = Hash.new - options[:protocol] = url.scheme + "://" - options[:server] = url.host - options[:port] = url.port - options[:directory] = url.path - options[:username] = url.user - options[:password] = url.password + options = { + :protocol => url.scheme + "://", + :server => url.host, + :port => url.port, + :directory => url.path, + :username => url.user, + :password => url.password + } options[:authentication] = 'basic' unless url.user.nil? end @@ -50,7 +52,7 @@ def initialize(options=ENV['NEO4J_URL'] || {}) @log_enabled = init[:log_enabled] @logger = Logger.new(@log_file) if @log_enabled @max_threads = init[:max_threads] - @authentication = Hash.new + @authentication = {} @authentication = {"#{init[:authentication]}_auth".to_sym => {:username => init[:username], :password => init[:password]}} unless init[:authentication].empty? @parser = init[:parser] @user_agent = {"User-Agent" => USER_AGENT} diff --git a/lib/neography/node_traverser.rb b/lib/neography/node_traverser.rb index b88e6b8..18b1d46 100644 --- a/lib/neography/node_traverser.rb +++ b/lib/neography/node_traverser.rb @@ -59,16 +59,18 @@ def order(o) end def filter(body) - @filter = Hash.new - @filter["language"] = "javascript" - @filter["body"] = body + @filter = { + "language" => "javascript", + "body" => body + } self end def prune(body) - @prune = Hash.new - @prune["language"] = "javascript" - @prune["body"] = body + @prune = { + "language" => "javascript", + "body" => body + } self end @@ -79,9 +81,10 @@ def depth(d) end def include_start_node - @filter = Hash.new - @filter["language"] = "builtin" - @filter["name"] = "all" + @filter = { + "language" => "builtin", + "name" => "all" + } self end @@ -108,13 +111,14 @@ def each end def iterator - options = Hash.new - options["order"] = @order - options["uniqueness"] = @uniqueness - options["relationships"] = @relationships - options["prune evaluator"] = @prune unless @prune.nil? - options["return filter"] = @filter unless @filter.nil? - options["depth"] = @depth unless @depth.nil? + options = { + "order" => @order, + "uniqueness" => @uniqueness, + "relationships" => @relationships + } + options["prune evaluator"] = @prune unless @prune.nil? + options["return filter"] = @filter unless @filter.nil? + options["depth"] = @depth unless @depth.nil? if @relationships[0]["type"].empty? rels = @from.neo_server.get_node_relationships(@from, @relationships[0]["direction"]) @@ -124,11 +128,11 @@ def iterator when "out" rels.collect { |r| @from.neo_server.get_node(r["end"]) } #.uniq else - rels.collect { |r| + rels.collect { |r| if @from.neo_id == r["start"].split('/').last - @from.neo_server.get_node(r["end"]) + @from.neo_server.get_node(r["end"]) else - @from.neo_server.get_node(r["start"]) + @from.neo_server.get_node(r["start"]) end } #.uniq end @@ -139,4 +143,4 @@ def iterator end -end \ No newline at end of file +end diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index db25be4..87a408d 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -121,12 +121,12 @@ def reset_relationship_properties(id, properties) @relationship_properties.reset(id, properties) end - def get_relationship_properties(id, properties = nil) - @relationship_properties.get(id, properties) + def get_relationship_properties(id, *properties) + @relationship_properties.get(id, *properties.flatten) end - def remove_relationship_properties(id, properties = nil) - @relationship_properties.remove(id, properties) + def remove_relationship_properties(id, *properties) + @relationship_properties.remove(id, *properties.flatten) end def set_relationship_properties(id, properties) diff --git a/lib/neography/rest/node_auto_indexes.rb b/lib/neography/rest/node_auto_indexes.rb index e68afb8..ae752da 100644 --- a/lib/neography/rest/node_auto_indexes.rb +++ b/lib/neography/rest/node_auto_indexes.rb @@ -15,17 +15,17 @@ def initialize(connection) end def get(key, value) - index = @connection.get(key_value_path(:key => key, :value => value)) || Array.new + index = @connection.get(key_value_path(:key => key, :value => value)) || [] return nil if index.empty? index end def find(key, value) - @connection.get(key_value_path(:key => key, :value => value)) || Array.new + @connection.get(key_value_path(:key => key, :value => value)) || [] end def query(query_expression) - @connection.get(query_index_path(:query => query_expression)) || Array.new + @connection.get(query_index_path(:query => query_expression)) || [] end def status diff --git a/lib/neography/rest/node_indexes.rb b/lib/neography/rest/node_indexes.rb index 9d2130b..f8e638b 100644 --- a/lib/neography/rest/node_indexes.rb +++ b/lib/neography/rest/node_indexes.rb @@ -69,18 +69,18 @@ def add(index, key, value, id) end def get(index, key, value) - index = @connection.get(key_value_path(:index => index, :key => key, :value => value)) || Array.new + index = @connection.get(key_value_path(:index => index, :key => key, :value => value)) || [] return nil if index.empty? index end # TODO FIX BUG %20 def find_by_value(index, key, value) - @connection.get(key_value2_path(:index => index, :key => key, :value => value)) || Array.new + @connection.get(key_value2_path(:index => index, :key => key, :value => value)) || [] end def find_by_query(index, query) - @connection.get(query_path(:index => index, :query => query)) || Array.new + @connection.get(query_path(:index => index, :query => query)) || [] end def remove(index, id) diff --git a/lib/neography/rest/node_paths.rb b/lib/neography/rest/node_paths.rb index 28ddb58..97e0947 100644 --- a/lib/neography/rest/node_paths.rb +++ b/lib/neography/rest/node_paths.rb @@ -20,7 +20,7 @@ def get(from, to, relationships, depth, algorithm) }.to_json, :headers => json_content_type } - @connection.post(base_path(:id => get_id(from)), options) || Hash.new + @connection.post(base_path(:id => get_id(from)), options) || {} end def get_all(from, to, relationships, depth, algorithm) @@ -32,7 +32,7 @@ def get_all(from, to, relationships, depth, algorithm) }.to_json, :headers => json_content_type } - @connection.post(all_path(:id => get_id(from)), options) || Array.new + @connection.post(all_path(:id => get_id(from)), options) || [] end def shortest_weighted(from, to, relationships, weight_attribute, depth, algorithm) @@ -45,7 +45,7 @@ def shortest_weighted(from, to, relationships, weight_attribute, depth, algorith }.to_json, :headers => json_content_type } - @connection.post(all_path(:id => get_id(from)), options) || Hash.new + @connection.post(all_path(:id => get_id(from)), options) || {} end private diff --git a/lib/neography/rest/node_relationships.rb b/lib/neography/rest/node_relationships.rb index 76bf047..c90422d 100644 --- a/lib/neography/rest/node_relationships.rb +++ b/lib/neography/rest/node_relationships.rb @@ -28,9 +28,9 @@ def get(id, direction, types) direction = get_direction(direction) if types.nil? - node_relationships = @connection.get(direction_path(:id => get_id(id), :direction => direction)) || Array.new + node_relationships = @connection.get(direction_path(:id => get_id(id), :direction => direction)) || [] else - node_relationships = @connection.get(type_path(:id => get_id(id), :direction => direction, :types => Array(types).join('&'))) || Array.new + node_relationships = @connection.get(type_path(:id => get_id(id), :direction => direction, :types => Array(types).join('&'))) || [] end return nil if node_relationships.empty? node_relationships diff --git a/lib/neography/rest/node_traversal.rb b/lib/neography/rest/node_traversal.rb index 6cbd2ff..b6d6946 100644 --- a/lib/neography/rest/node_traversal.rb +++ b/lib/neography/rest/node_traversal.rb @@ -24,7 +24,7 @@ def traverse(id, return_type, description) type = get_type(return_type) - @connection.post(traversal_path(:id => get_id(id), :type => type), options) || Array.new + @connection.post(traversal_path(:id => get_id(id), :type => type), options) || [] end private diff --git a/lib/neography/rest/nodes.rb b/lib/neography/rest/nodes.rb index abd0bd7..3d95307 100644 --- a/lib/neography/rest/nodes.rb +++ b/lib/neography/rest/nodes.rb @@ -16,7 +16,7 @@ def get(id) end def get_each(*nodes) - gotten_nodes = Array.new + gotten_nodes = [] Array(nodes).flatten.each do |node| gotten_nodes << get(node) end @@ -50,7 +50,7 @@ def create_empty def create_multiple(nodes) nodes = Array.new(nodes) if nodes.kind_of? Fixnum - created_nodes = Array.new + created_nodes = [] nodes.each do |node| created_nodes << create(node) end @@ -85,7 +85,7 @@ def create_multiple_threaded(nodes) end end - created_nodes = Array.new + created_nodes = [] while created_nodes.size < nodes.size created_nodes << responses.pop diff --git a/lib/neography/rest/relationship_auto_indexes.rb b/lib/neography/rest/relationship_auto_indexes.rb index 04393b2..7b282bd 100644 --- a/lib/neography/rest/relationship_auto_indexes.rb +++ b/lib/neography/rest/relationship_auto_indexes.rb @@ -15,17 +15,17 @@ def initialize(connection) end def get(key, value) - index = @connection.get(key_value_path(:key => key, :value => value)) || Array.new + index = @connection.get(key_value_path(:key => key, :value => value)) || [] return nil if index.empty? index end def find(key, value) - @connection.get(key_value_path(:key => key, :value => value)) || Array.new + @connection.get(key_value_path(:key => key, :value => value)) || [] end def query(query_expression) - @connection.get(query_index_path(:query => query_expression)) || Array.new + @connection.get(query_index_path(:query => query_expression)) || [] end def status diff --git a/lib/neography/rest/relationship_indexes.rb b/lib/neography/rest/relationship_indexes.rb index 676f033..97f578a 100644 --- a/lib/neography/rest/relationship_indexes.rb +++ b/lib/neography/rest/relationship_indexes.rb @@ -71,17 +71,17 @@ def add(index, key, value, id) end def get(index, key, value) - index = @connection.get(key_value_path(:index => index, :key => key, :value => value)) || Array.new + index = @connection.get(key_value_path(:index => index, :key => key, :value => value)) || [] return nil if index.empty? index end def find_by_key_query(index, key, query) - @connection.get(key_query_path(:index => index, :key => key, :query => query)) || Array.new + @connection.get(key_query_path(:index => index, :key => key, :query => query)) || [] end def find_by_query(index, query) - @connection.get(query_path(:index => index, :query => query)) || Array.new + @connection.get(query_path(:index => index, :query => query)) || [] end def remove(index, id) diff --git a/lib/neography/rest/relationship_properties.rb b/lib/neography/rest/relationship_properties.rb index 0821eb2..14a82c1 100644 --- a/lib/neography/rest/relationship_properties.rb +++ b/lib/neography/rest/relationship_properties.rb @@ -11,32 +11,40 @@ def initialize(connection) @connection = connection end - def get(id, properties) - if properties.nil? + def reset(id, properties) + options = { :body => properties.to_json, :headers => json_content_type } + @connection.put(all_path(:id => get_id(id)), options) + end + + def get(id, *properties) + if properties.none? @connection.get(all_path(:id => get_id(id))) else - relationship_properties = Hash.new - Array(properties).each do |property| - value = @connection.get(single_path(:id => get_id(id), :property => property)) - relationship_properties[property] = value unless value.nil? - end - return nil if relationship_properties.empty? - relationship_properties + get_each(id, *properties) end end - def reset(id, properties) - options = { :body => properties.to_json, :headers => json_content_type } - @connection.put(all_path(:id => get_id(id)), options) + def get_each(id, *properties) + relationship_properties = properties.inject({}) do |memo, property| + value = @connection.get(single_path(:id => get_id(id), :property => property)) + memo[property] = value unless value.nil? + memo + end + return nil if relationship_properties.empty? + relationship_properties end - def remove(id, properties) - if properties.nil? + def remove(id, *properties) + if properties.none? @connection.delete(all_path(id: get_id(id))) else - Array(properties).each do |property| - @connection.delete(single_path(:id => get_id(id), :property => property)) - end + remove_each(id, *properties) + end + end + + def remove_each(id, *properties) + properties.each do |property| + @connection.delete(single_path(:id => get_id(id), :property => property)) end end