Class: Parse::Query

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cls_name) ⇒ Query

Returns a new instance of Query


16
17
18
19
20
21
# File 'lib/parse/query.rb', line 16

def initialize(cls_name)
  @class_name = cls_name
  @where = {}
  @order = :ascending
  @ors = []
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name


8
9
10
# File 'lib/parse/query.rb', line 8

def class_name
  @class_name
end

#countObject

Returns the value of attribute count


13
14
15
# File 'lib/parse/query.rb', line 13

def count
  @count
end

#includeObject

Returns the value of attribute include


14
15
16
# File 'lib/parse/query.rb', line 14

def include
  @include
end

#limitObject

Returns the value of attribute limit


11
12
13
# File 'lib/parse/query.rb', line 11

def limit
  @limit
end

#orderObject

Returns the value of attribute order


10
11
12
# File 'lib/parse/query.rb', line 10

def order
  @order
end

#order_byObject

Returns the value of attribute order_by


9
10
11
# File 'lib/parse/query.rb', line 9

def order_by
  @order_by
end

#skipObject

Returns the value of attribute skip


12
13
14
# File 'lib/parse/query.rb', line 12

def skip
  @skip
end

#whereObject

Returns the value of attribute where


7
8
9
# File 'lib/parse/query.rb', line 7

def where
  @where
end

Instance Method Details

#add_constraint(field, constraint) ⇒ Object

Raises:

  • (ArgumentError)

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

def add_constraint(field, constraint)
  raise ArgumentError, "cannot add constraint to an $or query" if @ors.size > 0
  current = where[field]
  if current && current.is_a?(Hash) && constraint.is_a?(Hash)
    current.merge! constraint
  else
    where[field] = constraint
  end
end

#contains_all(field, values) ⇒ Object


96
97
98
99
# File 'lib/parse/query.rb', line 96

def contains_all(field, values)
  add_constraint field, { "$all" => values.map { |v| Parse.pointerize_value(v) } }
  self
end

#eq(hash_or_field, value = nil) ⇒ Object


45
46
47
48
49
# File 'lib/parse/query.rb', line 45

def eq(hash_or_field,value=nil)
  return eq_pair(hash_or_field,value) unless hash_or_field.is_a?(Hash)
  hash_or_field.each_pair { |k,v| eq_pair k, v }
  self
end

#eq_pair(field, value) ⇒ Object


51
52
53
54
# File 'lib/parse/query.rb', line 51

def eq_pair(field, value)
  add_constraint field, Parse.pointerize_value(value)
  self
end

#exists(field, value = true) ⇒ Object


106
107
108
109
# File 'lib/parse/query.rb', line 106

def exists(field, value = true)
  add_constraint field, { "$exists" => value }
  self
end

#firstObject


130
131
132
133
# File 'lib/parse/query.rb', line 130

def first
  self.limit = 1
  get.first
end

#getObject


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/parse/query.rb', line 135

def get
  uri   = Protocol.class_uri @class_name
  if @class_name == Parse::Protocol::CLASS_USER
    uri = Protocol.user_uri
  elsif @class_name == Parse::Protocol::CLASS_INSTALLATION
    uri = Protocol.installation_uri
  end
  query = { "where" => where_as_json.to_json }
  set_order(query)
  [:count, :limit, :skip, :include].each {|a| merge_attribute(a, query)}
  Parse.client.logger.info{"Parse query for #{uri} #{query.inspect}"} unless Parse.client.quiet
  response = Parse.client.request uri, :get, nil, query

  if response.is_a?(Hash) && response.has_key?(Protocol::KEY_RESULTS) && response[Protocol::KEY_RESULTS].is_a?(Array)
    parsed_results = response[Protocol::KEY_RESULTS].map{|o| Parse.parse_json(class_name, o)}
    if response.keys.size == 1
      parsed_results
    else
      response.dup.merge(Protocol::KEY_RESULTS => parsed_results)
    end
  else
    raise ParseError.new("query response not a Hash with #{Protocol::KEY_RESULTS} key: #{response.class} #{response.inspect}")
  end
end

#greater_eq(field, value) ⇒ Object


81
82
83
84
# File 'lib/parse/query.rb', line 81

def greater_eq(field, value)
  add_constraint field, { "$gte" => Parse.pointerize_value(value) }
  self
end

#greater_than(field, value) ⇒ Object


76
77
78
79
# File 'lib/parse/query.rb', line 76

def greater_than(field, value)
  add_constraint field, { "$gt" => Parse.pointerize_value(value) }
  self
end

#in_query(field, query = nil) ⇒ Object


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

def in_query(field, query=nil)
  query_hash = {Parse::Protocol::KEY_CLASS_NAME => query.class_name, "where" => query.where}
  add_constraint(field, "$inQuery" => query_hash)
  self
end

#includes(class_name) ⇒ Object

private :add_constraint


34
35
36
37
# File 'lib/parse/query.rb', line 34

def includes(class_name)
  @includes = class_name
  self
end

#less_eq(field, value) ⇒ Object


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

def less_eq(field, value)
  add_constraint field, { "$lte" => Parse.pointerize_value(value) }
  self
end

#less_than(field, value) ⇒ Object


66
67
68
69
# File 'lib/parse/query.rb', line 66

def less_than(field, value)
  add_constraint field, { "$lt" => Parse.pointerize_value(value) }
  self
end

#not_eq(field, value) ⇒ Object


56
57
58
59
# File 'lib/parse/query.rb', line 56

def not_eq(field, value)
  add_constraint field, { "$ne" => Parse.pointerize_value(value) }
  self
end

#or(query) ⇒ Object

Raises:

  • (ArgumentError)

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

def or(query)
  raise ArgumentError, "you must pass an entire #{self.class} to \#or" unless query.is_a?(self.class)
  @ors << query
  self
end

#regex(field, expression) ⇒ Object


61
62
63
64
# File 'lib/parse/query.rb', line 61

def regex(field, expression)
  add_constraint field, { "$regex" => expression }
  self
end

101
102
103
104
# File 'lib/parse/query.rb', line 101

def related_to(field,value)
  h = {"object" => Parse.pointerize_value(value), "key" => field}
  add_constraint("$relatedTo", h )
end

#value_in(field, values) ⇒ Object


86
87
88
89
# File 'lib/parse/query.rb', line 86

def value_in(field, values)
  add_constraint field, { "$in" => values.map { |v| Parse.pointerize_value(v) } }
  self
end

#value_not_in(field, values) ⇒ Object


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

def value_not_in(field, values)
  add_constraint field, { "$nin" => values.map { |v| Parse.pointerize_value(v) } }
  self
end

#where_as_jsonObject


122
123
124
125
126
127
128
# File 'lib/parse/query.rb', line 122

def where_as_json
  if @ors.size > 0
    {"$or" => [self.where] + @ors.map{|query| query.where_as_json}}
  else
    @where
  end
end