-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdeletion.rb
49 lines (42 loc) · 1 KB
/
deletion.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require "database_cleaner/strategy"
module DatabaseCleaner
module Redis
class Deletion < Strategy
def initialize only: [], except: []
@only = only
@except = except
end
def clean
if @only.none? && @except.none?
connection.flushdb
else
keys_to_delete.each do |key|
connection.del key
end
end
connection.quit unless db == :default
end
private
def keys_to_delete
only = expand_keys(@only)
except = expand_keys(@except)
only = connection.keys if only.none?
(only - except)
end
def expand_keys keys
keys.flat_map { |key| connection.keys(key) }
end
def connection
@connection ||= begin
if db == :default
::Redis.new
elsif db.is_a?(::Redis) # pass directly the connection
db
else
::Redis.new(url: db)
end
end
end
end
end
end