Class: Parsistence::Query
- Inherits:
-
Object
- Object
- Parsistence::Query
- Defined in:
- lib/Parsistence/Query.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
Class Method Summary collapse
Instance Method Summary collapse
-
#all(&callback) ⇒ Nil
Grab all results that match query.
- #cached(val = true) ⇒ Object
- #checkKey(key) ⇒ Object
- #count(&callback) ⇒ Object
- #createQuery ⇒ Object
-
#eq(*fields) ⇒ Object
Query for fields where key == value.
- #fetch(&callback) ⇒ Object
- #fetchAll(&callback) ⇒ Nil deprecated Deprecated.
- #fetchOne(&callback) ⇒ Nil deprecated Deprecated.
-
#first(&callback) ⇒ Nil
Grab first result that matches query.
-
#gt(*fields) ⇒ Object
Query for fields where key > value.
-
#gte(*fields) ⇒ Object
Query for fields where key >= value.
-
#in(*fields) ⇒ Object
Query for fields where key is in an array of values.
-
#includes(*fields) ⇒ Object
Include related object in query.
-
#initialize ⇒ Query
constructor
A new instance of Query.
-
#limit(offset, number = nil) ⇒ Object
Limit number of results.
-
#lt(*fields) ⇒ Object
Query for fields where key < value.
-
#lte(*fields) ⇒ Object
Query for fields where key <= value.
-
#notEq(*fields) ⇒ Object
Query for fields where key != value.
-
#order(*fields) ⇒ Object
Order query results.
-
#showQuery ⇒ Object
Prints current Query conditions to STDERR.
- #where(*conditions, &callback) ⇒ Object deprecated Deprecated.
Constructor Details
#initialize ⇒ Query
Returns a new instance of Query.
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/Parsistence/Query.rb', line 70 def initialize @conditions = {} @negativeConditions = {} @ltConditions = {} @gtConditions = {} @lteConditions = {} @gteConditions = {} @inConditions = {} @order = {} @limit = nil @offset = nil @includes = [] end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
52 53 54 |
# File 'lib/Parsistence/Query.rb', line 52 def klass @klass end |
Class Method Details
.cache=(val) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/Parsistence/Query.rb', line 62 def self.cache=(val) if val self.cache_policy = KPFCachePolicyCacheThenNetwork else self.cache_policy = KPFCachePolicyNetworkOnly end end |
.cache_policy ⇒ Object
58 59 60 |
# File 'lib/Parsistence/Query.rb', line 58 def self.cache_policy @@cache_policy ||= KPFCachePolicyIgnoreCache end |
.cache_policy=(policy) ⇒ Object
54 55 56 |
# File 'lib/Parsistence/Query.rb', line 54 def self.cache_policy=(policy) @@cache_policy = policy end |
Instance Method Details
#all(&callback) ⇒ Nil
Grab all results that match query
242 243 244 245 |
# File 'lib/Parsistence/Query.rb', line 242 def all(&callback) fetchAll(&callback) nil end |
#cached(val = true) ⇒ Object
373 374 375 376 |
# File 'lib/Parsistence/Query.rb', line 373 def cached(val=true) @cached = val self end |
#checkKey(key) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/Parsistence/Query.rb', line 84 def checkKey(key) # Sanity check for mis-entered keys. tmp = self.klass.new unless tmp.respond_to?(key.to_sym) $stderr.puts "Parsistence Warning: No field '#{key}' found for #{self.klass.to_s} query." end end |
#count(&callback) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/Parsistence/Query.rb', line 165 def count(&callback) query = createQuery error = Pointer.new(:object) query.countObjectsInBackgroundWithBlock lambda { |item_count, error| if error callback.call false else callback.call item_count end } self end |
#createQuery ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 |
# File 'lib/Parsistence/Query.rb', line 92 def createQuery if self.klass.to_s == "User" query = PFUser.query else query = PFQuery.queryWithClassName(self.klass.to_s) end if @cached == true query.cachePolicy = KPFCachePolicyCacheElseNetwork elsif @cached == false query.cachePolicy = KPFCachePolicyNetworkOnly else query.cachePolicy = self.class.cache_policy if self.class.cache_policy end @includes.each do |include| query.includeKey(include) end @conditions.each do |key, value| checkKey key value = value.PFObject if value.respond_to?(:PFObject) query.whereKey(key, equalTo: value) end @inConditions.each do |key, value| checkKey key value = value.PFObject if value.respond_to?(:PFObject) query.whereKey(key, containedIn: value) end @negativeConditions.each do |key, value| checkKey key value = value.PFObject if value.respond_to?(:PFObject) query.whereKey(key, notEqualTo: value) end @ltConditions.each do |key, value| checkKey key value = value.PFObject if value.respond_to?(:PFObject) query.whereKey(key, lessThan: value) end @gtConditions.each do |key, value| checkKey key value = value.PFObject if value.respond_to?(:PFObject) query.whereKey(key, greaterThan: value) end @lteConditions.each do |key, value| checkKey key value = value.PFObject if value.respond_to?(:PFObject) query.whereKey(key, lessThanOrEqualTo: value) end @gteConditions.each do |key, value| checkKey key value = value.PFObject if value.respond_to?(:PFObject) query.whereKey(key, greaterThanOrEqualTo: value) end first = true @order.each do |field, direction| checkKey field if first query.orderByAscending(field) if direction && direction == :asc query.orderByDescending(field) if direction && direction == :desc first = false else query.addAscendingOrder(field) if direction && direction == :asc query.addDescendingOrder(field) if direction && direction == :desc end end query.limit = @limit if @limit query.skip = @offset if @offset query end |
#eq(*fields) ⇒ Object
Query for fields where key == value
300 301 302 303 304 305 |
# File 'lib/Parsistence/Query.rb', line 300 def eq(*fields) fields.each do |field| @conditions.merge! field end self end |
#fetch(&callback) ⇒ Object
179 180 181 182 183 184 185 186 187 |
# File 'lib/Parsistence/Query.rb', line 179 def fetch(&callback) if @limit && @limit == 1 fetchOne(&callback) else fetchAll(&callback) end self end |
#fetchAll(&callback) ⇒ Nil
Grab all results that match query
(see #all)
196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/Parsistence/Query.rb', line 196 def fetchAll(&callback) query = createQuery myKlass = self.klass query.findObjectsInBackgroundWithBlock (->(items) { modelItems = [] modelItems = items.map! { |item| myKlass.new(item) } if items callback.call(modelItems) if callback.arity == 1 callback.call if callback.arity == 0 }) self end |
#fetchOne(&callback) ⇒ Nil
Grab first result that matches query
(see #first)
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/Parsistence/Query.rb', line 217 def fetchOne(&callback) limit(0, 1) query = createQuery myKlass = self.klass query.getFirstObjectInBackgroundWithBlock (lambda { |item| item = myKlass.new(item) if item callback.call(item) }) self end |
#first(&callback) ⇒ Nil
Grab first result that matches query
251 252 253 254 |
# File 'lib/Parsistence/Query.rb', line 251 def first(&callback) fetchOne(&callback) nil end |
#gt(*fields) ⇒ Object
Query for fields where key > value
333 334 335 336 337 338 |
# File 'lib/Parsistence/Query.rb', line 333 def gt(*fields) fields.each do |field| @gtConditions.merge! field end self end |
#gte(*fields) ⇒ Object
Query for fields where key >= value
366 367 368 369 370 371 |
# File 'lib/Parsistence/Query.rb', line 366 def gte(*fields) fields.each do |field| @gteConditions.merge! field end self end |
#in(*fields) ⇒ Object
Query for fields where key is in an array of values
344 345 346 347 348 349 |
# File 'lib/Parsistence/Query.rb', line 344 def in(*fields) fields.each do |field| @inConditions.merge! field end self end |
#includes(*fields) ⇒ Object
Include related object in query
382 383 384 385 386 387 |
# File 'lib/Parsistence/Query.rb', line 382 def includes(*fields) fields.each do |field| @includes << field end self end |
#limit(offset, number = nil) ⇒ Object
Limit number of results
275 276 277 278 279 280 281 282 283 |
# File 'lib/Parsistence/Query.rb', line 275 def limit(offset, number = nil) if number.nil? number = offset offset = 0 end @offset = offset @limit = number self end |
#lt(*fields) ⇒ Object
Query for fields where key < value
322 323 324 325 326 327 |
# File 'lib/Parsistence/Query.rb', line 322 def lt(*fields) fields.each do |field| @ltConditions.merge! field end self end |
#lte(*fields) ⇒ Object
Query for fields where key <= value
355 356 357 358 359 360 |
# File 'lib/Parsistence/Query.rb', line 355 def lte(*fields) fields.each do |field| @lteConditions.merge! field end self end |
#notEq(*fields) ⇒ Object
Query for fields where key != value
311 312 313 314 315 316 |
# File 'lib/Parsistence/Query.rb', line 311 def notEq(*fields) fields.each do |field| @negativeConditions.merge! field end self end |
#order(*fields) ⇒ Object
Order query results
289 290 291 292 293 294 |
# File 'lib/Parsistence/Query.rb', line 289 def order(*fields) fields.each do |field| @order.merge! field end self end |
#showQuery ⇒ Object
Prints current Query conditions to STDERR
257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/Parsistence/Query.rb', line 257 def showQuery $stderr.puts "Conditions: #{@conditions.to_s}" $stderr.puts "negativeConditions: #{@negativeConditions.to_s}" $stderr.puts "ltConditions: #{@ltConditions.to_s}" $stderr.puts "gtConditions: #{@gtConditions.to_s}" $stderr.puts "lteConditions: #{@lteConditions.to_s}" $stderr.puts "gteConditions: #{@gteConditions.to_s}" $stderr.puts "inConditions: #{@inConditions.to_s}" $stderr.puts "order: #{@order.to_s}" $stderr.puts "limit: #{@limit.to_s}" $stderr.puts "offset: #{@offset.to_s}" end |
#where(*conditions, &callback) ⇒ Object
(see #eq)
232 233 234 235 236 |
# File 'lib/Parsistence/Query.rb', line 232 def where(*conditions, &callback) eq(conditions.first) fetch(&callback) nil end |