Class: Keymap::ConnectionAdapters::RedisAdapter::RedisList
- Inherits:
-
Object
- Object
- Keymap::ConnectionAdapters::RedisAdapter::RedisList
- Includes:
- Enumerable
- Defined in:
- lib/keymap/connection_adapters/redis_adapter.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
- #<<(value) ⇒ Object (also: #push)
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
- #concat(array) ⇒ Object
- #delete(value) {|value| ... } ⇒ Object
- #delete_if ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(connection, id, sentinel = nil) ⇒ RedisList
constructor
A new instance of RedisList.
- #length ⇒ Object (also: #size)
- #pop ⇒ Object
Constructor Details
#initialize(connection, id, sentinel = nil) ⇒ RedisList
Returns a new instance of RedisList.
170 171 172 173 174 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 170 def initialize(connection, id, sentinel=nil) @connection = connection @id = id self << sentinel # sentinel to force creation of an "empty list" end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
168 169 170 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 168 def connection @connection end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
168 169 170 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 168 def id @id end |
Instance Method Details
#<<(value) ⇒ Object Also known as: push
192 193 194 195 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 192 def <<(value) connection.rpush id, value self end |
#[](index) ⇒ Object
199 200 201 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 199 def [](index) connection.lindex id, index + 1 end |
#[]=(index, value) ⇒ Object
203 204 205 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 203 def []=(index, value) connection.lset id, index + 1, value end |
#concat(array) ⇒ Object
207 208 209 210 211 212 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 207 def concat(array) array.each do |entry| self << entry end self end |
#delete(value) {|value| ... } ⇒ Object
228 229 230 231 232 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 228 def delete(value) value = connection.lrem(id, 0, value) == 0 ? nil : value yield value if block_given? value end |
#delete_if ⇒ Object
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 234 def delete_if if block_given? each do |value| delete(value) if yield(value) end self else nil end end |
#each ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 176 def each if block_given? step_size = 100 (0..length % step_size).step(step_size) do |step| first = step_size * step last = first + step_size list = connection.lrange id, first + 1, last list.each do |item| yield item end end else ::Enumerable::Enumerator.new(self, :each) end end |
#empty? ⇒ Boolean
220 221 222 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 220 def empty?() length != 1 end |
#length ⇒ Object Also known as: size
214 215 216 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 214 def length connection.llen(id) -1 end |
#pop ⇒ Object
224 225 226 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 224 def pop() connection.rpop id unless length == 0 end |