Class: Familia::List

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

Instance Attribute Summary

Attributes inherited from RedisObject

#cache, #name, #opts, #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, #qstamp, #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



361
362
363
# File 'lib/familia/redisobject.rb', line 361

def << v
  push v
end

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



382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/familia/redisobject.rb', line 382

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



462
463
464
# File 'lib/familia/redisobject.rb', line 462

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

#collect(&blk) ⇒ Object



446
447
448
# File 'lib/familia/redisobject.rb', line 446

def collect &blk
  range.collect &blk
end

#collectraw(&blk) ⇒ Object



454
455
456
# File 'lib/familia/redisobject.rb', line 454

def collectraw &blk
  rangeraw.collect &blk
end

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



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

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



430
431
432
# File 'lib/familia/redisobject.rb', line 430

def each &blk
  range.each &blk
end

#each_with_index(&blk) ⇒ Object



434
435
436
# File 'lib/familia/redisobject.rb', line 434

def each_with_index &blk
  range.each_with_index &blk
end

#eachraw(&blk) ⇒ Object



438
439
440
# File 'lib/familia/redisobject.rb', line 438

def eachraw &blk
  rangeraw.each &blk
end

#eachraw_with_index(&blk) ⇒ Object



442
443
444
# File 'lib/familia/redisobject.rb', line 442

def eachraw_with_index &blk
  rangeraw.each_with_index &blk
end

#empty?Boolean

Returns:

  • (Boolean)


349
350
351
# File 'lib/familia/redisobject.rb', line 349

def empty?
  size == 0
end

#firstObject



466
467
468
# File 'lib/familia/redisobject.rb', line 466

def first
  at 0
end

#lastObject



470
471
472
# File 'lib/familia/redisobject.rb', line 470

def last
  at -1
end

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



413
414
415
416
417
# File 'lib/familia/redisobject.rb', line 413

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

#membersraw(count = -1) ⇒ Object



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

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

#popObject



374
375
376
# File 'lib/familia/redisobject.rb', line 374

def pop
  from_redis redis.rpop(rediskey)
end

#push(*values) ⇒ Object



353
354
355
356
357
358
359
# File 'lib/familia/redisobject.rb', line 353

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



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

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

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



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

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

#select(&blk) ⇒ Object



450
451
452
# File 'lib/familia/redisobject.rb', line 450

def select &blk
  range.select &blk
end

#selectraw(&blk) ⇒ Object



458
459
460
# File 'lib/familia/redisobject.rb', line 458

def selectraw &blk
  rangeraw.select &blk
end

#shiftObject



378
379
380
# File 'lib/familia/redisobject.rb', line 378

def shift
  from_redis redis.lpop(rediskey)
end

#sizeObject Also known as: length



344
345
346
# File 'lib/familia/redisobject.rb', line 344

def size
  redis.llen rediskey
end

#unshift(*values) ⇒ Object



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

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