Class: Familia::List

Inherits:
RedisType show all
Defined in:
lib/familia/types/list.rb

Instance Attribute Summary

Attributes inherited from RedisType

#dump_method, #keystring, #load_method, #opts, #parent

Instance Method Summary collapse

Methods inherited from RedisType

#class?, #db, inherited, #initialize, #parent?, #parent_class?, #parent_instance?, #redis, #rediskey, register, #ttl, #uri, valid_keys_only

Methods included from RedisType::Serialization

#from_redis, #multi_from_redis, #multi_from_redis_with_nil, #to_redis, #update_expiration

Methods included from RedisType::Commands

#delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #realttl, #rename, #renamenx, #type

Methods included from Base

add_feature

Constructor Details

This class inherits a constructor from Familia::RedisType

Instance Method Details

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



22
23
24
# File 'lib/familia/types/list.rb', line 22

def <<(val)
  push val
end

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/familia/types/list.rb', line 43

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



119
120
121
# File 'lib/familia/types/list.rb', line 119

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

#collect(&blk) ⇒ Object



103
104
105
# File 'lib/familia/types/list.rb', line 103

def collect(&blk)
  range.collect(&blk)
end

#collectraw(&blk) ⇒ Object



111
112
113
# File 'lib/familia/types/list.rb', line 111

def collectraw(&blk)
  rangeraw.collect(&blk)
end

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



58
59
60
# File 'lib/familia/types/list.rb', line 58

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

#each(&blk) ⇒ Object



87
88
89
# File 'lib/familia/types/list.rb', line 87

def each(&blk)
  range.each(&blk)
end

#each_with_index(&blk) ⇒ Object



91
92
93
# File 'lib/familia/types/list.rb', line 91

def each_with_index(&blk)
  range.each_with_index(&blk)
end

#eachraw(&blk) ⇒ Object



95
96
97
# File 'lib/familia/types/list.rb', line 95

def eachraw(&blk)
  rangeraw.each(&blk)
end

#eachraw_with_index(&blk) ⇒ Object



99
100
101
# File 'lib/familia/types/list.rb', line 99

def eachraw_with_index(&blk)
  rangeraw.each_with_index(&blk)
end

#empty?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/familia/types/list.rb', line 10

def empty?
  size.zero?
end

#firstObject



123
124
125
# File 'lib/familia/types/list.rb', line 123

def first
  at 0
end

#lastObject



127
128
129
# File 'lib/familia/types/list.rb', line 127

def last
  at(-1)
end

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



74
75
76
77
78
# File 'lib/familia/types/list.rb', line 74

def members(count = -1)
  echo :members, caller(1..1).first if Familia.debug
  count -= 1 if count.positive?
  range 0, count
end

#membersraw(count = -1)) ⇒ Object



82
83
84
85
# File 'lib/familia/types/list.rb', line 82

def membersraw(count = -1)
  count -= 1 if count.positive?
  rangeraw 0, count
end

#popObject



35
36
37
# File 'lib/familia/types/list.rb', line 35

def pop
  from_redis redis.rpop(rediskey)
end

#push(*values) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/familia/types/list.rb', line 14

def push *values
  echo :push, caller(1..1).first 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



65
66
67
68
# File 'lib/familia/types/list.rb', line 65

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

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



70
71
72
# File 'lib/familia/types/list.rb', line 70

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

#select(&blk) ⇒ Object



107
108
109
# File 'lib/familia/types/list.rb', line 107

def select(&blk)
  range.select(&blk)
end

#selectraw(&blk) ⇒ Object



115
116
117
# File 'lib/familia/types/list.rb', line 115

def selectraw(&blk)
  rangeraw.select(&blk)
end

#shiftObject



39
40
41
# File 'lib/familia/types/list.rb', line 39

def shift
  from_redis redis.lpop(rediskey)
end

#sizeObject Also known as: length



5
6
7
# File 'lib/familia/types/list.rb', line 5

def size
  redis.llen rediskey
end

#unshift(*values) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/familia/types/list.rb', line 27

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