Class: RocketJob::Sliced::Writer::Input
- Inherits:
-
Object
- Object
- RocketJob::Sliced::Writer::Input
- Defined in:
- lib/rocket_job/sliced/writer/input.rb
Overview
Internal class for uploading records into input slices
Instance Attribute Summary collapse
-
#record_count ⇒ Object
readonly
Returns the value of attribute record_count.
Class Method Summary collapse
-
.collect(input, **args) ⇒ Object
Batch collection of lines into slices.
Instance Method Summary collapse
- #<<(line) ⇒ Object
- #close ⇒ Object
-
#initialize(input, on_first: nil) ⇒ Input
constructor
A new instance of Input.
Constructor Details
#initialize(input, on_first: nil) ⇒ Input
Returns a new instance of Input.
23 24 25 26 27 28 29 30 |
# File 'lib/rocket_job/sliced/writer/input.rb', line 23 def initialize(input, on_first: nil) @on_first = on_first @batch_count = 0 @record_count = 0 @input = input @record_number = 1 @slice = @input.new(first_record_number: @record_number) end |
Instance Attribute Details
#record_count ⇒ Object (readonly)
Returns the value of attribute record_count.
6 7 8 |
# File 'lib/rocket_job/sliced/writer/input.rb', line 6 def record_count @record_count end |
Class Method Details
.collect(input, **args) ⇒ Object
Batch collection of lines into slices.
Parameters
on_first: [Proc]
Block to call on the first line only, instead of storing in the slice.
Useful for extracting the header row
Default: nil
15 16 17 18 19 20 21 |
# File 'lib/rocket_job/sliced/writer/input.rb', line 15 def self.collect(input, **args) writer = new(input, **args) yield(writer) writer.record_count ensure writer&.close end |
Instance Method Details
#<<(line) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rocket_job/sliced/writer/input.rb', line 32 def <<(line) @record_number += 1 if @on_first @on_first.call(line) @on_first = nil return self end @slice << line @batch_count += 1 @record_count += 1 if @batch_count >= @input.slice_size @input.insert(@slice) @batch_count = 0 @slice = @input.new(first_record_number: @record_number) end self end |
#close ⇒ Object
50 51 52 |
# File 'lib/rocket_job/sliced/writer/input.rb', line 50 def close @input.insert(@slice) if @slice.size.positive? end |