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, members) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mock_redis/set_methods.rb', line 9

def sadd(key, members)
  members_class = members.class
  members = Array(members).map(&:to_s)
  assert_has_args(members, 'sadd')

  with_set_at(key) do |s|
    size_before = s.size
    if members.size > 1
      members.reverse_each { |m| s << m }
      s.size - size_before
    else
      added = !!s.add?(members.first)
      if members_class == Array
        s.size - size_before
      else
        added
      end
    end
  end
end

#sadd?(key, members) ⇒ Boolean

Returns:

  • (Boolean)


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

def sadd?(key, members)
  res = sadd(key, members)
  res.is_a?(Numeric) ? res > 0 : res
end

#scard(key) ⇒ Object



35
36
37
# File 'lib/mock_redis/set_methods.rb', line 35

def scard(key)
  with_set_at(key, &:length)
end

#sdiff(*keys) ⇒ Object



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

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

#sdiffstore(destination, *keys) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/mock_redis/set_methods.rb', line 44

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



52
53
54
55
56
57
58
# File 'lib/mock_redis/set_methods.rb', line 52

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

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

#sinterstore(destination, *keys) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/mock_redis/set_methods.rb', line 60

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



68
69
70
# File 'lib/mock_redis/set_methods.rb', line 68

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

#smembers(key) ⇒ Object



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

def smembers(key)
  with_set_at(key, &:to_a).map(&:dup).reverse
end

#smismember(key, *members) ⇒ Object



72
73
74
75
76
# File 'lib/mock_redis/set_methods.rb', line 72

def smismember(key, *members)
  with_set_at(key) do |set|
    members.flatten.map { |m| set.include?(m.to_s) }
  end
end

#smove(src, dest, member) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mock_redis/set_methods.rb', line 82

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, count = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mock_redis/set_methods.rb', line 95

def spop(key, count = nil)
  with_set_at(key) do |set|
    if count.nil?
      member = set.first
      set.delete(member)
      member
    else
      members = []
      count.times do
        member = set.first
        break if member.nil?
        set.delete(member)
        members << member
      end
      members
    end
  end
end

#srandmember(key, count = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mock_redis/set_methods.rb', line 114

def srandmember(key, count = nil)
  members = with_set_at(key, &:to_a)
  if count
    if count > 0
      members.sample(count)
    else
      Array.new(count.abs) { members[rand(members.length)] }
    end
  else
    members[rand(members.length)]
  end
end

#srem(key, members) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mock_redis/set_methods.rb', line 127

def srem(key, members)
  with_set_at(key) do |s|
    if members.is_a?(Array)
      orig_size = s.size
      members = members.map(&:to_s)
      s.delete_if { |m| members.include?(m) }
      orig_size - s.size
    else
      !!s.delete?(members.to_s)
    end
  end
end

#srem?(key, members) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
# File 'lib/mock_redis/set_methods.rb', line 140

def srem?(key, members)
  res = srem(key, members)
  res.is_a?(Numeric) ? res > 0 : res
end

#sscan(key, cursor, opts = {}) ⇒ Object



145
146
147
# File 'lib/mock_redis/set_methods.rb', line 145

def sscan(key, cursor, opts = {})
  common_scan(smembers(key), cursor, opts)
end

#sscan_each(key, opts = {}, &block) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/mock_redis/set_methods.rb', line 149

def sscan_each(key, opts = {}, &block)
  return to_enum(:sscan_each, key, opts) unless block_given?
  cursor = 0
  loop do
    cursor, keys = sscan(key, cursor, opts)
    keys.each(&block)
    break if cursor == '0'
  end
end

#sunion(*keys) ⇒ Object



159
160
161
162
# File 'lib/mock_redis/set_methods.rb', line 159

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

#sunionstore(destination, *keys) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/mock_redis/set_methods.rb', line 164

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