Class: Lockstep::Query

Inherits:
Object
  • Object
show all
Defined in:
app/concepts/lockstep/query.rb

Constant Summary collapse

PREDICATES =
{
  _not_eq: "NE",
  _eq: "EQ",
  _gteq: "GE",
  _gt: "GT",
  _lteq: "LE",
  _lt: "LT",
  _in: "IN",
  _contains: "CONTAINS",
  _starts_with: "STARTSWITH",
  _ends_with: "ENDSWITH",
  _is: "IS",
}.with_indifferent_access

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Query

Returns a new instance of Query.



2
3
4
# File 'app/concepts/lockstep/query.rb', line 2

def initialize(klass)
  @klass = klass
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'app/concepts/lockstep/query.rb', line 335

def method_missing(meth, *args, &block)
  method_name = method_name.to_s
  if method_name.start_with?("find_by_")
    attrib = method_name.gsub(/^find_by_/, "")
    finder_name = "find_all_by_#{attrib}"

    define_singleton_method(finder_name) do |target_value|
      where({ attrib.to_sym => target_value }).first
    end

    send(finder_name, args[0])
  elsif method_name.start_with?("find_all_by_")
    attrib = method_name.gsub(/^find_all_by_/, "")
    finder_name = "find_all_by_#{attrib}"

    define_singleton_method(finder_name) do |target_value|
      where({ attrib.to_sym => target_value }).all
    end

    send(finder_name, args[0])
  end

  if @klass.scopes[meth].present?
    instance_exec *args, &@klass.scopes[meth]
  elsif Array.method_defined?(meth)
    all.send(meth, *args, &block)
  else
    super
  end
end

Instance Method Details

#allObject



315
316
317
# File 'app/concepts/lockstep/query.rb', line 315

def all
  execute
end

#build_filterObject

def related_to(obj, key)

query = { "$relatedTo" => { "object" => obj.to_pointer, "key" => key } }
criteria[:conditions].merge!(query)
clone

end



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/concepts/lockstep/query.rb', line 126

def build_filter
  filter = ""
  criteria[:conditions].each do |condition|
    if condition.is_a?(Hash)
      if condition[:type] == "or"
        filter = build_filter_condition(filter, "OR", condition[:query].build_filter)
      else
        condition.each do |key, value|
          filter = build_filter_condition(filter, "AND", build_filter_query(key, value))
        end
      end
    elsif condition.is_a?(String)
      filter = build_filter_condition(filter, "AND", condition)
    end
  end
  filter
end

#build_filter_condition(filter, merge_predicate, condition) ⇒ Object



144
145
146
147
148
149
150
151
# File 'app/concepts/lockstep/query.rb', line 144

def build_filter_condition(filter, merge_predicate, condition)
  if filter.present?
    filter = "(#{filter}) #{merge_predicate} (#{condition})"
  else
    filter += condition
  end
  filter
end

#build_filter_query(key, value) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/concepts/lockstep/query.rb', line 167

def build_filter_query(key, value)
  key = key.to_s unless key.is_a?(String)
  predicate = "eq" # default
  PREDICATES.each do |k, p|
    if key.ends_with?(k)
      key = key.gsub(k, "")
      predicate = p
      break
    end
  end

  # Build value
  if value.is_a?(Array)
    value = "(#{value.map { |v| v.is_a?(String) ? "'#{v}'" : v }.join(",")})"
    predicate = "in" if predicate == "eq"
  elsif value.is_a?(String) and !["NULL", "NOT NULL"].include?(value)
    value = "'#{value}'"
  elsif value.nil?
    predicate = "IS" if predicate == "eq"
    value = "NULL"
  end

  "#{key.camelize(:lower)} #{predicate} #{value}"
end

#build_paramsObject



192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/concepts/lockstep/query.rb', line 192

def build_params
  params = {}
  params.merge!({ :filter => build_filter }) if criteria[:conditions]
  # Lockstep Platform API does not support a page size of 1
  params.merge!({ :pageSize => (criteria[:limit] == 1 ? 2 : criteria[:limit]) }) if criteria[:limit]
  params.merge!({ :pageNumber => criteria[:page_number] }) if criteria[:page_number]
  params.merge!({ :include => criteria[:include].join(",") }) if criteria[:include]
  params.merge!({ :order => criteria[:order].join(",") }) if criteria[:order]
  params.merge!({ :pageSize => 2 }) if criteria[:count]
  params.reject! { |k, v| v.blank? }
  params
