fold all three DC::Generic:: modules into a single DC::Strategy base class.

This commit is contained in:
Micah Geisel 2020-04-10 08:55:11 -07:00 committed by Micah Geisel
parent 992e693ba9
commit 02a35cb4f6
4 changed files with 50 additions and 80 deletions

View File

@ -1,29 +0,0 @@
module ::DatabaseCleaner
module Generic
module Base
def self.included(base)
base.extend(ClassMethods)
end
def db
:default
end
def cleaning(&block)
begin
start
yield
ensure
clean
end
end
module ClassMethods
def available_strategies
%W[]
end
end
end
end
end

View File

@ -1,11 +0,0 @@
module DatabaseCleaner
module Generic
module Transaction
def initialize(opts = {})
if !opts.empty?
raise ArgumentError, "Options are not available for transaction strategies."
end
end
end
end
end

View File

@ -1,40 +0,0 @@
module DatabaseCleaner
module Generic
module Truncation
def initialize(opts={})
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :reset_ids, :cache_tables]).empty?
raise ArgumentError, "The only valid options are :only, :except, :pre_count, :reset_ids or :cache_tables. You specified #{opts.keys.join(',')}."
end
if opts.has_key?(:only) && opts.has_key?(:except)
raise ArgumentError, "You may only specify either :only or :except. Doing both doesn't really make sense does it?"
end
@only = opts[:only]
@tables_to_exclude = Array( (opts[:except] || []).dup ).flatten
@tables_to_exclude += migration_storage_names
@pre_count = opts[:pre_count]
@reset_ids = opts[:reset_ids]
@cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
end
def start
#included for compatability reasons, do nothing if you don't need to
end
def clean
raise NotImplementedError
end
private
def tables_to_truncate
raise NotImplementedError
end
# overwrite in subclasses
# default implementation given because migration storage need not be present
def migration_storage_names
%w[]
end
end
end
end

View File

@ -0,0 +1,50 @@
# Abstract strategy class for orm adapter gems to subclass
module DatabaseCleaner
class Strategy
# Override this method if the strategy accepts options
def initialize(options=nil)
if options
name = self.class.name.sub("DatabaseCleaner::","").sub("::"," ") # e.g. "ActiveRecord Transaction"
raise ArgumentError, "No options are available for the #{name} strategy."
end
end
def db
@db ||= :default
end
attr_writer :db
# Override this method to start a database transaction if the strategy uses them
def start
end
# Override this method with the actual cleaning procedure. Its the only mandatory method implementation.
def clean
raise NotImplementedError
end
def cleaning(&block)
begin
start
yield
ensure
clean
end
end
private
# Optional helper method for use in strategies with :only and :except options
def tables_to_clean all_tables, only: [], except: []
if only.any?
if except.any?
raise ArgumentError, "You may only specify either :only or :except. Doing both doesn't really make sense does it?"
end
only
else
all_tables - except
end
end
end
end