Class: JobIteration::ActiveRecordEnumerator
- Inherits:
-
Object
- Object
- JobIteration::ActiveRecordEnumerator
- Defined in:
- lib/job-iteration/active_record_enumerator.rb
Overview
Builds Enumerator based on ActiveRecord Relation. Supports enumerating on rows and batches.
Constant Summary collapse
- SQL_DATETIME_WITH_NSEC =
"%Y-%m-%d %H:%M:%S.%N"
Instance Method Summary collapse
- #batches ⇒ Object
-
#initialize(relation, columns: nil, batch_size: 100, cursor: nil) ⇒ ActiveRecordEnumerator
constructor
A new instance of ActiveRecordEnumerator.
- #records ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(relation, columns: nil, batch_size: 100, cursor: nil) ⇒ ActiveRecordEnumerator
Returns a new instance of ActiveRecordEnumerator.
10 11 12 13 14 15 |
# File 'lib/job-iteration/active_record_enumerator.rb', line 10 def initialize(relation, columns: nil, batch_size: 100, cursor: nil) @relation = relation @batch_size = batch_size @columns = Array(columns || "#{relation.table_name}.#{relation.primary_key}") @cursor = cursor end |
Instance Method Details
#batches ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/job-iteration/active_record_enumerator.rb', line 27 def batches cursor = finder_cursor Enumerator.new(method(:size)) do |yielder| while (records = cursor.next_batch(@batch_size)) yielder.yield(records, cursor_value(records.last)) if records.any? end end end |
#records ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/job-iteration/active_record_enumerator.rb', line 17 def records Enumerator.new(method(:size)) do |yielder| batches.each do |batch, _| batch.each do |record| yielder.yield(record, cursor_value(record)) end end end end |
#size ⇒ Object
36 37 38 |
# File 'lib/job-iteration/active_record_enumerator.rb', line 36 def size @relation.count(:all) end |