Class: Parse::Query

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

Direct Known Subclasses

Subquery

Defined Under Namespace

Classes: Condition, OrCondition, RelatedToCondition, Subquery, WithinCondition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parse_class_name = nil, parse_client = nil) ⇒ Query

Returns a new instance of Query.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/parse/query.rb', line 6

def initialize parse_class_name=nil, parse_client=nil
  @parse_class_name = parse_class_name.to_s
  @parse_client = parse_client || Parse::Client.default
  @limit = nil
  @skip = nil
  @count = false
  @where = [] # array of Conditions
  @order = []
  @include = []
  @keys = []
end

Instance Attribute Details

#keys(*vals) ⇒ Object (readonly)

Returns the value of attribute keys.



3
4
5
# File 'lib/parse/query.rb', line 3

def keys
  @keys
end

#parse_class_nameObject

Returns the value of attribute parse_class_name.



4
5
6
# File 'lib/parse/query.rb', line 4

def parse_class_name
  @parse_class_name
end

#parse_clientObject

Returns the value of attribute parse_client.



4
5
6
# File 'lib/parse/query.rb', line 4

def parse_client
  @parse_client
end

Instance Method Details

#count(val = nil) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/parse/query.rb', line 48

def count val=nil
  if val
    @count = val
  else
    @count
  end
  self
end

#include(*vals) ⇒ Object



90
91
92
93
# File 'lib/parse/query.rb', line 90

def include *vals
  return @include if vals.empty?
  @include += vals
end

#inspectObject



112
113
114
# File 'lib/parse/query.rb', line 112

def inspect
  "#{@parse_class_name}, #{to_params}"
end

#limit(val = nil) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/parse/query.rb', line 30

def limit val=nil
  if val
    @limit = val
  else
    @limit
  end
  self
end

#order(*vals) ⇒ Object Also known as: order_asc



71
72
73
74
75
# File 'lib/parse/query.rb', line 71

def order *vals
  return @order if vals.empty?
  @order += vals
  self
end

#order_desc(*vals) ⇒ Object



78
79
80
81
82
# File 'lib/parse/query.rb', line 78

def order_desc *vals
  order *(vals.map do |val|
    val[0] == '-' ? val[1..-1] : "-#{val}"
  end)
end

#run(&block) ⇒ Object Also known as: invoke



18
19
20
21
22
23
24
25
26
27
# File 'lib/parse/query.rb', line 18

def run &block
  block = proc do |body| 
    # TODO: should handle error
    body['results']
  end unless block
  endpoint = %w(User).include?(@parse_class_name) \
    ? "#{@parse_class_name.lowercase}s" 
    : "classes/#{@parse_class_name}"
  @parse_client.call_api :get, "#{endpoint}?#{to_params}", nil, &block
end

#skip(val = nil) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/parse/query.rb', line 39

def skip val=nil
  if val
    @skip = val
  else
    @skip
  end
  self
end

#to_paramsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/parse/query.rb', line 95

def to_params
  params = []

  where = @where.join ','
  order = @order.join ','
  keys = @keys.join ','
  include = @include.join ','
  params.push "where=#{URI.encode "{#{where}}"}" unless where.empty?
  params.push "order=#{URI.encode order}" unless order.empty?
  params.push "keys=#{URI.encode keys}" unless keys.empty?
  params.push "include=#{URI.encode include}" unless include.empty?
  params.push "skip=#{URI.encode @skip.to_s}" if @skip
  params.push "limit=#{URI.encode @limit.to_s}" if @limit

  params.join '&'
end

#where(hash = nil, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/parse/query.rb', line 57

def where hash=nil, &block
  return @where if hash.nil? && block.nil?

  if hash.is_a? Hash
    hash.each do |k, v|
      @where << %Q|"#{k}":#{v.inspect}|
    end
  else
    block = hash if hash.is_a?(Proc) && block.nil?
    instance_eval &block if block
  end
  self
end