Class: Parse::Query
- Inherits:
-
Object
- Object
- Parse::Query
- Defined in:
- lib/parse/query.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#count ⇒ Object
Returns the value of attribute count.
-
#include ⇒ Object
Returns the value of attribute include.
-
#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
- #eq(field, value) ⇒ Object
- #exists(field, value = true) ⇒ Object
- #get ⇒ Object
- #greater_eq(field, value) ⇒ Object
- #greater_than(field, value) ⇒ Object
- #in_query(field, query = nil) ⇒ Object
-
#includes(class_name) ⇒ Object
private :add_constraint.
-
#initialize(cls_name) ⇒ 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
#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_name ⇒ Object
Returns the value of attribute class_name.
8 9 10 |
# File 'lib/parse/query.rb', line 8 def class_name @class_name end |
#count ⇒ Object
Returns the value of attribute count.
13 14 15 |
# File 'lib/parse/query.rb', line 13 def count @count end |
#include ⇒ Object
Returns the value of attribute include.
14 15 16 |
# File 'lib/parse/query.rb', line 14 def include @include end |
#limit ⇒ Object
Returns the value of attribute limit.
11 12 13 |
# File 'lib/parse/query.rb', line 11 def limit @limit end |
#order ⇒ Object
Returns the value of attribute order.
10 11 12 |
# File 'lib/parse/query.rb', line 10 def order @order end |
#order_by ⇒ Object
Returns the value of attribute order_by.
9 10 11 |
# File 'lib/parse/query.rb', line 9 def order_by @order_by end |
#skip ⇒ Object
Returns the value of attribute skip.
12 13 14 |
# File 'lib/parse/query.rb', line 12 def skip @skip end |
#where ⇒ Object
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
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 |
#eq(field, value) ⇒ Object
45 46 47 48 |
# File 'lib/parse/query.rb', line 45 def eq(field, value) add_constraint field, Parse.pointerize_value(value) self end |
#exists(field, value = true) ⇒ Object
95 96 97 98 |
# File 'lib/parse/query.rb', line 95 def exists(field, value = true) add_constraint field, { "$exists" => value } self end |
#get ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/parse/query.rb', line 119 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}"} 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
75 76 77 78 |
# File 'lib/parse/query.rb', line 75 def greater_eq(field, value) add_constraint field, { "$gte" => Parse.pointerize_value(value) } self end |
#greater_than(field, value) ⇒ Object
70 71 72 73 |
# File 'lib/parse/query.rb', line 70 def greater_than(field, value) add_constraint field, { "$gt" => Parse.pointerize_value(value) } self end |
#in_query(field, query = nil) ⇒ Object
100 101 102 103 104 |
# File 'lib/parse/query.rb', line 100 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
65 66 67 68 |
# File 'lib/parse/query.rb', line 65 def less_eq(field, value) add_constraint field, { "$lte" => Parse.pointerize_value(value) } self end |
#less_than(field, value) ⇒ Object
60 61 62 63 |
# File 'lib/parse/query.rb', line 60 def less_than(field, value) add_constraint field, { "$lt" => Parse.pointerize_value(value) } self end |
#not_eq(field, value) ⇒ Object
50 51 52 53 |
# File 'lib/parse/query.rb', line 50 def not_eq(field, value) add_constraint field, { "$ne" => Parse.pointerize_value(value) } self end |
#or(query) ⇒ Object
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
55 56 57 58 |
# File 'lib/parse/query.rb', line 55 def regex(field, expression) add_constraint field, { "$regex" => expression } self end |
#related_to(field, value) ⇒ Object
90 91 92 93 |
# File 'lib/parse/query.rb', line 90 def (field,value) h = {"object" => Parse.pointerize_value(value), "key" => field} add_constraint("$relatedTo", h ) end |
#value_in(field, values) ⇒ Object
80 81 82 83 |
# File 'lib/parse/query.rb', line 80 def value_in(field, values) add_constraint field, { "$in" => values.map { |v| Parse.pointerize_value(v) } } self end |
#value_not_in(field, values) ⇒ Object
85 86 87 88 |
# File 'lib/parse/query.rb', line 85 def value_not_in(field, values) add_constraint field, { "$nin" => values.map { |v| Parse.pointerize_value(v) } } self end |
#where_as_json ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/parse/query.rb', line 111 def where_as_json if @ors.size > 0 {"$or" => [self.where] + @ors.map{|query| query.where_as_json}} else @where end end |