Class: Parse::Query
- Inherits:
-
Object
- Object
- Parse::Query
- Defined in:
- lib/parse/query.rb
Overview
Query objects parseplatform.github.io/docs/rest/guide/#queries
Instance Attribute Summary collapse
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#client ⇒ Object
Returns the value of attribute client.
-
#count ⇒ Object
Returns the value of attribute count.
-
#include ⇒ Object
Returns the value of attribute include.
-
#keys ⇒ Object
Returns the value of attribute keys.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#order ⇒ Object
Returns the value of attribute order.
-
#order_by ⇒ Object
Returns the value of attribute order_by.
-
#skip ⇒ Object
Returns the value of attribute skip.
-
#where ⇒ Object
Returns the value of attribute where.
Instance Method Summary collapse
- #add_constraint(field, constraint) ⇒ Object
- #contains_all(field, values) ⇒ Object
- #eq(hash_or_field, value = nil) ⇒ Object
- #exists(field, value = true) ⇒ Object
- #first ⇒ Object
- #get ⇒ Object
- #greater_eq(field, value) ⇒ Object
- #greater_than(field, value) ⇒ Object
- #in_query(field, query = nil) ⇒ Object
-
#initialize(cls_name, client = nil) ⇒ Query
constructor
A new instance of Query.
- #less_eq(field, value) ⇒ Object
- #less_than(field, value) ⇒ Object
- #not_eq(field, value) ⇒ Object
- #or(query) ⇒ Object
- #regex(field, expression) ⇒ Object
- #related_to(field, value) ⇒ Object
- #value_in(field, values) ⇒ Object
- #value_not_in(field, values) ⇒ Object
- #where_as_json ⇒ Object
Constructor Details
Instance Attribute Details
#class_name ⇒ Object
Returns the value of attribute class_name.
7 8 9 |
# File 'lib/parse/query.rb', line 7 def class_name @class_name end |
#client ⇒ Object
Returns the value of attribute client.
14 15 16 |
# File 'lib/parse/query.rb', line 14 def client @client end |
#count ⇒ Object
Returns the value of attribute count.
12 13 14 |
# File 'lib/parse/query.rb', line 12 def count @count end |
#include ⇒ Object
Returns the value of attribute include.
13 14 15 |
# File 'lib/parse/query.rb', line 13 def include @include end |
#keys ⇒ Object
Returns the value of attribute keys.
15 16 17 |
# File 'lib/parse/query.rb', line 15 def keys @keys end |
#limit ⇒ Object
Returns the value of attribute limit.
10 11 12 |
# File 'lib/parse/query.rb', line 10 def limit @limit end |
#order ⇒ Object
Returns the value of attribute order.
9 10 11 |
# File 'lib/parse/query.rb', line 9 def order @order end |
#order_by ⇒ Object
Returns the value of attribute order_by.
8 9 10 |
# File 'lib/parse/query.rb', line 8 def order_by @order_by end |
#skip ⇒ Object
Returns the value of attribute skip.
11 12 13 |
# File 'lib/parse/query.rb', line 11 def skip @skip end |
#where ⇒ Object
Returns the value of attribute where.
6 7 8 |
# File 'lib/parse/query.rb', line 6 def where @where end |
Instance Method Details
#add_constraint(field, constraint) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/parse/query.rb', line 25 def add_constraint(field, constraint) raise ArgumentError, 'cannot add constraint to an $or query' unless @ors.empty? 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
90 91 92 93 94 |
# File 'lib/parse/query.rb', line 90 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
41 42 43 44 45 46 |
# File 'lib/parse/query.rb', line 41 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 |
#exists(field, value = true) ⇒ Object
101 102 103 104 |
# File 'lib/parse/query.rb', line 101 def exists(field, value = true) add_constraint field, '$exists' => value self end |
#first ⇒ Object
127 128 129 130 |
# File 'lib/parse/query.rb', line 127 def first self.limit = 1 get.first end |
#get ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/parse/query.rb', line 132 def get uri = if @class_name == Parse::Protocol::CLASS_USER Protocol.user_uri elsif @class_name == Parse::Protocol::CLASS_INSTALLATION Protocol.installation_uri else Protocol.class_uri @class_name end query = { 'where' => where_as_json.to_json } ordering(query) [:count, :limit, :skip, :include, :keys].each do |a| merge_attribute(a, query) end @client.logger.info { "Parse query for #{uri} #{query.inspect}" } unless @client.quiet response = @client.request uri, :get, nil, query if response.is_a?(Hash) && response.key?(Protocol::KEY_RESULTS) && response[Protocol::KEY_RESULTS].is_a?(Array) parsed_results = response[Protocol::KEY_RESULTS].map do |result| result = Parse.parse_json(class_name, result) Parse.copy_client(@client, result) end if response.keys.size == 1 parsed_results else response.dup.merge(Protocol::KEY_RESULTS => parsed_results) end else raise ParseError, "query response not a Hash with #{Protocol::KEY_RESULTS} key: #{response.class} #{response.inspect}" end end |
#greater_eq(field, value) ⇒ Object
73 74 75 76 |
# File 'lib/parse/query.rb', line 73 def greater_eq(field, value) add_constraint field, '$gte' => Parse.pointerize_value(value) self end |
#greater_than(field, value) ⇒ Object
68 69 70 71 |
# File 'lib/parse/query.rb', line 68 def greater_than(field, value) add_constraint field, '$gt' => Parse.pointerize_value(value) self end |
#in_query(field, query = nil) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/parse/query.rb', line 106 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 |
#less_eq(field, value) ⇒ Object
63 64 65 66 |
# File 'lib/parse/query.rb', line 63 def less_eq(field, value) add_constraint field, '$lte' => Parse.pointerize_value(value) self end |
#less_than(field, value) ⇒ Object
58 59 60 61 |
# File 'lib/parse/query.rb', line 58 def less_than(field, value) add_constraint field, '$lt' => Parse.pointerize_value(value) self end |
#not_eq(field, value) ⇒ Object
48 49 50 51 |
# File 'lib/parse/query.rb', line 48 def not_eq(field, value) add_constraint field, '$ne' => Parse.pointerize_value(value) self end |
#or(query) ⇒ Object
35 36 37 38 39 |
# File 'lib/parse/query.rb', line 35 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
53 54 55 56 |
# File 'lib/parse/query.rb', line 53 def regex(field, expression) add_constraint field, '$regex' => expression self end |
#related_to(field, value) ⇒ Object
96 97 98 99 |
# File 'lib/parse/query.rb', line 96 def (field, value) h = { 'object' => Parse.pointerize_value(value), 'key' => field } add_constraint('$relatedTo', h) end |
#value_in(field, values) ⇒ Object
78 79 80 81 82 |
# File 'lib/parse/query.rb', line 78 def value_in(field, values) add_constraint( field, '$in' => values.map { |v| Parse.pointerize_value(v) }) self end |
#value_not_in(field, values) ⇒ Object
84 85 86 87 88 |
# File 'lib/parse/query.rb', line 84 def value_not_in(field, values) add_constraint( field, '$nin' => values.map { |v| Parse.pointerize_value(v) }) self end |
#where_as_json ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/parse/query.rb', line 119 def where_as_json if @ors.empty? @where else { '$or' => [where] + @ors.map(&:where_as_json) } end end |