Skip to content

Commit

Permalink
Bringing Metrics/AbcSize down a notch
Browse files Browse the repository at this point in the history
  • Loading branch information
cheerfulstoic committed Aug 7, 2015
1 parent ccd9850 commit ce74097
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Expand Up @@ -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.
Expand Down
21 changes: 14 additions & 7 deletions lib/neo4j-core/query_clauses.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions lib/neo4j-embedded/embedded_session.rb
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions lib/neo4j-server/cypher_node.rb
Expand Up @@ -112,19 +112,22 @@ 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

common_labels = labels & labels_to_add
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)
Expand Down
6 changes: 5 additions & 1 deletion spec/neo4j-core/unit/query_spec.rb
Expand Up @@ -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)

Expand Down

0 comments on commit ce74097

Please sign in to comment.