Permalink
Browse files

extracts #connection and caches it

I am wondering if the #quit call will mess things up.. I'm not familiar
with Redis so I've asked why it was added:

hbpoison@ddbaece
  • Loading branch information...
1 parent 5de339b commit 982d62cd4b051e83ce0da8009177c8e0b3161a73 @bmabey bmabey committed Jan 21, 2012
Showing with 14 additions and 14 deletions.
  1. +9 −0 lib/database_cleaner/redis/base.rb
  2. +5 −14 lib/database_cleaner/redis/truncation.rb
@@ -16,6 +16,15 @@ def db=(desired_db)
def db
@db || :default
end
+
+ alias url db
+
+ private
+
+ def connection
+ @connection ||= url == :default ? ::Redis.connect : ::Redis.connect(:url => url)
+ end
+
end
end
end
@@ -8,28 +8,19 @@ class Truncation
include ::DatabaseCleaner::Generic::Truncation
def clean
- url = db
- redis = url == :default ? default_redis : ::Redis.connect(:url => url)
if @only
@only.each do |term|
- redis.keys(term).each { |k| redis.del k }
+ connection.keys(term).each { |k| connection.del k }
end
elsif @tables_to_exclude
keys_except = []
- @tables_to_exclude.each { |term| keys_except += redis.keys(term) }
- redis.keys.each { |k| redis.del(k) unless keys_except.include?(k) }
+ @tables_to_exclude.each { |term| keys_except += connection.keys(term) }
+ connection.keys.each { |k| connection.del(k) unless keys_except.include?(k) }
else
- redis.flushdb
+ connection.flushdb
end
- redis.quit unless url == :default
+ connection.quit unless url == :default
end
-
- private
-
- def default_redis
- ::Redis.connect
- end
-
end
end
end

0 comments on commit 982d62c

Please sign in to comment.