lib/parse/query.rb in parse-ruby-client-0.1.1 vs lib/parse/query.rb in parse-ruby-client-0.1.2

- old
+ new

@@ -13,22 +13,30 @@ def initialize(cls_name) @class_name = cls_name @where = {} @order = :ascending + @ors = [] end 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 #private :add_constraint + def or(query) + raise ArgumentError, "you must pass an entire #{self.class} to \#or" unless query.is_a?(self.class) + @ors << query + self + end + def eq(field, value) add_constraint field, value self end @@ -65,19 +73,37 @@ def exists(field, value = true) add_constraint field, { "$exists" => value } self end + def in_query(field, query) + query_hash = {Parse::Protocol::KEY_CLASS_NAME => query.class_name, "where" => query.where} + add_constraint(field, "$inQuery" => query_hash) + end + def count @count = true self end + def where_as_json + if @ors.size > 0 + {"$or" => [self.where] + @ors.map{|query| query.where_as_json}} + else + @where + end + end + def get uri = Protocol.class_uri @class_name - query = { "where" => CGI.escape(@where.to_json) } + 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].each {|a| merge_attribute(a, 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 \ No newline at end of file