Class: Parse::Query
- Inherits:
-
Object
show all
- Defined in:
- lib/parse/query.rb
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 = [] @order = []
@include = []
@keys = []
end
|
Instance Attribute Details
#keys(*vals) ⇒ Object
Returns the value of attribute keys.
3
4
5
|
# File 'lib/parse/query.rb', line 3
def keys
@keys
end
|
#parse_class_name ⇒ Object
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_client ⇒ Object
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
50
51
52
53
54
55
56
57
|
# File 'lib/parse/query.rb', line 50
def count val=nil
if val
@count = val
else
@count
end
self
end
|
#include(*vals) ⇒ Object
92
93
94
95
|
# File 'lib/parse/query.rb', line 92
def include *vals
return @include if vals.empty?
@include += vals
end
|
115
116
117
|
# File 'lib/parse/query.rb', line 115
def inspect
"#{@parse_class_name}, #{to_params}"
end
|
#limit(val = nil) ⇒ Object
32
33
34
35
36
37
38
39
|
# File 'lib/parse/query.rb', line 32
def limit val=nil
if val
@limit = val
else
@limit
end
self
end
|
#order(*vals) ⇒ Object
Also known as:
order_asc
73
74
75
76
77
|
# File 'lib/parse/query.rb', line 73
def order *vals
return @order if vals.empty?
@order += vals
self
end
|
#order_desc(*vals) ⇒ Object
80
81
82
83
84
|
# File 'lib/parse/query.rb', line 80
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
28
29
|
# File 'lib/parse/query.rb', line 18
def run &block
block = proc do |body|
results = body['results']
results.query_count = body['count'] if results
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
41
42
43
44
45
46
47
48
|
# File 'lib/parse/query.rb', line 41
def skip val=nil
if val
@skip = val
else
@skip
end
self
end
|
#to_params ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/parse/query.rb', line 97
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.push "count=1" if @count
params.join '&'
end
|
#where(hash = nil, &block) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/parse/query.rb', line 59
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
|