Sha256: eaaad8f5b37f1dd7e0d9681ebe714bddb989f7d3d9347aa0a0463d3e8d3d5a69

Contents?: true

Size: 1.08 KB

Versions: 18

Compression:

Stored size: 1.08 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 of 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

18 entries across 18 versions & 5 rubygems

Version Path
sunspot-2.1.0 lib/sunspot/batcher.rb
sunspot-2.0.0 lib/sunspot/batcher.rb
sunspot-2.0.0.pre.130115 lib/sunspot/batcher.rb
gojee-sunspot-2.0.5 lib/sunspot/batcher.rb
sunspot-2.0.0.pre.120925 lib/sunspot/batcher.rb
sunspot_solr-2.0.0.pre.120924 sunspot/lib/sunspot/batcher.rb
sunspot_rails-2.0.0.pre.120924 sunspot/lib/sunspot/batcher.rb
sunspot-2.0.0.pre.120924 sunspot/lib/sunspot/batcher.rb
gojee-sunspot-2.0.4 lib/sunspot/batcher.rb
gojee-sunspot-2.0.2 lib/sunspot/batcher.rb
sunspot-2.0.0.pre.120720 lib/sunspot/batcher.rb
sunspot-2.0.0.pre.120417 lib/sunspot/batcher.rb
sunspot-2.0.0.pre.120415 lib/sunspot/batcher.rb
erichummel-sunspot-2.0.0.pre.111215d lib/sunspot/batcher.rb
erichummel-sunspot-2.0.0.pre.111215c lib/sunspot/batcher.rb
erichummel-sunspot-2.0.0.pre.111215b lib/sunspot/batcher.rb
erichummel-sunspot-2.0.0.pre.111215a lib/sunspot/batcher.rb
erichummel-sunspot-2.0.0.pre.111215 lib/sunspot/batcher.rb