Class: Parse::Query

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

Direct Known Subclasses

Subquery

Defined Under Namespace

Classes: Condition, OrCondition, Subquery

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parse_class_or_name = nil) ⇒ Query

Returns a new instance of Query.



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

def initialize parse_class_or_name=nil
  @parse_class, @parse_class_name, @parse_client = 
    parse_class_or_name === Parse::Object \
      ? [parse_class_or_name, parse_class_or_name.parse_class_name, 
          parse_class_or_name.parse_client]
      : [nil, parse_class_or_name.to_s, Parse::Client.default_client]
  @limit = nil
  @skip = nil
  @count = false
  @where = []
  @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



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

#inspectObject



114
115
116
# File 'lib/parse/query.rb', line 114

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

#invoke(&block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/parse/query.rb', line 21

def invoke &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

#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

#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_paramsObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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.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}"|
    end
  else
    block = hash if hash.is_a?(Proc) && block.nil?
    instance_eval &block if block
  end
  self
end