Sha256: e83e7c75b31547b730159e056dd8f3bd3863c21331e6be512c151f37bd9d033f

Contents?: true

Size: 977 Bytes

Versions: 2

Compression:

Stored size: 977 Bytes

Contents

module CouchPotato
  module Persistence
    class BulkSaveQueue
      attr_reader :callbacks
      
      def initialize
        @other_queues = []
        @callbacks = []
        @instances = []
      end
      
      def <<(instance)
        if own?
          @instances << instance
        else
          @other_queues.last << instance
        end
      end
      
      def push_queue(queue)
        @other_queues.push queue
      end
      
      def pop_queue
        @other_queues.pop
      end
      
      def own?
        @other_queues.empty?
      end
      
      def save(&callback)
        if own?
          @callbacks << callback if callback
          res = CouchPotato::Persistence.Db.bulk_save @instances
          @instances.clear
          @callbacks.each do |_callback|
            _callback.call res
          end
          @callbacks.clear
        else
          @other_queues.last.callbacks << callback if callback
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
langalex-couch_potato-0.1.1 lib/couch_potato/persistence/bulk_save_queue.rb
langalex-couch_potato-0.1 lib/couch_potato/persistence/bulk_save_queue.rb