Skip to content

Commit

Permalink
Support Arrays for labels in ruby query method chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
cheerfulstoic committed Feb 20, 2015
1 parent da40727 commit 72f41e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
40 changes: 25 additions & 15 deletions lib/neo4j-core/query_clauses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,40 @@ def node_from_key_and_value(key, value, options = {})

def var_from_key_and_value(key, value, prefer = :var)
case value
when String, Symbol, Class, Module, NilClass
key
when String, Symbol, Class, Module, NilClass, Array then key
when Hash
key if value.values.any? { |v| v.is_a?(Hash) } || prefer == :var
key if _use_key_for_var?(value, prefer)
else
fail ArgError, value
end
end

def label_from_key_and_value(key, value, prefer = :var)
case value
when String, Symbol
value
when String, Symbol, Array then value
when NilClass then nil
when Class, Module
defined?(value::CYPHER_LABEL) ? value::CYPHER_LABEL : value.name
when Hash
if value.values.map(&:class) == [Hash]
value.first.first
else
key if value.values.none? { |v| v.is_a?(Hash) } && prefer == :label
key if !_use_key_for_var?(value, prefer)
end
when NilClass
nil
else
fail ArgError, value
end
end

def _use_key_for_var?(value, prefer)
_nested_value_hash?(value) || prefer == :var
end

def _nested_value_hash?(value)
value.values.any? { |v| v.is_a?(Hash) }
end


def attributes_from_key_and_value(key, value)
return nil unless value.is_a?(Hash)

Expand Down Expand Up @@ -129,14 +135,18 @@ def key_value_string(key, value, previous_keys = [], force_equals = false)
end
end

def format_label(label_string)
label_string = label_string.to_s
label_string.strip!
if !label_string.empty? && label_string[0] != ':'
label_string = "`#{label_string}`" unless label_string.match(' ')
label_string = ":#{label_string}"
def format_label(label_arg)
if label_arg.is_a?(Array)
return label_arg.map { |arg| format_label(arg) }.join
end

label_arg = label_arg.to_s
label_arg.strip!
if !label_arg.empty? && label_arg[0] != ':'
label_arg = "`#{label_arg}`" unless label_arg.match(' ')
label_arg = ":#{label_arg}"
end
label_string
label_arg
end

def attributes_string(attributes)
Expand Down
4 changes: 4 additions & 0 deletions spec/neo4j-core/unit/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ def self.it_generates(cypher, params = nil)
it_generates 'MATCH (n:`Person`)'
end

describe '.match(n: [:Person, "Animal"])' do
it_generates 'MATCH (n:`Person`:`Animal`)'
end

describe ".match(n: ' :Person')" do
it_generates 'MATCH (n:Person)'
end
Expand Down

0 comments on commit 72f41e7

Please sign in to comment.