end

#chunk(count = 100) ⇒ Object

Divides the query into multiple chunks if you’re running into RestClient::BadRequest errors.



114
115
116
117
118
# File 'app/concepts/lockstep/query.rb', line 114

def chunk(count = 100)
  with_clone do
    criteria[:chunk] = count
  end
end

#chunk_results(params = {}) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'app/concepts/lockstep/query.rb', line 288

def chunk_results(params = {})
  page = criteria[:page] || 0
  limit = criteria[:limit] || 0
  # completed = false
  records = []
  loop do
    params[:pageNumber] = page
    params[:pageSize] = limit > 0 ? [limit - records.size, criteria[:chunk]].min : criteria[:chunk]
    break if params[:pageSize] == 0

    results = get_results(params)
    break unless results.present?

    records += results
    break if limit > 0 and records.size >= limit

    page += 1
  end

  records
end

#cloneObject

Clone is used as return object as it messes with the OR query in situations like these

unscoped = Lockstep::Connection.unscoped
unscoped.where(a: 1).or(unscoped.where(b: 1))
both the where conditions in the above scenario is modifying the same object causing stack-overflow


14
15
16
17
18
# File 'app/concepts/lockstep/query.rb', line 14

def clone
  c = Lockstep::Query.new(@klass)
  c.criteria.deep_merge!(self.criteria.deep_dup)
  c
end

#convert_arg(arg) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/concepts/lockstep/query.rb', line 54

def convert_arg(arg)
  return arg.to_pointer if arg.is_a?(Lockstep::ApiRecord)
  return Lockstep::ApiRecord.to_date_object(arg) if arg.is_a?(Time) || arg.is_a?(Date)
  if arg.is_a?(Hash)
    arg.keys.each do |key|
      @klass.valid_attribute?(key, raise_exception: true)
    end
    return arg.update(arg) { |key, inner_arg| convert_arg(inner_arg) }
  end

  arg
end

#countObject



319
320
321
322
# File 'app/concepts/lockstep/query.rb', line 319

def count
  criteria[:count] = true
  execute
end

#criteriaObject



26
27
28
# File 'app/concepts/lockstep/query.rb', line 26

def criteria
  @criteria ||= { :conditions => [] }
end

#executeObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/concepts/lockstep/query.rb', line 205

def execute
  return [] if criteria[:none]
  # This code automatically adds all related objects
  #
  # if @klass.has_many_relations
  #   relations = @klass.has_many_relations.select {|k, val| val[:included]}.keys.map { |relation| relation.to_s }
  #   include_object(*relations)
  # end

  params = build_params

  return chunk_results(params) if criteria[:chunk]

  get_results(params)
end

#find(id) ⇒ Lockstep::ApiRecord

Find a Lockstep::ApiRecord object by ID

Parameters:

  • id (String)

    the ID of the Parse object you want to find.

Returns:

Raises:



328
329
330
331
332
333
# File 'app/concepts/lockstep/query.rb', line 328

def find(id)
  raise Lockstep::Exceptions::RecordNotFound, "Couldn't find #{name} without an ID" if id.blank?
  record = where(@klass.id_ref => id).first
  raise Lockstep::Exceptions::RecordNotFound, "Couldn't find #{name} with id: #{id}" if record.blank?
  record
end

#firstObject



310
311
312
313
# File 'app/concepts/lockstep/query.rb', line 310

def first
  limit(1)
  execute.first
end

#get_relation_objects(objects) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'app/concepts/lockstep/query.rb', line 246

def get_relation_objects(objects)
  return objects if criteria[:include].blank?

  included_objects = criteria[:include].map { |item| item.to_s.downcase }

  if @klass.has_many_relations
    objects.each do |item|
      @klass.has_many_relations.each do |relation, relation_config|
        next unless included_objects.include?(relation.to_s.downcase)

        if !item.attributes.has_key?(relation.to_s)
          item.attributes[relation.to_s] = Lockstep::RelationArray.new(@klass, [], relation, relation_config[:class_name])
        elsif !item.attributes[relation].is_a?(Lockstep::RelationArray)
          item.attributes[relation.to_s] = Lockstep::RelationArray.new(@klass, item.attributes[relation], relation, relation_config[:class_name])
        end
      end
    end
  end

  objects.each do |item|
    item.attributes.each do |key, value|
      if value.is_a?(Array)
        relation = @klass.has_many_relations[key]
        next if relation.blank? or !included_objects.include?(key.to_s.downcase)

        value.each do |relation_hash|
          relation_obj = turn_relation_hash_into_object(relation, relation_hash)
          value[value.index(relation_hash)] = relation_obj
        end
      elsif value.is_a?(Hash)
        relation = @klass.belongs_to_relations[key]
        next if relation.blank?

        relation_hash = value
        relation_obj = turn_relation_hash_into_object(relation, relation_hash)
        item.attributes[key] = relation_obj
      end
    end
  end
  objects
