Class: Sunspot::Batcher

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sunspot/batcher.rb

Overview

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.

Defined Under Namespace

Classes: NoCurrentBatchError

Instance Method Summary collapse

Constructor Details

#initializeBatcher

Returns a new instance of Batcher.



23
24
25
# File 'lib/sunspot/batcher.rb', line 23

def initialize
  @stack = []
end

Instance Method Details

#batching?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/sunspot/batcher.rb', line 45

def batching?
  depth > 0
end

#concat(values) ⇒ Object



58
59
60
# File 'lib/sunspot/batcher.rb', line 58

def concat(values)
  current.concat values
end

#currentObject



27
28
29
# File 'lib/sunspot/batcher.rb', line 27

def current
  @stack.last or start_new
end

#depthObject



41
42
43
# File 'lib/sunspot/batcher.rb', line 41

def depth
  @stack.length
end

#each(&block) ⇒ Object



49
50
51
# File 'lib/sunspot/batcher.rb', line 49

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

#end_currentObject



35
36
37
38
39
# File 'lib/sunspot/batcher.rb', line 35

def end_current
  fail NoCurrentBatchError if @stack.empty?

  @stack.pop
end

#push(value) ⇒ Object Also known as: <<



53
54
55
# File 'lib/sunspot/batcher.rb', line 53

def push(value)
  current << value
end

#start_newObject



31
32
33
# File 'lib/sunspot/batcher.rb', line 31

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