Class: Keymap::ConnectionAdapters::RedisList
- Inherits:
-
Object
- Object
- Keymap::ConnectionAdapters::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.
171 172 173 174 175 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 171 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.
169 170 171 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 169 def connection @connection end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
169 170 171 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 169 def id @id end |
Instance Method Details
#<<(value) ⇒ Object Also known as: push
193 194 195 196 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 193 def <<(value) connection.rpush id, value self end |
#[](index) ⇒ Object
200 201 202 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 200 def [](index) connection.lindex id, index + 1 end |
#[]=(index, value) ⇒ Object
204 205 206 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 204 def []=(index, value) connection.lset id, index + 1, value end |
#concat(array) ⇒ Object
208 209 210 211 212 213 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 208 def concat array array.each do |entry| self << entry end self end |
#delete(value) {|value| ... } ⇒ Object
229 230 231 232 233 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 229 def delete(value) value = connection.lrem(id, 0, value) == 0 ? nil : value yield value if block_given? value end |
#delete_if ⇒ Object
235 236 237 238 239 240 241 242 243 244 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 235 def delete_if if block_given? each do |value| delete(value) if yield(value) end self else nil end end |
#each ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 177 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
221 222 223 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 221 def empty?() length != 1 end |
#length ⇒ Object Also known as: size
215 216 217 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 215 def length connection.llen(id) -1 end |
#pop ⇒ Object
225 226 227 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 225 def pop() connection.rpop id unless length == 0 end |