Class: Neo4j::Core::QueryClauses::Clause

Inherits:
Object
  • Object
show all
Includes:
CypherTranslator
Defined in:
lib/neo4j-core/query_clauses.rb

Constant Summary collapse

UNDERSCORE =
'_'
COMMA_SPACE =
', '
AND =
' AND '

Constants included from CypherTranslator

CypherTranslator::EMPTY_PROPS, CypherTranslator::SANITIZE_ESCAPED_REGEXP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CypherTranslator

#create_escape_value, #cypher_prop_list!, #cypher_string, #escape_quotes, #escape_value, #label_string, #prop_identifier, #sanitize_escape_sequences, sanitized_column_names, translate_response

Constructor Details

#initialize(arg, options = {}) ⇒ Clause

Returns a new instance of Clause.



21
22
23
24
25
# File 'lib/neo4j-core/query_clauses.rb', line 21

def initialize(arg, options = {})
  @arg = arg
  @options = options
  @params = {}
end

Instance Attribute Details

#argObject

Returns the value of attribute arg.



19
20
21
# File 'lib/neo4j-core/query_clauses.rb', line 19

def arg
  @arg
end

#paramsObject

Returns the value of attribute params.



19
20
21
# File 'lib/neo4j-core/query_clauses.rb', line 19

def params
  @params
end

Class Method Details

.from_arg(arg, options = {}) ⇒ Object



130
131
132
# File 'lib/neo4j-core/query_clauses.rb', line 130

def from_arg(arg, options = {})
  new(arg, options) if !arg.respond_to?(:empty?) || !arg.empty?
end

.from_args(args, options = {}) ⇒ Object



125
126
127
128
# File 'lib/neo4j-core/query_clauses.rb', line 125

def from_args(args, options = {})
  args.flatten!
  args.map { |arg| from_arg(arg, options) }.tap(&:compact!)
end

.keywordObject



117
118
119
# File 'lib/neo4j-core/query_clauses.rb', line 117

def keyword
  self::KEYWORD
end

.keyword_downcaseObject



121
122
123
# File 'lib/neo4j-core/query_clauses.rb', line 121

def keyword_downcase
  keyword.downcase
end

.to_cypher(clauses) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/neo4j-core/query_clauses.rb', line 134

def to_cypher(clauses)
  @question_mark_param_index = 1

  string = clause_string(clauses)
  string.strip!

  "#{keyword} #{string}" if string.size > 0
end

Instance Method Details

#_nested_value_hash?(value) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/neo4j-core/query_clauses.rb', line 101

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

#_use_key_for_var?(value, prefer) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/neo4j-core/query_clauses.rb', line 97

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

#attributes_from_key_and_value(_key, value) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/neo4j-core/query_clauses.rb', line 106

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

  if value.values.map(&:class) == [Hash]
    value.first[1]
  else
    value
  end
end

#from_hash(value) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/neo4j-core/query_clauses.rb', line 41

def from_hash(value)
  if self.respond_to?(:from_key_and_value)
    value.map do |k, v|
      from_key_and_value k, v
    end
  else
    fail ArgError
  end
end

#from_string(value) ⇒ Object



51
52
53
# File 'lib/neo4j-core/query_clauses.rb', line 51

def from_string(value)
  value
end

#label_from_key_and_value(key, value, prefer = :var) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/neo4j-core/query_clauses.rb', line 82

def label_from_key_and_value(key, value, prefer = :var)
  case value
  when String, Symbol, Array, NilClass then value
  when Class, Module then value.name
  when Hash
    if value.values.map(&:class) == [Hash]
      value.first.first
    else
      key if !_use_key_for_var?(value, prefer)
    end
  else
    fail ArgError, value
  end
end

#node_from_key_and_value(key, value, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/neo4j-core/query_clauses.rb', line 55

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)
  attributes = attributes_from_key_and_value(key, value)

  prefix_value = value
  if value.is_a?(Hash)
    prefix_value = if value.values.any? { |v| v.is_a?(Hash) }
                     value.keys.join(UNDERSCORE)
                   end
  end

  prefix_array = [key, prefix_value].tap(&:compact!).join(UNDERSCORE)
  formatted_attributes = attributes_string(attributes, "#{prefix_array}#{UNDERSCORE}")
  "(#{var}#{format_label(label)}#{formatted_attributes})"
end

#valueObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/neo4j-core/query_clauses.rb', line 27

def value
  [String, Symbol, Integer, Hash].each do |arg_class|
    from_method = "from_#{arg_class.name.downcase}"
    return send(from_method, @arg) if @arg.is_a?(arg_class) && self.respond_to?(from_method)
  end

  fail ArgError
rescue ArgError => arg_error
  message = "Invalid argument for #{self.class.keyword}.  Full arguments: #{@arg.inspect}"
  message += " | Invalid part: #{arg_error.arg_part.inspect}" if arg_error.arg_part

  raise ArgumentError, message
end

#var_from_key_and_value(key, value, prefer = :var) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/neo4j-core/query_clauses.rb', line 72

def var_from_key_and_value(key, value, prefer = :var)
  case value
  when String, Symbol, Class, Module, NilClass, Array then key
  when Hash
    key if _use_key_for_var?(value, prefer)
  else
    fail ArgError, value
  end
end