class Cloaca::Operations::AddNumericIndexColumn

Public Class Methods

new( column_delimiter:, index_delta:, index_header:, index_seed:, input:, output:, ** ) click to toggle source
# File lib/cloaca/operations/add_numeric_index_column.rb, line 4
def initialize(
  column_delimiter:,
  index_delta:,
  index_header:,
  index_seed:,
  input:,
  output:,
  **
)
  @column_delimiter = column_delimiter
  @index_delta = index_delta
  @index_header = index_header
  @index_seed = index_header ? index_seed - index_delta : index_seed
  @input = input
  @output = output
end

Public Instance Methods

run!() click to toggle source
# File lib/cloaca/operations/add_numeric_index_column.rb, line 21
def run!
  use_big_decimal_math? ? run_with_big_decimals : run_with_integers
end

Private Instance Methods

run_with_big_decimals() click to toggle source
# File lib/cloaca/operations/add_numeric_index_column.rb, line 27
def run_with_big_decimals
  cumulative_delta = BigDecimal.new(@index_seed)
  @input.each_with_index do |line, index|
    @output << (index == 0 && @index_header ? @index_header : cumulative_delta.to_digits)
    @output << @column_delimiter
    @output << line
    cumulative_delta = cumulative_delta + @index_delta
  end
end
run_with_integers() click to toggle source
# File lib/cloaca/operations/add_numeric_index_column.rb, line 37
def run_with_integers
  cumulative_delta = @index_seed.to_i
  @input.each_with_index do |line, index|
    @output << (index == 0 && @index_header ? @index_header : cumulative_delta.to_i)
    @output << @column_delimiter
    @output << line
    cumulative_delta = cumulative_delta + @index_delta
  end
end
use_big_decimal_math?() click to toggle source
# File lib/cloaca/operations/add_numeric_index_column.rb, line 47
def use_big_decimal_math?
  (@index_seed != @index_seed.to_i) || (@index_delta != @index_delta.to_i)
end