File: truncation_mixin.rb

package info (click to toggle)
ruby-database-cleaner 1.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 756 kB
  • sloc: ruby: 4,852; makefile: 10; sh: 4
file content (39 lines) | stat: -rw-r--r-- 913 bytes parent folder | download | duplicates (3)
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
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 @db.nil? || @db == :default
          ::Mongoid::Clients.default
        else
          ::Mongoid::Clients.with_name(@db)
        end
      end

      def collections
        if db != :default
          database.use(db)
        end

        database.collections.collect { |c| c.namespace.split('.',2)[1] }

        # database['system.namespaces'].find(:name => { '$not' => /\.system\.|\$/ }).to_a.map do |collection|
        #   _, name = collection['name'].split('.', 2)
        #   name
        # end
      end

    end
  end
end