Class: Blendris::RedisList

Inherits:
RedisNode show all
Includes:
Enumerable
Defined in:
lib/blendris/list.rb

Overview

RedisList is a wrapper for the Redis LIST data type.

Instance Attribute Summary

Attributes inherited from RedisNode

#key

Instance Method Summary collapse

Methods inherited from RedisNode

#clear, #exists?, #notify_changed, #rename, #type

Methods included from RedisAccessor

#generate_key, #in_temporary_set, redis, #redis

Methods included from Utils

#blank, #camelize, #constantize, #pairify, #sanitize_key

Constructor Details

#initialize(key, options = {}) ⇒ RedisList

Returns a new instance of RedisList.



9
10
11
12
13
# File 'lib/blendris/list.rb', line 9

def initialize(key, options = {})
  @key = key.to_s
  @options = options
  @on_change = options[:on_change]
end

Instance Method Details

#<<(value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/blendris/list.rb', line 33

def <<(value)
  values = [ value ].flatten.compact

  values.flatten.compact.each do |v|
    redis.rpush key, v
  end

  notify_changed if values.count > 0

  self
end

#countObject



55
56
57
# File 'lib/blendris/list.rb', line 55

def count
  redis.llen key
end

#delete(value) ⇒ Object



49
50
51
52
53
# File 'lib/blendris/list.rb', line 49

def delete(value)
  redis.lrem key, 0, value
ensure
  notify_changed
end

#eachObject



27
28
29
30
31
# File 'lib/blendris/list.rb', line 27

def each
  redis.lrange(key, 0, -1).each do |value|
    yield value
  end
end

#getObject



45
46
47
# File 'lib/blendris/list.rb', line 45

def get
  self
end

#set(*values) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/blendris/list.rb', line 15

def set(*values)
  # Remove all of the old values.
  self.clear

  # Add all of the new values.
  self << values

  self
ensure
  notify_changed
end