Class: Batcher::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/batcher/process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, opts = {}) ⇒ Process

Returns a new instance of Process.



5
6
7
8
# File 'lib/batcher/process.rb', line 5

def initialize(scope, opts = {})
  @scope      =  scope
  @batch_size = opts[:batch_size] || 1000
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



3
4
5
# File 'lib/batcher/process.rb', line 3

def batch_size
  @batch_size
end

Instance Method Details

#each(&block) ⇒ Object



10
11
12
13
14
# File 'lib/batcher/process.rb', line 10

def each(&block)
  iterator.each do |object|
    yield object
  end
end

#iteratorObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/batcher/process.rb', line 16

def iterator
  Enumerator.new do |yielder|
    offset = 0
    count  = @scope.count
    begin
      @scope.limit(batch_size).offset(offset).each do |record|
        yielder << record
      end
      offset += batch_size
    end until (batch_size + offset - 1) > count
  end
end