Class: Redis::List
- Inherits:
-
BaseObject
- Object
- BaseObject
- Redis::List
- 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
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Works like push.
- #==(x) ⇒ Object
-
#[](index, length = nil) ⇒ Object
(also: #slice)
Same functionality as Ruby arrays.
-
#at(index) ⇒ Object
Return the value at the given index.
-
#delete(name, count = 0) ⇒ Object
Delete the element(s) from the list that match name.
-
#each(&block) ⇒ Object
Iterate through each member of the set.
-
#empty? ⇒ Boolean
Returns true if there are no elements in the list.
-
#first ⇒ Object
Return the first element in the list.
-
#last ⇒ Object
Return the last element in the list.
-
#length ⇒ Object
(also: #size)
Return the length of the list.
-
#pop ⇒ Object
Remove a member from the end of the list.
-
#push(value) ⇒ Object
Add a member to the end of the list.
-
#range(start_index, end_index) ⇒ Object
Return a range of values from
start_index
toend_index
. -
#shift ⇒ Object
Remove a member from the start of the list.
- #to_s ⇒ Object
-
#unshift(value) ⇒ Object
Add a member to the start of the list.
-
#values ⇒ Object
(also: #get)
Return all values in the list.
Methods included from Helpers::Serialize
Methods included from Helpers::CoreCommands
#exists?, #expire, #expireat, #move, #rename, #renamenx, #sort, #ttl, #type
Methods inherited from BaseObject
Constructor Details
This class inherits a constructor from Redis::BaseObject
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
16 17 18 |
# File 'lib/redis/list.rb', line 16 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/redis/list.rb', line 16 def @options end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
16 17 18 |
# File 'lib/redis/list.rb', line 16 def redis @redis end |
Instance Method Details
#<<(value) ⇒ Object
Works like push. Can chain together: list << ‘a’ << ‘b’
19 20 21 22 |
# File 'lib/redis/list.rb', line 19 def <<(value) push(value) self # for << 'a' << 'b' end |
#==(x) ⇒ Object
119 120 121 |
# File 'lib/redis/list.rb', line 119 def ==(x) values == x end |
#[](index, length = nil) ⇒ Object Also known as: slice
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.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/redis/list.rb', line 56 def [](index, length=nil) if index.is_a? Range last = index.exclude_end? ? (index.last - 1) : index.last range(index.first, last) elsif length case length <=> 0 when 1 then range(index, index + length - 1) when 0 then [] when -1 then nil # Ruby does this (a bit weird) end else at(index) end end |
#at(index) ⇒ Object
Return the value at the given index. Can also use familiar list syntax. Redis: LINDEX
94 95 96 |
# File 'lib/redis/list.rb', line 94 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
76 77 78 |
# File 'lib/redis/list.rb', line 76 def delete(name, count=0) redis.lrem(key, count, to_redis(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.
82 83 84 |
# File 'lib/redis/list.rb', line 82 def each(&block) values.each(&block) end |
#empty? ⇒ Boolean
Returns true if there are no elements in the list. Redis: LLEN == 0
115 116 117 |
# File 'lib/redis/list.rb', line 115 def empty? length == 0 end |
#first ⇒ Object
Return the first element in the list. Redis: LINDEX(0)
99 100 101 |
# File 'lib/redis/list.rb', line 99 def first at(0) end |
#last ⇒ Object
Return the last element in the list. Redis: LINDEX(-1)
104 105 106 |
# File 'lib/redis/list.rb', line 104 def last at(-1) end |
#length ⇒ Object Also known as: size
Return the length of the list. Aliased as size. Redis: LLEN
109 110 111 |
# File 'lib/redis/list.rb', line 109 def length redis.llen(key) end |
#pop ⇒ Object
Remove a member from the end of the list. Redis: RPOP
31 32 33 |
# File 'lib/redis/list.rb', line 31 def pop from_redis redis.rpop(key) end |
#push(value) ⇒ Object
Add a member to the end of the list. Redis: RPUSH
25 26 27 28 |
# File 'lib/redis/list.rb', line 25 def push(value) redis.rpush(key, to_redis(value)) redis.ltrim(key, -[:maxlength], -1) if [:maxlength] 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
88 89 90 |
# File 'lib/redis/list.rb', line 88 def range(start_index, end_index) from_redis redis.lrange(key, start_index, end_index) end |
#shift ⇒ Object
Remove a member from the start of the list. Redis: LPOP
42 43 44 |
# File 'lib/redis/list.rb', line 42 def shift from_redis redis.lpop(key) end |
#to_s ⇒ Object
123 124 125 |
# File 'lib/redis/list.rb', line 123 def to_s values.join(', ') end |
#unshift(value) ⇒ Object
Add a member to the start of the list. Redis: LPUSH
36 37 38 39 |
# File 'lib/redis/list.rb', line 36 def unshift(value) redis.lpush(key, to_redis(value)) redis.ltrim(key, 0, [:maxlength] - 1) if [:maxlength] end |
#values ⇒ Object Also known as: get
Return all values in the list. Redis: LRANGE(0,-1)
47 48 49 50 |
# File 'lib/redis/list.rb', line 47 def values v = from_redis range(0, -1) v.nil? ? [] : v end |