Skip to content

Commit 1ad6ac1

Browse files
botandrose-machinebotandrose
authored andcommitted
Subclass strategies from the new DC::Strategy base class from database_cleaner-core, instead of including the DC::Generic::* family of modules.
1 parent 2a6367b commit 1ad6ac1

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

lib/database_cleaner/redis.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@
22
require "database_cleaner/core"
33
require "database_cleaner/redis/truncation"
44

5+
module DatabaseCleaner
6+
module Redis
7+
def self.available_strategies
8+
[:truncation]
9+
end
10+
end
11+
end
12+
513
DatabaseCleaner[:redis].strategy = :truncation

lib/database_cleaner/redis/truncation.rb

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,42 @@
1-
require "database_cleaner/generic/base"
2-
require "database_cleaner/generic/truncation"
1+
require "database_cleaner/strategy"
32

43
module DatabaseCleaner
54
module Redis
6-
def self.available_strategies
7-
%i[truncation]
8-
end
9-
10-
def self.default_strategy
11-
available_strategies.first
12-
end
13-
14-
class Truncation
15-
include DatabaseCleaner::Generic::Base
16-
include DatabaseCleaner::Generic::Truncation
17-
18-
def db
19-
@db ||= :default
5+
class Truncation < Strategy
6+
def initialize only: [], except: []
7+
@only = only
8+
@except = except
209
end
21-
attr_writer :db
22-
23-
alias_method :url, :db
2410

2511
def clean
26-
if @only
27-
@only.each do |term|
28-
connection.keys(term).each { |k| connection.del k }
29-
end
30-
elsif @tables_to_exclude
31-
keys_except = []
32-
@tables_to_exclude.each { |term| keys_except += connection.keys(term) }
33-
connection.keys.each { |k| connection.del(k) unless keys_except.include?(k) }
34-
else
12+
only = expand_keys(@only)
13+
except = expand_keys(@except)
14+
15+
if only.none? && except.none?
3516
connection.flushdb
17+
else
18+
tables_to_clean(connection.keys, only: only, except: except).each do |key|
19+
connection.del key
20+
end
3621
end
37-
connection.quit unless url == :default
22+
23+
connection.quit unless db == :default
3824
end
3925

4026
private
4127

28+
def expand_keys keys
29+
keys.flat_map { |key| connection.keys(key) }
30+
end
31+
4232
def connection
4333
@connection ||= begin
44-
if url == :default
34+
if db == :default
4535
::Redis.new
4636
elsif db.is_a?(::Redis) # pass directly the connection
4737
db
4838
else
49-
::Redis.new(url: url)
39+
::Redis.new(url: db)
5040
end
5141
end
5242
end

spec/database_cleaner/redis_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
module DatabaseCleaner
66
RSpec.describe Redis do
77
it_behaves_like "a database_cleaner adapter"
8-
9-
it "has a default_strategy of truncation" do
10-
expect(described_class.default_strategy).to eq(:truncation)
11-
end
128
end
139
end
1410

0 commit comments

Comments
 (0)