-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ActiveRecordBatchProcessor (no specs)
- Loading branch information
1 parent
28c03df
commit bde5fa2
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require_relative "null_processor" | ||
|
||
module Processor | ||
module Data | ||
class ActiveRecordBatchProcessor < NullProcessor | ||
attr_reader :source | ||
|
||
def initialize(source:, presenter: -> { Hash.new }, output:, batch_size: 1000) | ||
@source = source | ||
@presenter = presenter | ||
@output = output | ||
@batch_size = batch_size | ||
end | ||
|
||
def records | ||
source.find_each(batch_size: batch_size) | ||
end | ||
|
||
def process(record) | ||
output.write presenter.call(record) | ||
"OK" | ||
end | ||
|
||
def record_id(record) | ||
record.id | ||
end | ||
|
||
def finalize | ||
output.close | ||
end | ||
|
||
def total_records | ||
@total_records ||= source.count | ||
end | ||
|
||
private | ||
attr_reader :presenter, :output, :batch_size | ||
end | ||
end | ||
end |