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

Attributes included from Features

#features_enabled

Instance Method Summary collapse

Methods inherited from RedisType

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

Methods included from Features

#feature

Methods included from RedisType::Serialization

#from_redis, #multi_from_redis, #multi_from_redis_with_nil, #to_redis

Methods included from RedisType::Commands

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

Methods included from Base

add_feature, #generate_id, #update_expiration, #uuid

Constructor Details

This class inherits a constructor from Familia::RedisType

Instance Method Details

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



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

def <<(val)
  push val
end

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



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

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



121
122
123
# File 'lib/familia/types/list.rb', line 121

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

#collect(&blk) ⇒ Object



105
106
107
# File 'lib/familia/types/list.rb', line 105

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

#collectraw(&blk) ⇒ Object



113
114
115
# File 'lib/familia/types/list.rb', line 113

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

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



60
61
62
# File 'lib/familia/types/list.rb', line 60

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

#each(&blk) ⇒ Object



89
90
91
# File 'lib/familia/types/list.rb', line 89

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

#each_with_index(&blk) ⇒ Object



93
94
95
# File 'lib/familia/types/list.rb', line 93

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

#eachraw(&blk) ⇒ Object



97
98
99
# File 'lib/familia/types/list.rb', line 97

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

#eachraw_with_index(&blk) ⇒ Object



101
102
103
# File 'lib/familia/types/list.rb', line 101

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



125
126
127
# File 'lib/familia/types/list.rb', line 125

def first
  at 0
end

#lastObject



129
130
131
# File 'lib/familia/types/list.rb', line 129

def last
  at(-1)
end

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



76
77
78
79
80
# File 'lib/familia/types/list.rb', line 76

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



84
85
86
87
# File 'lib/familia/types/list.rb', line 84

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

#popObject



37
38
39
# File 'lib/familia/types/list.rb', line 37

def pop
  from_redis redis.rpop(rediskey)
end

#push(*values) ⇒ Object Also known as: append



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



67
68
69
70
# File 'lib/familia/types/list.rb', line 67

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

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



72
73
74
# File 'lib/familia/types/list.rb', line 72

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

#select(&blk) ⇒ Object



109
110
111
# File 'lib/familia/types/list.rb', line 109

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

#selectraw(&blk) ⇒ Object



117
118
119
# File 'lib/familia/types/list.rb', line 117

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

#shiftObject



41
42
43
# File 'lib/familia/types/list.rb', line 41

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 Also known as: prepend



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

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