Sha256: c8224339e145590f6f1d793b5e72db859e8a7128778589449a42bff5e1b02c90

Contents?: true

Size: 1.09 KB

Versions: 17

Compression:

Stored size: 1.09 KB

Contents

module Sunspot
  #
  # Keeps a stack of batches and helps out when Indexer is asked to batch documents.
  #
  # If the client does something like 
  #   
  #   Sunspot.batch do
  #     some_code_here
  #     which_triggers_some_other_code
  #     which_again_calls
  #     Sunspot.batch { ... }
  #   end
  #
  # it is the Batcher's job to keep track of these nestings. The inner will
  # be sent off to be indexed first.
  #
  class Batcher
    include Enumerable

    # Raised if you ask to end current, but no current exists
    class NoCurrentBatchError < StandardError; end

    def initialize
      @stack = []
    end

    def current
      @stack.last or start_new
    end

    def start_new
      (@stack << []).last
    end

    def end_current
      fail NoCurrentBatchError if @stack.empty?

      @stack.pop
    end

    def depth
      @stack.length
    end

    def batching?
      depth > 0
    end

    def each(&block)
      current.each(&block)
    end

    def push(value)
      current << value
    end
    alias << push

    def concat(values)
      current.concat values
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
sunspot-2.7.1 lib/sunspot/batcher.rb
sunspot-2.7.0 lib/sunspot/batcher.rb
sunspot-2.6.0 lib/sunspot/batcher.rb
sunspot-2.5.0 lib/sunspot/batcher.rb
sunspot-2.4.0 lib/sunspot/batcher.rb
sunspot-2.3.0 lib/sunspot/batcher.rb
sunspot-2.2.8 lib/sunspot/batcher.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/sunspot-2.2.7/lib/sunspot/batcher.rb
sunspot-2.2.7 lib/sunspot/batcher.rb
sunspot-2.2.6 lib/sunspot/batcher.rb
sunspot-2.2.5 lib/sunspot/batcher.rb
sunspot-2.2.4 lib/sunspot/batcher.rb
sunspot-2.2.3 lib/sunspot/batcher.rb
sunspot-2.2.2 lib/sunspot/batcher.rb
sunspot-2.2.1 lib/sunspot/batcher.rb
sunspot-2.2.0 lib/sunspot/batcher.rb
sunspot-2.1.1 lib/sunspot/batcher.rb