Class: CSVUtils::CSVCompare

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_utils/csv_compare.rb

Overview

Compares two CSV files sorted on the same key and yields what it takes to make the secondary file match the primary one: records to create, update or delete. Sort both files first, CSVSort can do it.

Updates are only reported for the update_comparison_columns; subclass and override update_row? for other rules.

Examples:

comparer = CSVUtils::CSVCompare.new('primary.csv', ['updated_at']) { |src, dest| src['id'].to_i <=> dest['id'].to_i }
comparer.compare('secondary.csv') { |action, record| puts "#{action} #{record['id']}" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(primary_data_file, update_comparison_columns = nil, csv_options = {}) {|src, dest| ... } ⇒ CSVCompare

Returns a new instance of CSVCompare.

Parameters:

  • primary_data_file (String) —

    path of the source of truth

  • update_comparison_columns (Array<String>, nil) (defaults to: nil) —

    columns compared on matching records; without them no updates are yielded

  • csv_options (Hash) (defaults to: {}) —

    options passed to CSV.open for both files, ex: col_sep: "\t", or encoding: 'BINARY' to read bytes that aren't valid UTF-8; the :encoding defaults to 'bom|utf-8'

Yield Parameters:

  • src (Hash{String => String}) —

    record from the primary file

  • dest (Hash{String => String}) —

    record from the secondary file

Yield Returns:

  • (Integer) —

    negative, 0 or positive like <=>, using the same order both files are sorted by



31
32
33
34
35
36
# File 'lib/csv_utils/csv_compare.rb', line 31

def initialize(primary_data_file, update_comparison_columns = nil, csv_options = {}, &block)
  @primary_data_file = primary_data_file
  @update_comparison_columns = update_comparison_columns
  @csv_options = csv_options
  @compare_proc = block
end

Instance Attribute Details

#compare_proc ⇒ Proc (readonly)

Returns compares the key columns of a primary and a secondary record, returning -1, 0 or 1.

Returns:

  • (Proc) —

    compares the key columns of a primary and a secondary record, returning -1, 0 or 1



19
20
21
# File 'lib/csv_utils/csv_compare.rb', line 19

def compare_proc
  @compare_proc
end

#csv_options ⇒ Hash (readonly)

Returns options passed to CSV.open for both files.

Returns:

  • (Hash) —

    options passed to CSV.open for both files



21
22
23
# File 'lib/csv_utils/csv_compare.rb', line 21

def csv_options
  @csv_options
end

#primary_data_file ⇒ String (readonly)

Returns path of the source of truth.

Returns:

  • (String) —

    path of the source of truth



14
15
16
# File 'lib/csv_utils/csv_compare.rb', line 14

def primary_data_file
  @primary_data_file
end

#update_comparison_columns ⇒ Array<String>? (readonly)

Returns columns, present in both files, that trigger an update when they differ (ex: updated_at, a timestamp or a hash of the row).

Returns:

  • (Array<String>, nil) —

    columns, present in both files, that trigger an update when they differ (ex: updated_at, a timestamp or a hash of the row)



17
18
19
# File 'lib/csv_utils/csv_compare.rb', line 17

def update_comparison_columns
  @update_comparison_columns
end

Instance Method Details

#compare(secondary_data_file, check_order: true) {|action, record| ... } ⇒ void

This method returns an undefined value.

Walks both files and yields each difference.

Parameters:

  • secondary_data_file (String) —

    path of the file to bring in line with the primary file

  • check_order (Boolean) (defaults to: true) —

    raise when a file isn't sorted the way the compare block expects, by comparing each record with the one before it in the same file. A file is only checked when the block compares its first record equal to itself, so a block written for different headers in each file skips the check.

Yield Parameters:

  • action (Symbol) —

    :create (only in the primary file), :update (in both, with changed update_comparison_columns) or :delete (only in the secondary file)

  • record (CSVIterator::RowWrapper) —

    the primary record for :create and :update, the secondary record for :delete; a Hash of header to value that knows the line it starts on

Raises:



50
51
52
53
54
55
56
57
# File 'lib/csv_utils/csv_compare.rb', line 50

def compare(secondary_data_file, check_order: true, &)
  @check_order = check_order
  open_csv(primary_data_file) do |src|
    open_csv(secondary_data_file) do |dest|
      compare_records(src, dest, &)
    end
  end
end