separate mongodb ruby driver v2 into it's own module

This commit is contained in:
Jon Principe 2015-06-06 21:23:06 -04:00
parent f080668b98
commit dd14ae4b76
3 changed files with 55 additions and 28 deletions

View file

@ -0,0 +1,16 @@
module DatabaseCleaner
module Mongo2
def self.available_strategies
%w[truncation]
end
module Base
def db=(desired_db)
@db = desired_db
end
def db
@db || raise("You have not specified a database. (see Mongo2::Database)")
end
end
end
end

View file

@ -0,0 +1,37 @@
module DatabaseCleaner
module Mongo2
module TruncationMixin
def clean
if @only
collections.each { |c| database[c].find.delete_many if @only.include?(c) }
else
collections.each { |c| database[c].find.delete_many unless @tables_to_exclude.include?(c) }
end
true
end
private
def database
if not(@db.nil? or @db == :default)
::Mongoid::Clients.with_name(@db)
else
::Mongoid::Clients.default
end
end
def collections
if db != :default
database.use(db)
end
database['system.namespaces'].find(:name => { '$not' => /\.system\.|\$/ }).to_a.map do |collection|
_, name = collection['name'].split('.', 2)
name
end
end
end
end
end

View file

@ -1,6 +1,7 @@
require 'database_cleaner/mongoid/base'
require 'database_cleaner/generic/truncation'
require 'database_cleaner/mongo/truncation_mixin'
require 'database_cleaner/mongo2/truncation_mixin'
require 'database_cleaner/moped/truncation_base'
require 'mongoid/version'
@ -40,35 +41,8 @@ module DatabaseCleaner
else
def clean
if @only
collections.each { |c| database[c].find.delete_many if @only.include?(c) }
else
collections.each { |c| database[c].find.delete_many unless @tables_to_exclude.include?(c) }
end
true
end
include ::DatabaseCleaner::Mongo2::TruncationMixin
private
def collections
if db != :default
database.use(db)
end
database['system.namespaces'].find(:name => { '$not' => /\.system\.|\$/ }).to_a.map do |collection|
_, name = collection['name'].split('.', 2)
name
end
end
def database
if not(@db.nil? or @db == :default)
::Mongoid::Clients.with_name(@db)
else
::Mongoid::Clients.default
end
end
end
end
end