class ActiveGraph::Core::QueryClauses::Clause

Constants

AND
COMMA_SPACE
PRETTY_NEW_LINE
UNDERSCORE

Attributes

arg[RW]
options[R]
param_vars_added[R]
params[RW]

Public Class Methods

clause_color() click to toggle source
    # File lib/active_graph/core/query_clauses.rb
154 def clause_color
155   ANSI::CYAN
156 end
clause_join() click to toggle source
    # File lib/active_graph/core/query_clauses.rb
150 def clause_join
151   ''
152 end
clause_string(clauses, pretty) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
142 def clause_string(clauses, pretty)
143   join_string = pretty ? clause_join + PRETTY_NEW_LINE : clause_join
144 
145   strings = clause_strings(clauses)
146   stripped_string = strings.join(join_string).strip
147   pretty && strings.size > 1 ? PRETTY_NEW_LINE + stripped_string : stripped_string
148 end
from_arg(arg, params, options = {}) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
126 def from_arg(arg, params, options = {})
127   new(arg, params, options) if !arg.respond_to?(:empty?) || !arg.empty?
128 end
from_args(args, params, options = {}) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
121 def from_args(args, params, options = {})
122   args.flatten!
123   args.map { |arg| from_arg(arg, params, options) }.tap(&:compact!)
124 end
from_key_and_single_value(key, value) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
158 def from_key_and_single_value(key, value)
159   value.to_sym == :neo_id ? "ID(#{key})" : "#{key}.#{value}"
160 end
keyword() click to toggle source
    # File lib/active_graph/core/query_clauses.rb
113 def keyword
114   self::KEYWORD
115 end
keyword_downcase() click to toggle source
    # File lib/active_graph/core/query_clauses.rb
117 def keyword_downcase
118   keyword.downcase
119 end
new(arg, params, options = {}) click to toggle source
   # File lib/active_graph/core/query_clauses.rb
21 def initialize(arg, params, options = {})
22   @arg = arg
23   @options = options
24   @params = params
25   @param_vars_added = []
26 end
paramaterize_key!(key) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
163 def self.paramaterize_key!(key)
164   key.tr_s!('^a-zA-Z0-9', UNDERSCORE)
165   key.gsub!(/^_+|_+$/, '')
166 end
to_cypher(clauses, pretty = false) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
130 def to_cypher(clauses, pretty = false)
131   string = clause_string(clauses, pretty)
132 
133   final_keyword = if pretty
134                     "#{clause_color}#{keyword}#{ANSI::CLEAR}"
135                   else
136                     keyword
137                   end
138 
139   "#{final_keyword} #{string}" if !string.empty?
140 end

Public Instance Methods

_nested_value_hash?(value) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
102 def _nested_value_hash?(value)
103   value.values.any? { |v| v.is_a?(Hash) }
104 end
_use_key_for_var?(value, prefer) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
 98 def _use_key_for_var?(value, prefer)
 99   _nested_value_hash?(value) || prefer == :var
100 end
add_param(key, value) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
168 def add_param(key, value)
169   @param_vars_added << key
170   @params.add_param(key, value)
171 end
add_params(keys_and_values) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
173 def add_params(keys_and_values)
174   @param_vars_added += keys_and_values.keys
175   @params.add_params(keys_and_values)
176 end
attributes_from_key_and_value(_key, value) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
106 def attributes_from_key_and_value(_key, value)
107   return nil unless value.is_a?(Hash)
108 
109   value.values.map(&:class) == [Hash] ? value.first[1] : value
110 end
from_hash(value) click to toggle source
   # File lib/active_graph/core/query_clauses.rb
44 def from_hash(value)
45   fail ArgError if !respond_to?(:from_key_and_value)
46 
47   value.map do |k, v|
48     from_key_and_value k, v
49   end
50 end
from_string(value) click to toggle source
   # File lib/active_graph/core/query_clauses.rb
52 def from_string(value)
53   value
54 end
label_from_key_and_value(key, value, prefer = :var) click to toggle source
   # File lib/active_graph/core/query_clauses.rb
83 def label_from_key_and_value(key, value, prefer = :var)
84   case value
85   when String, Symbol, Array, NilClass then value
86   when Class, Module then value.name
87   when Hash
88     if value.values.map(&:class) == [Hash]
89       value.first.first
90     elsif !_use_key_for_var?(value, prefer)
91       key
92     end
93   else
94     fail ArgError, value
95   end
96 end
node_from_key_and_value(key, value, options = {}) click to toggle source
   # File lib/active_graph/core/query_clauses.rb
