simplify configuration internals.

This commit is contained in:
Micah Geisel 2018-05-23 00:53:49 -07:00
parent 85767fa701
commit b38160f17e
2 changed files with 21 additions and 34 deletions

View file

@ -2,21 +2,20 @@ require 'database_cleaner/base'
module DatabaseCleaner module DatabaseCleaner
class NoORMDetected < StandardError; end class NoORMDetected < StandardError; end
class UnknownStrategySpecified < ArgumentError; end class UnknownStrategySpecified < ArgumentError; end
class Configuration class Configuration
def [](orm,opts = {}) def initialize
@cleaners ||= {}
end
# FIXME this method conflates creation with lookup... both a command and a query. yuck.
def [](orm, opts = {})
raise NoORMDetected unless orm raise NoORMDetected unless orm
init_cleaners @cleaners.fetch([orm, opts]) { add_cleaner(orm, opts) }
# TODO: deprecate
# this method conflates creation with lookup. Both a command and a query. Yuck.
if @cleaners.has_key? [orm, opts]
@cleaners[[orm, opts]]
else
add_cleaner(orm, opts)
end
end end
attr_accessor :app_root, :logger attr_accessor :app_root, :logger
def app_root def app_root
@ -45,8 +44,6 @@ module DatabaseCleaner
connections.each { |connection| connection.clean } connections.each { |connection| connection.clean }
end end
alias clean! clean
def cleaning(&inner_block) def cleaning(&inner_block)
connections.inject(inner_block) do |curr_block, connection| connections.inject(inner_block) do |curr_block, connection|
proc { connection.cleaning(&curr_block) } proc { connection.cleaning(&curr_block) }
@ -57,39 +54,31 @@ module DatabaseCleaner
connections.each { |connection| connection.clean_with(*args) } connections.each { |connection| connection.clean_with(*args) }
end end
# TODO deprecate and remove the following aliases:
alias clean! clean
alias clean_with! clean_with alias clean_with! clean_with
# TODO deprecate and then privatize the following methods: # TODO deprecate and then privatize the following methods:
def init_cleaners def init_cleaners
@cleaners ||= {} $stderr.puts "Calling `DatabaseCleaner.init_cleaners` is deprecated, and will be removed in database_cleaner 2.0 with no replacement."
# ghetto ordered hash.. maintains 1.8 compat and old API
@connections ||= []
end end
def add_cleaner(orm,opts = {}) def add_cleaner(orm, opts = {})
init_cleaners @cleaners[[orm, opts]] = ::DatabaseCleaner::Base.new(orm, opts)
cleaner = DatabaseCleaner::Base.new(orm,opts)
@cleaners[[orm, opts]] = cleaner
@connections << cleaner
cleaner
end end
def connections def connections
# double yuck.. can't wait to deprecate this whole class... add_cleaner(:autodetect) if @cleaners.none?
unless defined?(@cleaners) && @cleaners @cleaners.values
autodetected = ::DatabaseCleaner::Base.new
add_cleaner(autodetected.orm)
end
@connections
end end
def remove_duplicates def remove_duplicates
temp = [] @cleaners = @cleaners.reduce({}) do |cleaners, (key, value)|
connections.each do |connect| cleaners[key] = value unless cleaners.values.include?(value)
temp.push connect unless temp.include? connect cleaners
end end
@connections = temp
end end
end end
end end

View file

@ -1,5 +1,4 @@
module ArrayHelper module ArrayHelper
def zipmap(array, vals) def zipmap(array, vals)
Hash[*(array.zip(vals).flatten)] Hash[*(array.zip(vals).flatten)]
end end
@ -11,7 +10,6 @@ module DatabaseCleaner
# hackey, hack.. connections needs to stick around until I can properly deprecate the API # hackey, hack.. connections needs to stick around until I can properly deprecate the API
def connections_stub(array) def connections_stub(array)
@cleaners = ArrayHelper.zipmap((1..array.size).to_a, array) @cleaners = ArrayHelper.zipmap((1..array.size).to_a, array)
@connections = array
end end
end end
end end