Class: Parse::Query::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(query, column_name) ⇒ Condition

Returns a new instance of Condition.



174
175
176
177
178
# File 'lib/parse/query.rb', line 174

def initialize query, column_name
  @query = query
  @column_name = column_name
  @conditions = []
end

Instance Method Details

#between(*range) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/parse/query.rb', line 206

def between *range
  if range.size == 1 && range.first.is_a?(Range)
    range = range.first
  elsif range.size == 2
    range = range[0]..range[1]
  else
    raise ArgumentErrr
  end

  if range.exclude_end?
    self.gt(range.begin).lt(range.end)
  else
    self.gte(range.begin).lte(range.end)
  end
end

#exists(val = true) ⇒ Object



222
223
224
225
# File 'lib/parse/query.rb', line 222

def exists val=true
  @conditions.push ['$exists', val]
  self
end

#max_distance_in_kilometers(kilometers) ⇒ Object



273
274
275
276
# File 'lib/parse/query.rb', line 273

def max_distance_in_kilometers kilometers
  @conditions.push ['$maxDistanceInKilometers', kilometers]
  self
end

#max_distance_in_miles(miles) ⇒ Object



268
269
270
271
# File 'lib/parse/query.rb', line 268

def max_distance_in_miles miles
  @conditions.push ['$maxDistanceInMiles', miles]
  self
end

#max_distance_in_radians(radians) ⇒ Object



278
279
280
281
# File 'lib/parse/query.rb', line 278

def max_distance_in_radians radians
  @conditions.push ['$maxDistanceInRadians', radians]
  self
end

#near_sphere(geo_point) ⇒ Object

conditions for GeoPoints



263
264
265
266
# File 'lib/parse/query.rb', line 263

def near_sphere geo_point
  @conditions.push ['$nearSphere', geo_point]
  self
end

#or(cond) ⇒ Object



252
253
254
255
256
257
258
259
# File 'lib/parse/query.rb', line 252

def or cond
  # quite dirty!!
  @query.where.delete self
  @query.where.delete cond
  or_cond = Condition.new @query, '$or'
  or_cond.eq [self, cond]
  @query.where.push or_cond
end

#regex(regex) ⇒ Object



288
289
290
291
# File 'lib/parse/query.rb', line 288

def regex regex
  @conditions.push ['$regex', regex.source]
  self
end

#starts_with(str) ⇒ Object



294
295
296
# File 'lib/parse/query.rb', line 294

def starts_with str
  regex %r|^\Q#{str}\E|
end

#to_sObject



298
299
300
301
302
303
304
305
306
307
# File 'lib/parse/query.rb', line 298

def to_s
  if @conditions.size == 1 && !@conditions[0].is_a?(Array)
    "#{@column_name.to_s.inspect}:#{condition_to_s @conditions[0]}"
  elsif @conditions.size == 1 && @conditions[0][0].to_s[0] != '$'
    # $or
    "#{@column_name.to_s.inspect}:#{condition_to_s @conditions[0]}"
  else
    "#{@column_name.to_s.inspect}:{#{@conditions.map{|c| condition_to_s c}.join ','}}"
  end
end

#within(southwest_geo_point, northeast_geo_point) ⇒ Object



283
284
285
286
# File 'lib/parse/query.rb', line 283

def within southwest_geo_point, northeast_geo_point
  @conditions.push WithinCondition.new(southwest_geo_point, northeast_geo_point)
  self
end