<commit_before>module DatabaseCleaner
  module Mongo
    module Truncation

      def clean
        if @only
          collections.each { |c| c.remove if @only.include?(c.name) }
        else
          collections.each { |c| c.remove unless @tables_to_exclude.include?(c.name) }
        end
        true
      end

      private

      def collections_cache
        @@collections_cache ||= {}
      end

      def collections
        collections_cache[database.name] ||= database.collections.select { |c| c.name !~ /^system\./ }
      end
    end
  end
end<commit_msg>Prevent cacheing of new databases<commit_after>module DatabaseCleaner
  module Mongo
    module Truncation

      def clean
        if @only
          collections.each { |c| c.remove if @only.include?(c.name) }
        else
          collections.each { |c| c.remove unless @tables_to_exclude.include?(c.name) }
        end
        true
      end

      private

      def collections_cache
        @@collections_cache ||= {}
      end

      def mongoid_collection_names
        @@mongoid_collection_names ||= Hash.new{|h,k| h[k]=[]}.tap do |names|
          ObjectSpace.each_object(Class) do |klass|
            next unless klass.ancestors.include?(::Mongoid::Document)
            next if klass.embedded
            next if klass.collection_name.empty?
            names[klass.db.name] << klass.collection_name
          end
        end
      end

      def collections
        return collections_cache[database.name] if collections_cache.has_key?(database.name)
        db_collections = database.collections.select { |c| c.name !~ /^system\./ }

        missing_collections = db_collections.map(&:name) - mongoid_collection_names[database.name]

        if missing_collections.empty?
          collections_cache[database.name] = db_collections
        else
          puts "Not cacheing collection names. Missing these from models: #{missing_collections.inspect}"
        end

        return db_collections
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09031936127744511, 'fraction_numerical': 0.0, 'mean_word_length': 2.1875993640699525, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2965446', 'n_tokens_mistral': 584, 'n_tokens_neox': 578, 'n_words': 132}