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

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

Constant Summary

Constants included from CypherTranslator

CypherTranslator::EMPTY_PROPS, CypherTranslator::SANITIZE_ESCAPED_REGEXP

Class Attribute Summary collapse

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.



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

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

Class Attribute Details

.keywordObject (readonly)

Returns the value of attribute keyword.



103
104
105
# File 'lib/neo4j-core/query_clauses.rb', line 103

def keyword
  @keyword
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



16
17
18
# File 'lib/neo4j-core/query_clauses.rb', line 16

def params
  @params
end

Class Method Details

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



105
106
107
108
109
# File 'lib/neo4j-core/query_clauses.rb', line 105

def from_args(args, options = {})
  args.flatten.map do |arg|
    new(arg, options) if !arg.respond_to?(:empty?) || !arg.empty?
  end.compact
end

.to_cypher(clauses) ⇒ Object



111
112
113
114
115
116
# File 'lib/neo4j-core/query_clauses.rb', line 111

def to_cypher(clauses)
  string = clause_string(clauses)
  string.strip!

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

Instance Method Details

#attributes_from_key_and_value(key, value) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/neo4j-core/query_clauses.rb', line 92

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



38
39
40
41
42
43
44
45
46
# File 'lib/neo4j-core/query_clauses.rb', line 38

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



48
49
50
# File 'lib/neo4j-core/query_clauses.rb', line 48

def from_string(value)
  value
end

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/neo4j-core/query_clauses.rb', line 75

def label_from_key_and_value(key, value, prefer = :var)
  case value
  when String, Symbol
    value
  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
    end
  else
    fail ArgError, value
  end
end

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



52
53
54
55
56
57
58
# File 'lib/neo4j-core/query_clauses.rb', line 52

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)

  "(#{var}#{format_label(label)}#{attributes_string(attributes)})"
end

#valueObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/neo4j-core/query_clauses.rb', line 24

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/neo4j-core/query_clauses.rb', line 60

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