/usr/lib/ruby/vendor_ruby/database_cleaner/base.rb is in ruby-database-cleaner 1.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | require 'database_cleaner/null_strategy'
module DatabaseCleaner
class Base
def initialize(desired_orm = nil,opts = {})
if [:autodetect, nil, "autodetect"].include?(desired_orm)
autodetect
else
self.orm = desired_orm
end
self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model)
set_default_orm_strategy
end
def db=(desired_db)
self.strategy_db = desired_db
@db = desired_db
end
def strategy_db=(desired_db)
if strategy.respond_to? :db=
strategy.db = desired_db
elsif desired_db!= :default
raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
end
end
def db
@db ||= :default
end
def create_strategy(*args)
strategy, *strategy_args = args
orm_strategy(strategy).new(*strategy_args)
end
def clean_with(*args)
strategy = create_strategy(*args)
set_strategy_db strategy, self.db
strategy.clean
strategy
end
alias clean_with! clean_with
def set_strategy_db(strategy, desired_db)
if strategy.respond_to? :db=
strategy.db = desired_db
elsif desired_db != :default
raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
end
end
def strategy=(args)
strategy, *strategy_args = args
if strategy.is_a?(Symbol)
@strategy = create_strategy(*args)
elsif strategy_args.empty?
@strategy = strategy
else
raise ArgumentError, "You must provide a strategy object, or a symbol for a known strategy along with initialization params."
end
set_strategy_db @strategy, self.db
@strategy
end
def strategy
@strategy ||= NullStrategy
end
def orm=(desired_orm)
@orm = desired_orm.to_sym
end
def orm
@orm || autodetect
end
def start
strategy.start
end
def clean
strategy.clean
end
alias clean! clean
def cleaning(&block)
strategy.cleaning(&block)
end
def auto_detected?
!!@autodetected
end
#TODO make strategies directly comparable
def ==(other)
self.orm == other.orm && self.db == other.db
end
def autodetect_orm
if defined? ::ActiveRecord
:active_record
elsif defined? ::DataMapper
:data_mapper
elsif defined? ::MongoMapper
:mongo_mapper
elsif defined? ::Mongoid
:mongoid
elsif defined? ::CouchPotato
:couch_potato
elsif defined? ::Sequel
:sequel
elsif defined? ::Moped
:moped
elsif defined? ::Ohm
:ohm
elsif defined? ::Redis
:redis
end
end
private
def orm_module
::DatabaseCleaner.orm_module(orm)
end
def orm_strategy(strategy)
require "database_cleaner/#{orm.to_s}/#{strategy.to_s}"
orm_module.const_get(strategy.to_s.capitalize)
rescue LoadError
if orm_module.respond_to? :available_strategies
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
else
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM!"
end
end
def autodetect
@autodetected = true
@orm ||= autodetect_orm ||
raise(NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded?")
end
def set_default_orm_strategy
case orm
when :active_record, :data_mapper, :sequel
self.strategy = :transaction
when :mongo_mapper, :mongoid, :couch_potato, :moped, :ohm, :redis
self.strategy = :truncation
end
end
end
end
|