Sha256: 253667a6a22f889586a1468167ddb1ed48db3f06dc5e5407161302136b7b00dd

Contents?: true

Size: 948 Bytes

Versions: 21

Compression:

Stored size: 948 Bytes

Contents

#!/usr/bin/env ruby
require "csv"
require_relative "../lib/file_path_generator"

def columns_changed?(previous_row, row, change_columns)
  changed = false
  change_columns.each do |column|
    changed ||= previous_row[column] != row[column]
  end
  changed
end

def file_name_from_changed_columns(row, change_columns)
  change_columns.map { |column| row[column] }.join("_") + ".csv"
end

change_columns = ARGV[0].split(/,/).map(&:to_i)
input_file = ARGV[1]

previous_row = output_csv = nil
file_path_generator = FilePathGenerator.new()

CSV.foreach(input_file) do |row|
  if previous_row.nil? || (previous_row && columns_changed?(previous_row, row, change_columns))
    output_csv.close if output_csv
    output_file_name = file_name_from_changed_columns(row, change_columns)
    path = file_path_generator.path_for_new_file(output_file_name)
    output_csv = CSV.open(path, "w")
  end
  output_csv << row
  previous_row = row
end
output_csv.close

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
advance-0.4.11 bin/csv_split_on_change_nh.rb
advance-0.4.10 bin/csv_split_on_change_nh.rb
advance-0.4.8 bin/csv_split_on_change_nh.rb
advance-0.4.7 bin/csv_split_on_change_nh.rb
advance-0.4.6 bin/csv_split_on_change_nh.rb
advance-0.4.5 bin/csv_split_on_change_nh.rb
advance-0.4.4 bin/csv_split_on_change_nh.rb
advance-0.4.3 bin/csv_split_on_change_nh.rb
advance-0.4.2 bin/csv_split_on_change_nh.rb
advance-0.4.1 bin/csv_split_on_change_nh.rb
advance-0.3.18 bin/csv_split_on_change_nh.rb
advance-0.3.16 bin/csv_split_on_change_nh.rb
advance-0.3.15 bin/csv_split_on_change_nh.rb
advance-0.3.14 bin/csv_split_on_change_nh.rb
advance-0.3.13 bin/csv_split_on_change_nh.rb
advance-0.3.12 bin/csv_split_on_change_nh.rb
advance-0.3.11 bin/csv_split_on_change_nh.rb
advance-0.3.10 bin/csv_split_on_change_nh.rb
advance-0.3.9 bin/csv_split_on_change_nh.rb
advance-0.3.8 bin/csv_split_on_change_nh.rb