Sha256: 1d892ffc93de1c9c2bb9e14ce72ac182560760e0bd63ff6777fdac30b98a02a1

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module DatabaseFlusher
  module Mongoid
    class DeletionStrategy
      attr_reader :collections

      class Subscriber
        def initialize(strategy)
          @strategy = strategy
        end

        def started(event)
          if event.command_name == :insert || event.command_name == 'insert'.freeze
            collection = event.command['insert'.freeze]
            if collection
              @strategy.collections << collection
            end
          end
        end

        private

        def method_missing(*args, &block)
        end
      end

      def initialize
        @collections = Set.new
      end

      def start
        @subscriber ||= Mongo::Monitoring::Global.subscribe(
          Mongo::Monitoring::COMMAND,
          Subscriber.new(self)
        )
      end

      def stop
        raise NotImplementedError, "Mongo doesn't provide unsubscribe"
      end

      def clean
        return if collections.empty?
        # puts "Cleaning #{collections.inspect}"
        collections.each do |name|
          client = ::Mongoid::Clients.default
          client[name].delete_many
        end
        collections.clear
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
database_flusher-0.2.0 lib/database_flusher/mongoid/deletion_strategy.rb