end

#get_results(params = {}) ⇒ Object

Raises:

  • (StandardError)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/concepts/lockstep/query.rb', line 221

def get_results(params = {})
  resp = @klass.resource.get(@klass.query_path, :params => params)

  return [] if %w(404).include?(resp.code.to_s)
  # TODO handle non 200 response code. Throwing an exception for now
  raise StandardError.new("#{resp.code} error while fetching: #{resp.body}") unless %w(201 200).include?(resp.code.to_s)

  parsed_response = JSON.parse(resp.body)

  if criteria[:count]
    raise StandardError.new("Count is not supported for #{@klass}") if parsed_response.is_a?(Array)

    results = parsed_response["totalCount"]
    return results.to_i
  else
    results = parsed_response.is_a?(Array) ? parsed_response : parsed_response["records"]
    results = results[0..(criteria[:limit] - 1)] if criteria[:limit]
    get_relation_objects results.map { |r|
      # Convert camelcase to snake-case
      r = r.transform_keys { |key| key.underscore }
      @klass.model_name.to_s.constantize.new(r, false)
    }
  end
end

#include_object(*objects) ⇒ Object



81
82
83
84
85
86
87
# File 'app/concepts/lockstep/query.rb', line 81

def include_object(*objects)
  with_clone do
    criteria[:include] ||= []
    criteria[:include] += objects
    criteria[:include].uniq!
  end
end

#limit(limit) ⇒ Object



67
68
69
70
71
72
73
# File 'app/concepts/lockstep/query.rb', line 67

def limit(limit)
  with_clone do
    # If > 1000, set chunking, because large queries over 1000 need it with Parse
    # criteria[:chunk] = 1000 if limit > 1000
    criteria[:limit] = limit
  end
end

#modelObject



6
7
8
# File 'app/concepts/lockstep/query.rb', line 6

def model
  @klass
end

#noneObject



36
37
38
39
40
# File 'app/concepts/lockstep/query.rb', line 36

def none
  with_clone do
    criteria[:none] = true
  end
end

#or(query) ⇒ Object



30
31
32
33
34
# File 'app/concepts/lockstep/query.rb', line 30

def or(query)
  with_clone do
    criteria[:conditions] << { type: "or", query: query }
  end
end

#order(*attr) ⇒ Object

attr: :desc



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/concepts/lockstep/query.rb', line 90

def order(*attr)
  with_clone do
    criteria[:order] ||= []
    attr.each do |item|
      if item.is_a?(Hash)
        item.each do |k, v|
          criteria[:order] << "#{k.to_s.camelize.upcase} #{v}"
        end
      elsif item.is_a?(String)
        criteria[:order] << item
      end
    end
  end
end

#page(page_number) ⇒ Object



75
76
77
78
79
# File 'app/concepts/lockstep/query.rb', line 75

def page(page_number)
  with_clone do
    criteria[:page_number] = page_number
  end
end

#reorder(attr) ⇒ Object

attr: :desc



106
107
108
109
110
111
# File 'app/concepts/lockstep/query.rb', line 106

def reorder(attr)
  with_clone do
    criteria[:order] = []
    order(attr)
  end
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


366
367
368
369
370
371
372
# File 'app/concepts/lockstep/query.rb', line 366

def respond_to?(meth)
  if Array.method_defined?(meth)
    true
  else
    super
  end
end

#unscopedObject



374
375
376
# File 'app/concepts/lockstep/query.rb', line 374

def unscoped
  Lockstep::Query.new(@klass)
end

#where(args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'app/concepts/lockstep/query.rb', line 42

def where(args)
  if args.is_a?(Hash)
    args.each do |k, v|
      return none if v.is_a?(Array) and v.blank?
    end
  end

  with_clone do
    criteria[:conditions] << convert_arg(args)
  end
end

#with_clone(&block) ⇒ Object



20
21
22
23
24
# File 'app/concepts/lockstep/query.rb', line 20

def with_clone(&block)
  c = clone
  c.instance_exec(&block)
  c
end