Class: Seabright::Collection
- Inherits:
-
Array
- Object
- Array
- Seabright::Collection
- Includes:
- CachedScripts
- Defined in:
- lib/redis_object/collection.rb
Class Method Summary collapse
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #[](k) ⇒ Object
- #class_const ⇒ Object
- #cleanup! ⇒ Object
- #clear! ⇒ Object
- #delete(obj) ⇒ Object
- #each ⇒ Object
- #find(k) ⇒ Object
- #first ⇒ Object
- #index_key(idx) ⇒ Object
- #indexed(idx, num = -1,, reverse = false) ⇒ Object
-
#initialize(name, owner) ⇒ Collection
constructor
A new instance of Collection.
- #item_key(k) ⇒ Object
- #key ⇒ Object
- #keys_by_index(idx, num = -1,, reverse = false) ⇒ Object
- #last ⇒ Object
- #latest ⇒ Object
- #map(&block) ⇒ Object
- #match(pkt) ⇒ Object
- #objects ⇒ Object
- #push(obj) ⇒ Object
- #real_at(key) ⇒ Object
- #remove! ⇒ Object
- #select(&block) ⇒ Object
- #store ⇒ Object
- #temp_key ⇒ Object
Methods included from CachedScripts
Constructor Details
#initialize(name, owner) ⇒ Collection
Returns a new instance of Collection.
206 207 208 209 |
# File 'lib/redis_object/collection.rb', line 206 def initialize(name,owner) @name = name.to_s @owner = owner end |
Class Method Details
.class_const_for(name) ⇒ Object
387 388 389 |
# File 'lib/redis_object/collection.rb', line 387 def class_const_for(name) Object.const_get(name.to_s.classify.to_sym) rescue RedisObject end |
.load(name, owner) ⇒ Object
381 382 383 384 385 |
# File 'lib/redis_object/collection.rb', line 381 def load(name,owner) out = new(name,owner) out.replace class_const_for(name).store.zrange(out.key,0,-1) out end |
Instance Method Details
#<<(obj) ⇒ Object
357 358 359 360 361 |
# File 'lib/redis_object/collection.rb', line 357 def <<(obj) k = obj.class == String ? obj : obj.hkey store.zadd(key,store.zcount(key,"-inf", "+inf"),k) super(k) end |
#[](k) ⇒ Object
272 273 274 |
# File 'lib/redis_object/collection.rb', line 272 def [](k) find k end |
#class_const ⇒ Object
367 368 369 |
# File 'lib/redis_object/collection.rb', line 367 def class_const self.class.class_const_for(@name) end |
#cleanup! ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/redis_object/collection.rb', line 319 def cleanup! each_index do |key| unless a = class_const.find_by_key(at(key)) Log.debug "Deleting #{key} because not #{a.inspect}" delete at(key) end end if size < 1 Log.debug "Deleting collection #{@name} because empty" remove! end end |
#clear! ⇒ Object
353 354 355 |
# File 'lib/redis_object/collection.rb', line 353 def clear! store.zrem(key,self.join(" ")) end |
#delete(obj) ⇒ Object
347 348 349 350 351 |
# File 'lib/redis_object/collection.rb', line 347 def delete(obj) k = obj.class == String ? obj : obj.hkey store.zrem(key,k) super(k) end |
#each ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/redis_object/collection.rb', line 302 def each out = Enumerator.new do |y| each_index do |key| if a = class_const.find_by_key(at(key)) y << a end end end if block_given? out.each do |a| yield a end else out end end |
#find(k) ⇒ Object
261 262 263 264 265 266 267 268 269 270 |
# File 'lib/redis_object/collection.rb', line 261 def find(k) if k.is_a? String return real_at(item_key(k)) elsif k.is_a? Hash return match(k) elsif k.is_a? Integer return real_at(at(k)) end return nil end |
#first ⇒ Object
294 295 296 |
# File 'lib/redis_object/collection.rb', line 294 def first class_const.find_by_key(super) end |
#index_key(idx) ⇒ Object
253 254 255 |
# File 'lib/redis_object/collection.rb', line 253 def index_key(idx) class_const.index_key(idx) end |
#indexed(idx, num = -1,, reverse = false) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/redis_object/collection.rb', line 219 def indexed(idx,num=-1,reverse=false) keys = keys_by_index(idx,num,reverse) out = ListEnumerator.new(keys) do |y| keys.each do |member| if a = class_const.find_by_key(member) y << a end end end if block_given? out.each do |itm| yield itm end else out end end |
#item_key(k) ⇒ Object
257 258 259 |
# File 'lib/redis_object/collection.rb', line 257 def item_key(k) "#{class_const}:#{k}_h" end |
#key ⇒ Object
375 376 377 |
# File 'lib/redis_object/collection.rb', line 375 def key "#{@owner ? "#{@owner.key}:" : ""}COLLECTION:#{@name}" end |
#keys_by_index(idx, num = -1,, reverse = false) ⇒ Object
244 245 246 247 248 249 250 251 |
# File 'lib/redis_object/collection.rb', line 244 def keys_by_index(idx,num=-1,reverse=false) keys = run_script(reverse ? :RevScript : :FwdScript, [temp_key, index_key(idx), key, num]) ListEnumerator.new(keys) do |y| keys.each do |member| y << member end end end |
#last ⇒ Object
298 299 300 |
# File 'lib/redis_object/collection.rb', line 298 def last class_const.find_by_key(super) end |
#latest ⇒ Object
215 216 217 |
# File 'lib/redis_object/collection.rb', line 215 def latest indexed(:created_at,5,true).first || first end |
#map(&block) ⇒ Object
332 333 334 |
# File 'lib/redis_object/collection.rb', line 332 def map(&block) each.map(&block) end |
#match(pkt) ⇒ Object
276 277 278 279 280 281 282 283 284 |
# File 'lib/redis_object/collection.rb', line 276 def match(pkt) Enumerator.new do |y| each do |i| if pkt.all? {|hk,va| i.get(hk)==va } y << i end end end end |
#objects ⇒ Object
290 291 292 |
# File 'lib/redis_object/collection.rb', line 290 def objects each.to_a end |
#push(obj) ⇒ Object
363 364 365 |
# File 'lib/redis_object/collection.rb', line 363 def push(obj) self << obj end |
#real_at(key) ⇒ Object
286 287 288 |
# File 'lib/redis_object/collection.rb', line 286 def real_at(key) class_const.find_by_key(key) end |
#remove! ⇒ Object
211 212 213 |
# File 'lib/redis_object/collection.rb', line 211 def remove! @owner.remove_collection! @name end |
#select(&block) ⇒ Object
336 337 338 339 340 341 342 343 344 345 |
# File 'lib/redis_object/collection.rb', line 336 def select(&block) return nil unless block_given? Enumerator.new do |y| each_index do |key| if (a = class_const.find_by_key(at(key))) && block.call(a) y << a end end end end |
#store ⇒ Object
371 372 373 |
# File 'lib/redis_object/collection.rb', line 371 def store class_const.store end |
#temp_key ⇒ Object
237 238 239 |
# File 'lib/redis_object/collection.rb', line 237 def temp_key "#{key}::zintersect_temp::#{RedisObject.new_id(4)}" end |