Class: Batcher::Process
- Inherits:
-
Object
- Object
- Batcher::Process
- Defined in:
- lib/batcher/process.rb
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
Returns the value of attribute batch_size.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(scope, opts = {}) ⇒ Process
constructor
A new instance of Process.
- #iterator ⇒ Object
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_size ⇒ Object
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 |
#iterator ⇒ Object
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 |