56 def node_from_key_and_value(key, value, options = {})
57   prefer = options[:prefer] || :var
58   var = var_from_key_and_value(key, value, prefer)
59   label = label_from_key_and_value(key, value, prefer)
60 
61   attributes = attributes_from_key_and_value(key, value)
62 
63   prefix_value = value
64   if value.is_a?(Hash)
65     prefix_value = (value.keys.join(UNDERSCORE) if value.values.any? { |v| v.is_a?(Hash) })
66   end
67 
68   prefix_array = [key, prefix_value].tap(&:compact!).join(UNDERSCORE)
69   formatted_attributes = attributes_string(attributes, "#{prefix_array}#{UNDERSCORE}")
70   "(#{var}#{format_label(label)}#{formatted_attributes})"
71 end
value() click to toggle source
   # File lib/active_graph/core/query_clauses.rb
28 def value
29   return @value if @value
30 
31   [String, Symbol, Integer, Hash, NilClass].each do |arg_class|
32     from_method = "from_#{arg_class.name.downcase}"
33     return @value = send(from_method, @arg) if @arg.is_a?(arg_class) && respond_to?(from_method)
34   end
35 
36   fail ArgError
37 rescue ArgError => arg_error
38   message = "Invalid argument for #{self.class.keyword}.  Full arguments: #{@arg.inspect}"
39   message += " | Invalid part: #{arg_error.arg_part.inspect}" if arg_error.arg_part
40 
41   raise ArgumentError, message
42 end
var_from_key_and_value(key, value, prefer = :var) click to toggle source
   # File lib/active_graph/core/query_clauses.rb
73 def var_from_key_and_value(key, value, prefer = :var)
74   case value
75   when String, Symbol, Class, Module, NilClass, Array then key
76   when Hash
77     key if _use_key_for_var?(value, prefer)
78   else
79     fail ArgError, value
80   end
81 end

Private Instance Methods

array_value?(value, is_set) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
200 def array_value?(value, is_set)
201   value.is_a?(Array) && !is_set
202 end
attributes_string(attributes, prefix = '') click to toggle source
    # File lib/active_graph/core/query_clauses.rb
215 def attributes_string(attributes, prefix = '')
216   return '' if not attributes
217 
218   attributes_string = attributes.map do |key, value|
219     if value.to_s =~ /^{.+}$/
220       "#{key}: #{value}"
221     else
222       param_key = "#{prefix}#{key}".gsub(/:+/, '_')
223       param_key = add_param(param_key, value)
224       "#{key}: $#{param_key}"
225     end
226   end.join(Clause::COMMA_SPACE)
227 
228   " {#{attributes_string}}"
229 end
format_label(label_arg) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
204 def format_label(label_arg)
205   return label_arg.map { |arg| format_label(arg) }.join if label_arg.is_a?(Array)
206 
207   label_arg = label_arg.to_s.strip
208   if !label_arg.empty? && label_arg[0] != ':'
209     label_arg = "`#{label_arg}`" unless label_arg[' ']
210     label_arg = ":#{label_arg}"
211   end
212   label_arg
213 end
key_value_string(key, value, previous_keys = [], is_set = false) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
180 def key_value_string(key, value, previous_keys = [], is_set = false)
181   param = (previous_keys << key).join(UNDERSCORE)
182   self.class.paramaterize_key!(param)
183 
184   if value.is_a?(Range)
185     range_key_value_string(key, value, previous_keys, param)
186   else
187     value = value.first if array_value?(value, is_set) && value.size == 1
188 
189     param = add_param(param, value)
190 
191     "#{key} #{array_value?(value, is_set) ? 'IN' : '='} $#{param}"
192   end
193 end
range_key_value_string(key, value, previous_keys, param) click to toggle source
    # File lib/active_graph/core/query_clauses.rb
195 def range_key_value_string(key, value, previous_keys, param)
196   begin_param, end_param = add_params("#{param}_range_min" => value.begin, "#{param}_range_max" => value.end)
197   "#{key} >= $#{begin_param} AND #{previous_keys[-2]}.#{key} <#{'=' unless value.exclude_end?} $#{end_param}"
198 end