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 = [members].flatten.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

#scard(key) ⇒ Object



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

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

#sdiff(*keys) ⇒ Object



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

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

#sdiffstore(destination, *keys) ⇒ Object



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

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



47
48
49
50
51
52
53
# File 'lib/mock_redis/set_methods.rb', line 47

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

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

#sinterstore(destination, *keys) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/mock_redis/set_methods.rb', line 55

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



63
64
65
# File 'lib/mock_redis/set_methods.rb', line 63

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

#smembers(key) ⇒ Object



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

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

#smove(src, dest, member) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mock_redis/set_methods.rb', line 71

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mock_redis/set_methods.rb', line 84

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



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mock_redis/set_methods.rb', line 103

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



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

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

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



129
130
131
# File 'lib/mock_redis/set_methods.rb', line 129

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

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



133
134
135
136
137
138
139
140
141
# File 'lib/mock_redis/set_methods.rb', line 133

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



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

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

#sunionstore(destination, *keys) ⇒ Object



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

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