Class: Blendris::RedisSet

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

Overview

RedisSet is a wrapper to the Redis SET 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 = {}) ⇒ RedisSet

Returns a new instance of RedisSet.



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

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

Instance Method Details

#<<(value) ⇒ Object



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

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

  self
ensure
  notify_changed
end

#countObject



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

def count
  redis.scard key
end

#delete(value) ⇒ Object



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

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

#eachObject



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

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

  self
end

#getObject



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

def get
  self
end

#intersect!(other) ⇒ Object

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



60
61
62
63
64
# File 'lib/blendris/set.rb', line 60

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

#set(*values) ⇒ Object



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

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.



67
68
69
70
71
# File 'lib/blendris/set.rb', line 67

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