Class: Redis::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Helpers::CoreCommands, Helpers::Serialize
Defined in:
lib/redis/list.rb

Overview

Class representing a Redis list. Instances of Redis::List are designed to behave as much like Ruby arrays as possible.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Serialize

#from_redis, #to_redis

Methods included from Helpers::CoreCommands

#exists?, #expire, #expireat, #move, #rename, #renamenx, #type

Constructor Details

#initialize(key, redis = $redis, options = {}) ⇒ List

Returns a new instance of List.



16
17
18
19
20
# File 'lib/redis/list.rb', line 16

def initialize(key, redis=$redis, options={})
  @key = key
  @redis = redis
  @options = options
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



14
15
16
# File 'lib/redis/list.rb', line 14

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/redis/list.rb', line 14

def options
  @options
end

#redisObject (readonly)

Returns the value of attribute redis.



14
15
16
# File 'lib/redis/list.rb', line 14

def redis
  @redis
end

Instance Method Details

#<<(value) ⇒ Object

Works like push. Can chain together: list << ‘a’ << ‘b’



23
24
25
26
# File 'lib/redis/list.rb', line 23

def <<(value)
  push(value)
  self  # for << 'a' << 'b'
end

#==(x) ⇒ Object



114
115
116
# File 'lib/redis/list.rb', line 114

def ==(x)
  values == x
end

#[](index, length = nil) ⇒ Object

Same functionality as Ruby arrays. If a single number is given, return just the element at that index using Redis: LINDEX. Otherwise, return a range of values using Redis: LRANGE.



57
58
59
60
61
62
63
64
65
# File 'lib/redis/list.rb', line 57

def [](index, length=nil)
  if index.is_a? Range
    range(index.first, index.last)
  elsif length
    range(index, length)
  else
    at(index)
  end
end

#at(index) ⇒ Object

Return the value at the given index. Can also use familiar list syntax. Redis: LINDEX



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

def at(index)
  from_redis redis.lindex(key, index)
end

#delete(name, count = 0) ⇒ Object

Delete the element(s) from the list that match name. If count is specified, only the first-N (if positive) or last-N (if negative) will be removed. Use .del to completely delete the entire key. Redis: LREM



71
72
73
# File 'lib/redis/list.rb', line 71

def delete(name, count=0)
  redis.lrem(key, count, name)  # weird api
end

#each(&block) ⇒ Object

Iterate through each member of the set. Redis::Objects mixes in Enumerable, so you can also use familiar methods like collect, detect, and so forth.



77
78
79
# File 'lib/redis/list.rb', line 77

def each(&block)
  values.each(&block)
end

#empty?Boolean

Returns true if there are no elements in the list. Redis: LLEN == 0

Returns:

  • (Boolean)


110
111
112
# File 'lib/redis/list.rb', line 110

def empty?
  length == 0
end

#firstObject

Return the first element in the list. Redis: LINDEX(0)



94
95
96
# File 'lib/redis/list.rb', line 94

def first
  at(0)
end

#lastObject

Return the last element in the list. Redis: LINDEX(-1)



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

def last
  at(-1)
end

#lengthObject Also known as: size

Return the length of the list. Aliased as size. Redis: LLEN



104
105
106
# File 'lib/redis/list.rb', line 104

def length
  redis.llen(key)
end

#popObject

Remove a member from the end of the list. Redis: RPOP



34
35
36
# File 'lib/redis/list.rb', line 34

def pop
  from_redis redis.rpop(key)
end

#push(value) ⇒ Object

Add a member to the end of the list. Redis: RPUSH



29
30
31
# File 'lib/redis/list.rb', line 29

def push(value)
  redis.rpush(key, to_redis(value))
end

#range(start_index, end_index) ⇒ Object

Return a range of values from start_index to end_index. Can also use the familiar list Ruby syntax. Redis: LRANGE



83
84
85
# File 'lib/redis/list.rb', line 83

def range(start_index, end_index)
  from_redis redis.lrange(key, start_index, end_index)
end

#shiftObject

Remove a member from the start of the list. Redis: LPOP



44
45
46
# File 'lib/redis/list.rb', line 44

def shift
  from_redis redis.lpop(key)
end

#to_sObject



118
119
120
# File 'lib/redis/list.rb', line 118

def to_s
  values.join(', ')
end

#unshift(value) ⇒ Object

Add a member to the start of the list. Redis: LPUSH



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

def unshift(value)
  redis.lpush(key, to_redis(value))
end

#valuesObject Also known as: get

Return all values in the list. Redis: LRANGE(0,-1)



49
50
51
# File 'lib/redis/list.rb', line 49

def values
  from_redis range(0, -1)
end