Class: CSVUtils::CSVCompare
- Inherits:
-
Object
- Object
- CSVUtils::CSVCompare
- 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.
Instance Attribute Summary collapse
-
#compare_proc ⇒ Proc
readonly
Compares the key columns of a primary and a secondary record, returning -1, 0 or 1.
-
#csv_options ⇒ Hash
readonly
Options passed to CSV.open for both files.
-
#primary_data_file ⇒ String
readonly
Path of the source of truth.
-
#update_comparison_columns ⇒ Array<String>?
readonly
Columns, present in both files, that trigger an update when they differ (ex: updated_at, a timestamp or a hash of the row).
Instance Method Summary collapse
-
#compare(secondary_data_file, check_order: true) {|action, record| ... } ⇒ void
Walks both files and yields each difference.
-
#initialize(primary_data_file, update_comparison_columns = nil, csv_options = {}) {|src, dest| ... } ⇒ CSVCompare
constructor
A new instance of CSVCompare.
Constructor Details
#initialize(primary_data_file, update_comparison_columns = nil, csv_options = {}) {|src, dest| ... } ⇒ CSVCompare
Returns a new instance of CSVCompare.
31 32 33 34 35 36 |
# File 'lib/csv_utils/csv_compare.rb', line 31 def initialize(primary_data_file, update_comparison_columns = nil, = {}, &block) @primary_data_file = primary_data_file @update_comparison_columns = update_comparison_columns @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.
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.
21 22 23 |
# File 'lib/csv_utils/csv_compare.rb', line 21 def @csv_options end |
#primary_data_file ⇒ String (readonly)
Returns 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).
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.
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 |