Class: Neo4j::Core::QueryClauses::Clause
- Inherits:
-
Object
- Object
- Neo4j::Core::QueryClauses::Clause
show all
- Defined in:
- lib/neo4j-core/query_clauses.rb
Direct Known Subclasses
CallClause, CreateClause, DeleteClause, LimitClause, MatchClause, OrderClause, RemoveClause, ReturnClause, SetClause, SkipClause, StartClause, UnwindClause, UsingClause, WhereClause, WithClause
Constant Summary
collapse
- UNDERSCORE =
'_'
- COMMA_SPACE =
', '
- AND =
' AND '
- PRETTY_NEW_LINE =
"\n "
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#_nested_value_hash?(value) ⇒ Boolean
-
#_use_key_for_var?(value, prefer) ⇒ Boolean
-
#add_param(key, value) ⇒ Object
-
#add_params(keys_and_values) ⇒ Object
-
#attributes_from_key_and_value(_key, value) ⇒ Object
-
#from_hash(value) ⇒ Object
-
#from_string(value) ⇒ Object
-
#initialize(arg, params, options = {}) ⇒ Clause
constructor
A new instance of Clause.
-
#label_from_key_and_value(key, value, prefer = :var) ⇒ Object
-
#node_from_key_and_value(key, value, options = {}) ⇒ Object
-
#value ⇒ Object
-
#var_from_key_and_value(key, value, prefer = :var) ⇒ Object
Constructor Details
#initialize(arg, params, options = {}) ⇒ Clause
Returns a new instance of Clause.
21
22
23
24
25
26
|
# File 'lib/neo4j-core/query_clauses.rb', line 21
def initialize(arg, params, options = {})
@arg = arg
@options = options
@params = params
@param_vars_added = []
end
|
Instance Attribute Details
#arg ⇒ Object
Returns the value of attribute arg.
18
19
20
|
# File 'lib/neo4j-core/query_clauses.rb', line 18
def arg
@arg
end
|
#options ⇒ Object
Returns the value of attribute options.
19
20
21
|
# File 'lib/neo4j-core/query_clauses.rb', line 19
def options
@options
end
|
#param_vars_added ⇒ Object
Returns the value of attribute param_vars_added.
19
20
21
|
# File 'lib/neo4j-core/query_clauses.rb', line 19
def param_vars_added
@param_vars_added
end
|
#params ⇒ Object
Returns the value of attribute params.
18
19
20
|
# File 'lib/neo4j-core/query_clauses.rb', line 18
def params
@params
end
|
Class Method Details
.clause_color ⇒ Object
160
161
162
|
# File 'lib/neo4j-core/query_clauses.rb', line 160
def clause_color
ANSI::CYAN
end
|
.clause_join ⇒ Object
156
157
158
|
# File 'lib/neo4j-core/query_clauses.rb', line 156
def clause_join
''
end
|
.clause_string(clauses, pretty) ⇒ Object
147
148
149
150
151
152
153
154
|
# File 'lib/neo4j-core/query_clauses.rb', line 147
def clause_string(clauses, pretty)
join_string = pretty ? clause_join + PRETTY_NEW_LINE : clause_join
strings = clause_strings(clauses)
stripped_string = strings.join(join_string)
stripped_string.strip!
(pretty && strings.size > 1) ? PRETTY_NEW_LINE + stripped_string : stripped_string
end
|
.from_arg(arg, params, options = {}) ⇒ Object
131
132
133
|
# File 'lib/neo4j-core/query_clauses.rb', line 131
def from_arg(arg, params, options = {})
new(arg, params, options) if !arg.respond_to?(:empty?) || !arg.empty?
end
|
.from_args(args, params, options = {}) ⇒ Object
126
127
128
129
|
# File 'lib/neo4j-core/query_clauses.rb', line 126
def from_args(args, params, options = {})
args.flatten!
args.map { |arg| from_arg(arg, params, options) }.tap(&:compact!)
end
|
.from_key_and_single_value(key, value) ⇒ Object
164
165
166
|
# File 'lib/neo4j-core/query_clauses.rb', line 164
def from_key_and_single_value(key, value)
value.to_sym == :neo_id ? "ID(#{key})" : "#{key}.#{value}"
end
|
.keyword ⇒ Object
118
119
120
|
# File 'lib/neo4j-core/query_clauses.rb', line 118
def keyword
self::KEYWORD
end
|
.keyword_downcase ⇒ Object
122
123
124
|
# File 'lib/neo4j-core/query_clauses.rb', line 122
def keyword_downcase
keyword.downcase
end
|
.paramaterize_key!(key) ⇒ Object
169
170
171
172
|
# File 'lib/neo4j-core/query_clauses.rb', line 169
def self.paramaterize_key!(key)
key.tr_s!('^a-zA-Z0-9', UNDERSCORE)
key.gsub!(/^_+|_+$/, '')
end
|
.to_cypher(clauses, pretty = false) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/neo4j-core/query_clauses.rb', line 135
def to_cypher(clauses, pretty = false)
string = clause_string(clauses, pretty)
final_keyword = if pretty
"#{clause_color}#{keyword}#{ANSI::CLEAR}"
else
keyword
end
"#{final_keyword} #{string}" if !string.empty?
end
|
Instance Method Details
#_nested_value_hash?(value) ⇒ Boolean
106
107
108
|
# File 'lib/neo4j-core/query_clauses.rb', line 106
def _nested_value_hash?(value)
value.values.any? { |v| v.is_a?(Hash) }
end
|
#_use_key_for_var?(value, prefer) ⇒ Boolean
102
103
104
|
# File 'lib/neo4j-core/query_clauses.rb', line 102
def _use_key_for_var?(value, prefer)
_nested_value_hash?(value) || prefer == :var
end
|
#add_param(key, value) ⇒ Object
174
175
176
177
|
# File 'lib/neo4j-core/query_clauses.rb', line 174
def add_param(key, value)
@param_vars_added << key
@params.add_param(key, value)
end
|
#add_params(keys_and_values) ⇒ Object
179
180
181
182
|
# File 'lib/neo4j-core/query_clauses.rb', line 179
def add_params(keys_and_values)
@param_vars_added += keys_and_values.keys
@params.add_params(keys_and_values)
end
|
#attributes_from_key_and_value(_key, value) ⇒ Object
111
112
113
114
115
|
# File 'lib/neo4j-core/query_clauses.rb', line 111
def attributes_from_key_and_value(_key, value)
return nil unless value.is_a?(Hash)
value.values.map(&:class) == [Hash] ? value.first[1] : value
end
|
#from_hash(value) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/neo4j-core/query_clauses.rb', line 44
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
54
55
56
|
# File 'lib/neo4j-core/query_clauses.rb', line 54
def from_string(value)
value
end
|
#label_from_key_and_value(key, value, prefer = :var) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/neo4j-core/query_clauses.rb', line 87
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
elsif !_use_key_for_var?(value, prefer)
key
end
else
fail ArgError, value
end
end
|
#node_from_key_and_value(key, value, options = {}) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/neo4j-core/query_clauses.rb', line 58
def node_from_key_and_value(key, value, options = {})
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
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
|
#value ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/neo4j-core/query_clauses.rb', line 28
def value
return @value if @value
[String, Symbol, Integer, Hash, NilClass].each do |arg_class|
from_method = "from_#{arg_class.name.downcase}"
return @value = 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
77
78
79
80
81
82
83
84
85
|
# File 'lib/neo4j-core/query_clauses.rb', line 77
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
|