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.
-
#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) ⇒ Object
-
#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
private :add_constraint.
- #regex(field, expression) ⇒ Object
- #value_in(field, values) ⇒ Object
- #where_as_json ⇒ Object
Constructor Details
#initialize(cls_name) ⇒ Query
Returns a new instance of Query.
14 15 16 17 18 19 |
# File 'lib/parse/query.rb', line 14 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.
7 8 9 |
# File 'lib/parse/query.rb', line 7 def class_name @class_name end |
#count ⇒ Object
Returns the value of attribute count.
12 13 14 |
# File 'lib/parse/query.rb', line 12 def count @count 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
21 22 23 24 25 26 27 28 29 |
# File 'lib/parse/query.rb', line 21 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
38 39 40 41 |
# File 'lib/parse/query.rb', line 38 def eq(field, value) add_constraint field, value self end |
#exists(field, value = true) ⇒ Object
78 79 80 81 |
# File 'lib/parse/query.rb', line 78 def exists(field, value = true) add_constraint field, { "$exists" => value } self end |
#get ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/parse/query.rb', line 102 def get uri = Protocol.class_uri @class_name if @class_name == Parse::Protocol::CLASS_USER uri = Protocol.user_uri end query = { "where" => CGI.escape(where_as_json.to_json) } set_order(query) [:count, :limit, :skip].each {|a| merge_attribute(a, query)} response = Parse.client.request uri, :get, nil, query Parse.parse_json class_name, response end |
#greater_eq(field, value) ⇒ Object
68 69 70 71 |
# File 'lib/parse/query.rb', line 68 def greater_eq(field, value) add_constraint field, { "$gte" => value } self end |
#greater_than(field, value) ⇒ Object
63 64 65 66 |
# File 'lib/parse/query.rb', line 63 def greater_than(field, value) add_constraint field, { "$gt" => value } self end |
#in_query(field, query) ⇒ Object
83 84 85 86 87 |
# File 'lib/parse/query.rb', line 83 def in_query(field, query) 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
58 59 60 61 |
# File 'lib/parse/query.rb', line 58 def less_eq(field, value) add_constraint field, { "$lte" => value } self end |
#less_than(field, value) ⇒ Object
53 54 55 56 |
# File 'lib/parse/query.rb', line 53 def less_than(field, value) add_constraint field, { "$lt" => value } self end |
#not_eq(field, value) ⇒ Object
43 44 45 46 |
# File 'lib/parse/query.rb', line 43 def not_eq(field, value) add_constraint field, { "$ne" => value } self end |
#or(query) ⇒ Object
private :add_constraint
32 33 34 35 36 |
# File 'lib/parse/query.rb', line 32 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
48 49 50 51 |
# File 'lib/parse/query.rb', line 48 def regex(field, expression) add_constraint field, { "$regex" => expression } self end |
#value_in(field, values) ⇒ Object
73 74 75 76 |
# File 'lib/parse/query.rb', line 73 def value_in(field, values) add_constraint field, { "$in" => values } self end |
#where_as_json ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/parse/query.rb', line 94 def where_as_json if @ors.size > 0 {"$or" => [self.where] + @ors.map{|query| query.where_as_json}} else @where end end |