Module: MockRedis::SetMethods

Includes:
Assertions, UtilityMethods
Included in:
Database
Defined in:
lib/mock_redis/set_methods.rb

Instance Method Summary collapse

Instance Method Details

#sadd(key, member) ⇒ Object



9
10
11
# File 'lib/mock_redis/set_methods.rb', line 9

def sadd(key, member)
  with_set_at(key) {|s| !!s.add?(member.to_s)}
end

#scard(key) ⇒ Object



13
14
15
# File 'lib/mock_redis/set_methods.rb', line 13

def scard(key)
  with_set_at(key) {|s| s.length}
end

#sdiff(*keys) ⇒ Object



17
18
19
20
# File 'lib/mock_redis/set_methods.rb', line 17

def sdiff(*keys)
  assert_has_args(keys, 'sdiff')
  with_sets_at(*keys) {|*sets| sets.reduce(&:-)}.to_a
end

#sdiffstore(destination, *keys) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/mock_redis/set_methods.rb', line 22

def sdiffstore(destination, *keys)
  assert_has_args(keys, 'sdiffstore')
  with_set_at(destination) do |set|
    set.replace(sdiff(*keys))
  end
  scard(destination)
end

#sinter(*keys) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mock_redis/set_methods.rb', line 30

def sinter(*keys)
  assert_has_args(keys, 'sinter')

  with_sets_at(*keys) do |*sets|
    sets.reduce(&:&).to_a
  end
end

#sinterstore(destination, *keys) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/mock_redis/set_methods.rb', line 38

def sinterstore(destination, *keys)
  assert_has_args(keys, 'sinterstore')
  with_set_at(destination) do |set|
    set.replace(sinter(*keys))
  end
  scard(destination)
end

#sismember(key, member) ⇒ Object



46
47
48
# File 'lib/mock_redis/set_methods.rb', line 46

def sismember(key, member)
  with_set_at(key) {|s| s.include?(member.to_s)}
end

#smembers(key) ⇒ Object



50
51
52
# File 'lib/mock_redis/set_methods.rb', line 50

def smembers(key)
  with_set_at(key, &:to_a)
end

#smove(src, dest, member) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mock_redis/set_methods.rb', line 54

def smove(src, dest, member)
  member = member.to_s

  with_sets_at(src, dest) do |src_set, dest_set|
    if src_set.delete?(member)
      dest_set.add(member)
      true
    else
      false
    end
  end
end

#spop(key) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/mock_redis/set_methods.rb', line 67

def spop(key)
  with_set_at(key) do |set|
    member = set.first
    set.delete(member)
    member
  end
end

#srandmember(key) ⇒ Object



75
76
77
# File 'lib/mock_redis/set_methods.rb', line 75

def srandmember(key)
  with_set_at(key, &:first)
end

#srem(key, member) ⇒ Object



79
80
81
# File 'lib/mock_redis/set_methods.rb', line 79

def srem(key, member)
  with_set_at(key) {|s| !!s.delete?(member.to_s)}
end

#sunion(*keys) ⇒ Object



83
84
85
86
# File 'lib/mock_redis/set_methods.rb', line 83

def sunion(*keys)
  assert_has_args(keys, 'sunion')
  with_sets_at(*keys) {|*sets| sets.reduce(&:+).to_a}
end

#sunionstore(destination, *keys) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/mock_redis/set_methods.rb', line 88

def sunionstore(destination, *keys)
  assert_has_args(keys, 'sunionstore')
  with_set_at(destination) do |dest_set|
    dest_set.replace(sunion(*keys))
  end
  scard(destination)
end