Class: Blendris::RedisSet

Inherits:
Object
  • Object
show all
Includes:
RedisNode, Enumerable
Defined in:
lib/blendris/set.rb

Overview

RedisSet is a wrapper to the Redis SET data type.

Instance Attribute Summary

Attributes included from RedisNode

#key

Instance Method Summary collapse

Methods included from RedisNode

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

Methods included from RedisAccessor

database=, flushdb, #generate_key, #in_temporary_set, redis, #redis

Methods included from Utils

#blank, #camelize, #constantize, #sanitize_key

Constructor Details

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

Returns a new instance of RedisSet.



10
11
12
13
14
# File 'lib/blendris/set.rb', line 10

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

Instance Method Details

#<<(value) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/blendris/set.rb', line 36

def <<(value)
  [ value ].flatten.compact.each do |v|
    redis.sadd key, v
  end

  self
ensure
  notify_changed
end

#delete(value) ⇒ Object



50
51
52
53
54
# File 'lib/blendris/set.rb', line 50

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

#eachObject



16
17
18
19
20
21
22
# File 'lib/blendris/set.rb', line 16

def each
  redis.smembers(key).each do |value|
    yield value
  end

  self
end

#getObject



46
47
48
# File 'lib/blendris/set.rb', line 46

def get
  self
end

#intersect!(other) ⇒ Object

Set this set’s members to the intersection of this set and the given set.



57
58
59
60
61
# File 'lib/blendris/set.rb', line 57

def intersect!(other)
  redis.sinterstore key, key, other.key
ensure
  notify_changed
end

#set(*values) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/blendris/set.rb', line 24

def set(*values)
  self.clear

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

  self
ensure
  notify_changed
end

#union!(other) ⇒ Object

Set this set’s members to the union of this set and the given set.



64
65
66
67
68
# File 'lib/blendris/set.rb', line 64

def union!(other)
  redis.sunionstore key, key, other.key
ensure
  notify_changed
end