From ce74097fda3cabd253b1d39b7262db841bf25542 Mon Sep 17 00:00:00 2001 From: Brian Underwood Date: Fri, 7 Aug 2015 17:54:43 -0300 Subject: [PATCH] Bringing Metrics/AbcSize down a notch --- .rubocop_todo.yml | 4 ++-- lib/neo4j-core/query_clauses.rb | 21 ++++++++++++++------- lib/neo4j-embedded/embedded_session.rb | 18 ++++++++++++------ lib/neo4j-server/cypher_node.rb | 13 ++++++++----- spec/neo4j-core/unit/query_spec.rb | 6 +++++- 5 files changed, 41 insertions(+), 21 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 55626cce..919626ca 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -14,9 +14,9 @@ Lint/HandleExceptions: Lint/UnusedMethodArgument: Enabled: false -# Offense count: 10 +# Offense count: 15 Metrics/AbcSize: - Max: 18 + Max: 17 # Offense count: 9 # Configuration parameters: CountComments. diff --git a/lib/neo4j-core/query_clauses.rb b/lib/neo4j-core/query_clauses.rb index b91bc5f3..d3106c0a 100644 --- a/lib/neo4j-core/query_clauses.rb +++ b/lib/neo4j-core/query_clauses.rb @@ -52,8 +52,10 @@ def from_string(value) end def node_from_key_and_value(key, value, options = {}) - var = var_from_key_and_value(key, value, options[:prefer] || :var) - label = label_from_key_and_value(key, value, options[:prefer] || :var) + prefer = options[:prefer] || :var + var = var_from_key_and_value(key, value, prefer) + label = label_from_key_and_value(key, value, prefer) + attributes = attributes_from_key_and_value(key, value) prefix_value = value @@ -133,20 +135,25 @@ def from_arg(arg, options = {}) def to_cypher(clauses, options = {}) @question_mark_param_index = 1 - join = clause_join + (options[:pretty] ? "\n " : '') - - strings = clause_strings(clauses) - string = ((options[:pretty] && strings.size > 1) ? "\n " : '') - string += strings.join(join).strip + string = clause_string(clauses, options) final_keyword = if options[:pretty] "#{clause_color}#{keyword}#{ANSI::CLEAR}" else keyword end + "#{final_keyword} #{string}" if string.size > 0 end + def clause_string(clauses, options = {}) + join_string = clause_join + (options[:pretty] ? "\n " : '') + + strings = clause_strings(clauses) + string = ((options[:pretty] && strings.size > 1) ? "\n " : '') + string + strings.join(join_string).strip + end + def clause_join '' end diff --git a/lib/neo4j-embedded/embedded_session.rb b/lib/neo4j-embedded/embedded_session.rb index 57c31acd..3ffd630e 100644 --- a/lib/neo4j-embedded/embedded_session.rb +++ b/lib/neo4j-embedded/embedded_session.rb @@ -16,16 +16,22 @@ class Error < StandardError def_delegator :@graph_db, :begin_tx def initialize(db_location, config = {}) + @config = config @db_location = db_location - @auto_commit = !!config[:auto_commit] - @properties_file = config[:properties_file] - if config[:properties_map] - props = config[:properties_map].each_with_object({}) { |(k, v), m| m[k.to_s.to_java] = v.to_s.to_java } - @properties_map = java.util.HashMap.new(props) - end + @auto_commit = !!@config[:auto_commit] + @properties_file = @config[:properties_file] Neo4j::Session.register(self) end + def properties_map + return @properties_map if @properties_map + + props = @config[:properties_map].each_with_object({}) do |(k, v), m| + m[k.to_s.to_java] = v.to_s.to_java + end + @properties_map = java.util.HashMap.new(props) + end + def db_type :embedded_db end diff --git a/lib/neo4j-server/cypher_node.rb b/lib/neo4j-server/cypher_node.rb index d4e37bad..0fe2b53b 100644 --- a/lib/neo4j-server/cypher_node.rb +++ b/lib/neo4j-server/cypher_node.rb @@ -112,8 +112,6 @@ def remove_label(*target_labels) end def set_label(*label_names) - q = match_start_query - labels_to_add = label_names.map(&:to_sym).uniq labels_to_remove = labels - label_names @@ -121,10 +119,15 @@ def set_label(*label_names) labels_to_add -= common_labels labels_to_remove -= common_labels - q = q.remove(n: labels_to_remove) unless labels_to_remove.empty? - q = q.set(n: labels_to_add) unless labels_to_add.empty? + query = _set_label_query(labels_to_add, labels_to_remove) + @session._query_or_fail(query, false) unless (labels_to_add + labels_to_remove).empty? + end - @session._query_or_fail(q, false) unless (labels_to_add + labels_to_remove).empty? + def _set_label_query(labels_to_add, labels_to_remove) + query = match_start_query + query = query.remove(n: labels_to_remove) unless labels_to_remove.empty? + query = query.set(n: labels_to_add) unless labels_to_add.empty? + query end # (see Neo4j::Node#del) diff --git a/spec/neo4j-core/unit/query_spec.rb b/spec/neo4j-core/unit/query_spec.rb index 42577df5..56558e5d 100644 --- a/spec/neo4j-core/unit/query_spec.rb +++ b/spec/neo4j-core/unit/query_spec.rb @@ -182,9 +182,13 @@ class Person end + def add_query_doc_line(cypher, params = {}) + @doc_generator.add_query_doc_line(self.class, self.class.description, cypher, params) + end + def expects_cypher(cypher, params = {}) query = eval("Neo4j::Core::Query.new#{self.class.description}") # rubocop:disable Lint/Eval - @doc_generator.add_query_doc_line(self.class, self.class.description, cypher, params) + add_query_doc_line(cypher, params) expect(query.to_cypher).to eq(cypher)