Class: MongoBatch::Batcher
- Inherits:
-
Object
- Object
- MongoBatch::Batcher
- Defined in:
- lib/mongo_batch.rb
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
readonly
Returns the value of attribute batch_size.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#order_by ⇒ Object
readonly
Returns the value of attribute order_by.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
- #batches ⇒ Object
-
#initialize(query, options = {}) ⇒ Batcher
constructor
A new instance of Batcher.
- #options ⇒ Object
- #with_order ⇒ Object
Constructor Details
#initialize(query, options = {}) ⇒ Batcher
Returns a new instance of Batcher.
5 6 7 8 9 10 11 |
# File 'lib/mongo_batch.rb', line 5 def initialize(query, = {}) @query = query @batch_size = .fetch(:batch_size) { 1_000 } @to = .fetch(:to) { query.count } @offset = .fetch(:offset) { 0 } @order_by = .fetch(:order_by) { { _id: :asc } } end |
Instance Attribute Details
#batch_size ⇒ Object (readonly)
Returns the value of attribute batch_size.
3 4 5 |
# File 'lib/mongo_batch.rb', line 3 def batch_size @batch_size end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
3 4 5 |
# File 'lib/mongo_batch.rb', line 3 def offset @offset end |
#order_by ⇒ Object (readonly)
Returns the value of attribute order_by.
3 4 5 |
# File 'lib/mongo_batch.rb', line 3 def order_by @order_by end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
3 4 5 |
# File 'lib/mongo_batch.rb', line 3 def query @query end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
3 4 5 |
# File 'lib/mongo_batch.rb', line 3 def to @to end |
Instance Method Details
#batches ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mongo_batch.rb', line 13 def batches Enumerator.new(to) do |yielder| processed_so_far = offset offset.step(by: batch_size, to: to - batch_size).each do |offset| yielder << with_order.limit(batch_size).skip(offset) processed_so_far += batch_size end if processed_so_far < to last_limit = to - processed_so_far yielder << with_order.limit(last_limit).skip(processed_so_far) end end end |
#options ⇒ Object
33 34 35 |
# File 'lib/mongo_batch.rb', line 33 def query.criteria. end |
#with_order ⇒ Object
29 30 31 |
# File 'lib/mongo_batch.rb', line 29 def with_order .sort ? query : query.order_by(order_by) end |