lib/database_cleaner/configuration.rb in database_cleaner-0.5.2 vs lib/database_cleaner/configuration.rb in database_cleaner-0.6.0.rc.1

- old
+ new

@@ -1,129 +1,77 @@ +require 'database_cleaner/base' + module DatabaseCleaner class NoStrategySetError < StandardError; end class NoORMDetected < StandardError; end class UnknownStrategySpecified < ArgumentError; end - module ActiveRecord - def self.available_strategies - %w[truncation transaction] + class << self + def [](orm,opts = {}) + raise NoORMDetected if orm.nil? + @connections ||= [] + cleaner = DatabaseCleaner::Base.new(orm,opts) + connections.push cleaner + cleaner end - end - module DataMapper - def self.available_strategies - %w[truncation transaction] + def app_root=(desired_root) + @app_root = desired_root end - end - - module MongoMapper - def self.available_strategies - %w[truncation] + def app_root + @app_root || Dir.pwd end - end - module Mongoid - def self.available_strategies - %w[truncation] + def connections + @connections ||= [::DatabaseCleaner::Base.new] end - end - module CouchPotato - def self.available_strategies - %w[truncation] + def strategy=(stratagem) + self.connections.each { |connect| connect.strategy = stratagem } + remove_duplicates end - end - class << self - - def create_strategy(*args) - strategy, *strategy_args = args - orm_strategy(strategy).new(*strategy_args) + def orm=(orm) + self.connections.each { |connect| connect.orm = orm } + remove_duplicates end - def clean_with(*args) - strategy = create_strategy(*args) - strategy.clean - strategy - end - - alias clean_with! clean_with - - 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 know strategy along with initialization params." - end - end - - def orm=(orm_string) - @orm = orm_string - end - def start - strategy.start + self.connections.each { |connection| connection.start } end def clean - strategy.clean + self.connections.each { |connection| connection.clean } end - alias clean! clean - - private - - def strategy - return @strategy if @strategy - raise NoStrategySetError, "Please set a strategy with DatabaseCleaner.strategy=." + def clean_with(stratagem) + self.connections.each { |connection| connection.clean_with stratagem } end - def orm_strategy(strategy) - require "database_cleaner/#{orm}/#{strategy}" - orm_module.const_get(strategy.to_s.capitalize) - rescue LoadError => e - raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}" - end - - - def orm - @orm ||=begin - if defined? ::ActiveRecord - 'active_record' - elsif defined? ::DataMapper - 'data_mapper' - elsif defined? ::MongoMapper - 'mongo_mapper' - elsif defined? ::Mongoid - 'mongoid' - elsif defined? ::CouchPotato - 'couch_potato' - else - raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, or CouchPotato loaded?" - end + def remove_duplicates + temp = [] + self.connections.each do |connect| + temp.push connect unless temp.include? connect end + @connections = temp end - - - def orm_module - case orm - when 'active_record' - DatabaseCleaner::ActiveRecord - when 'data_mapper' - DatabaseCleaner::DataMapper - when 'mongo_mapper' - DatabaseCleaner::MongoMapper - when 'mongoid' - DatabaseCleaner::Mongoid - when 'couch_potato' - DatabaseCleaner::CouchPotato + + def orm_module(symbol) + case symbol + when :active_record + DatabaseCleaner::ActiveRecord + when :data_mapper + DatabaseCleaner::DataMapper + when :mongo + DatabaseCleaner::Mongo + when :mongoid + DatabaseCleaner::Mongoid + when :mongo_mapper + DatabaseCleaner::MongoMapper + when :couch_potato + DatabaseCleaner::CouchPotato end end - end - end