Sha256: 8d8d8c05066272e7a0605f60ea98fcae204af0dd3349dbbdfb33dda61b5c75e4

Contents?: true

Size: 1.47 KB

Versions: 25

Compression:

Stored size: 1.47 KB

Contents

module RocketJob
  module Sliced
    module Writer
      # Internal class for uploading records into input slices
      class Input
        attr_reader :record_count

        # 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
        def self.collect(input, **args)
          writer = new(input, **args)
          yield(writer)
          writer.record_count
        ensure
          writer&.close
        end

        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

        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

        def close
          @input.insert(@slice) if @slice.size.positive?
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
rocketjob-6.0.0.rc3 lib/rocket_job/sliced/writer/input.rb
rocketjob-6.0.0.rc2 lib/rocket_job/sliced/writer/input.rb
rocketjob-6.0.0.rc1 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.4.1 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.4.0 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.4.0.beta2 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.4.0.beta1 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.3.3 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.3.2 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.3.1 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.3.0 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.2.0 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.2.0.beta3 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.2.0.beta2 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.2.0.beta1 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.1.1 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.1.0 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.0.0 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.0.0.rc1 lib/rocket_job/sliced/writer/input.rb
rocketjob-5.0.0.beta4 lib/rocket_job/sliced/writer/input.rb