Class: Familia::List

Inherits:
RedisObject show all
Defined in:
lib/familia/redisobject.rb

Instance Attribute Summary

Attributes inherited from RedisObject

#cache, #name, #parent, #redis

Instance Method Summary collapse

Methods inherited from RedisObject

#class?, #clear_cache, #db, #dump_method, #echo, #exists?, #expire, #expireat, #from_redis, inherited, #initialize, #load_method, #move, #multi_from_redis, #parent?, #persist, #realttl, #rediskey, register, registration, #rename, #renamenx, #to_redis, #ttl, #type, #update_expiration

Constructor Details

This class inherits a constructor from Familia::RedisObject

Instance Method Details

#<<(v) ⇒ Object Also known as: add



323
324
325
# File 'lib/familia/redisobject.rb', line 323

def << v
  push v
end

#[](idx, count = nil) ⇒ Object Also known as: slice



344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/familia/redisobject.rb', line 344

def [] idx, count=nil
  if idx.is_a? Range
    range idx.first, idx.last
  elsif count
    case count <=> 0
    when 1  then range(idx, idx + count - 1)
    when 0  then []
    when -1 then nil
    end
  else
    at idx
  end
end

#at(idx) ⇒ Object



424
425
426
# File 'lib/familia/redisobject.rb', line 424

def at idx
  from_redis redis.lindex(rediskey, idx)
end

#collect(&blk) ⇒ Object



408
409
410
# File 'lib/familia/redisobject.rb', line 408

def collect &blk
  range.collect &blk
end

#collectraw(&blk) ⇒ Object



416
417
418
# File 'lib/familia/redisobject.rb', line 416

def collectraw &blk
  rangeraw.collect &blk
end

#delete(v, count = 0) ⇒ Object Also known as: remove, rem, del



359
360
361
# File 'lib/familia/redisobject.rb', line 359

def delete v, count=0
  redis.lrem rediskey, count, to_redis(v)
end

#each(&blk) ⇒ Object

def revmembers count=1 #TODO

range -count, 0

end



392
393
394
# File 'lib/familia/redisobject.rb', line 392

def each &blk
  range.each &blk
end

#each_with_index(&blk) ⇒ Object



396
397
398
# File 'lib/familia/redisobject.rb', line 396

def each_with_index &blk
  range.each_with_index &blk
end

#eachraw(&blk) ⇒ Object



400
401
402
# File 'lib/familia/redisobject.rb', line 400

def eachraw &blk
  rangeraw.each &blk
end

#eachraw_with_index(&blk) ⇒ Object



404
405
406
# File 'lib/familia/redisobject.rb', line 404

def eachraw_with_index &blk
  rangeraw.each_with_index &blk
end

#empty?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/familia/redisobject.rb', line 311

def empty?
  size == 0
end

#firstObject



428
429
430
# File 'lib/familia/redisobject.rb', line 428

def first
  at 0
end

#lastObject



432
433
434
# File 'lib/familia/redisobject.rb', line 432

def last
  at -1
end

#members(count = -1) ⇒ Object Also known as: all, to_a



375
376
377
378
379
# File 'lib/familia/redisobject.rb', line 375

def members count=-1
  echo :members, caller[0] if Familia.debug
  count -= 1 if count > 0
  range 0, count
end

#membersraw(count = -1) ⇒ Object



383
384
385
386
# File 'lib/familia/redisobject.rb', line 383

def membersraw count=-1
  count -= 1 if count > 0
  rangeraw 0, count
end

#popObject



336
337
338
# File 'lib/familia/redisobject.rb', line 336

def pop
  from_redis redis.rpop(rediskey)
end

#push(*values) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/familia/redisobject.rb', line 315

def push *values
  echo :push, caller[0] if Familia.debug
  values.flatten.compact.each { |v| redis.rpush rediskey, to_redis(v) }
  redis.ltrim rediskey, -@opts[:maxlength], -1 if @opts[:maxlength]
  update_expiration
  self
end

#range(sidx = 0, eidx = -1) ⇒ Object



366
367
368
369
# File 'lib/familia/redisobject.rb', line 366

def range sidx=0, eidx=-1
  el = rangeraw sidx, eidx
  multi_from_redis *el
end

#rangeraw(sidx = 0, eidx = -1) ⇒ Object



371
372
373
# File 'lib/familia/redisobject.rb', line 371

def rangeraw sidx=0, eidx=-1
  redis.lrange(rediskey, sidx, eidx)
end

#select(&blk) ⇒ Object



412
413
414
# File 'lib/familia/redisobject.rb', line 412

def select &blk
  range.select &blk
end

#selectraw(&blk) ⇒ Object



420
421
422
# File 'lib/familia/redisobject.rb', line 420

def selectraw &blk
  rangeraw.select &blk
end

#shiftObject



340
341
342
# File 'lib/familia/redisobject.rb', line 340

def shift
  from_redis redis.lpop(rediskey)
end

#sizeObject Also known as: length



306
307
308
# File 'lib/familia/redisobject.rb', line 306

def size
  redis.llen rediskey
end

#unshift(*values) ⇒ Object



328
329
330
331
332
333
334
# File 'lib/familia/redisobject.rb', line 328

def unshift *values
  values.flatten.compact.each { |v| redis.lpush rediskey, to_redis(v) }
  # TODO: test maxlength
  redis.ltrim rediskey, 0, @opts[:maxlength] - 1 if @opts[:maxlength]
  update_expiration
  self
end