Class: CSVUtils::CSVCompare
- Inherits:
-
Object
- Object
- CSVUtils::CSVCompare
- Defined in:
- lib/csv_utils/csv_compare.rb
Overview
CSVUtils::CSVCompare purpose is to determine which rows in the secondary_data_file need to be created, deleted or updated **requires both CSV files to be sorted on the same columns, CSVUtils::CSVSort can accomplish this In order to receive updates, update_comparison_columns must configured or use inheritance and change the update_row? method
Instance Attribute Summary collapse
-
#compare_proc ⇒ Object
readonly
primary_data_file is the source of truth compare_proc used to compare the id column(s) update_comparison_columns column(s) to compare for equality, ex: updated_at, timestamp, hash caveat: update_comparison_columns need to be in both csv files.
-
#primary_data_file ⇒ Object
readonly
primary_data_file is the source of truth compare_proc used to compare the id column(s) update_comparison_columns column(s) to compare for equality, ex: updated_at, timestamp, hash caveat: update_comparison_columns need to be in both csv files.
-
#update_comparison_columns ⇒ Object
readonly
primary_data_file is the source of truth compare_proc used to compare the id column(s) update_comparison_columns column(s) to compare for equality, ex: updated_at, timestamp, hash caveat: update_comparison_columns need to be in both csv files.
Instance Method Summary collapse
- #compare(secondary_data_file) ⇒ Object
-
#initialize(primary_data_file, update_comparison_columns = nil, &block) ⇒ CSVCompare
constructor
A new instance of CSVCompare.
Constructor Details
#initialize(primary_data_file, update_comparison_columns = nil, &block) ⇒ CSVCompare
Returns a new instance of CSVCompare.
13 14 15 16 17 |
# File 'lib/csv_utils/csv_compare.rb', line 13 def initialize(primary_data_file, update_comparison_columns=nil, &block) @primary_data_file = primary_data_file @update_comparison_columns = update_comparison_columns @compare_proc = block end |
Instance Attribute Details
#compare_proc ⇒ Object (readonly)
primary_data_file is the source of truth compare_proc used to compare the id column(s) update_comparison_columns column(s) to compare for equality, ex: updated_at, timestamp, hash
caveat: update_comparison_columns need to be in both csv files
9 10 11 |
# File 'lib/csv_utils/csv_compare.rb', line 9 def compare_proc @compare_proc end |
#primary_data_file ⇒ Object (readonly)
primary_data_file is the source of truth compare_proc used to compare the id column(s) update_comparison_columns column(s) to compare for equality, ex: updated_at, timestamp, hash
caveat: update_comparison_columns need to be in both csv files
9 10 11 |
# File 'lib/csv_utils/csv_compare.rb', line 9 def primary_data_file @primary_data_file end |
#update_comparison_columns ⇒ Object (readonly)
primary_data_file is the source of truth compare_proc used to compare the id column(s) update_comparison_columns column(s) to compare for equality, ex: updated_at, timestamp, hash
caveat: update_comparison_columns need to be in both csv files
9 10 11 |
# File 'lib/csv_utils/csv_compare.rb', line 9 def update_comparison_columns @update_comparison_columns end |
Instance Method Details
#compare(secondary_data_file) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/csv_utils/csv_compare.rb', line 19 def compare(secondary_data_file) src = CSV.open(primary_data_file, 'rb') src_headers = src.shift strip_bom!(src_headers[0]) dest = CSV.open(secondary_data_file, 'rb') dest_headers = dest.shift strip_bom!(dest_headers[0]) read_next_src = true read_next_dest = true while(!src.eof? || !dest.eof?) src_record = next_record_from_file(src_headers, src) if read_next_src dest_record = next_record_from_file(dest_headers, dest) if read_next_dest if ! src_record read_next_src = false read_next_dest = true yield :delete, dest_record elsif ! dest_record read_next_src = true read_next_dest = false yield :create, src_record elsif compare_proc.call(src_record, dest_record) == 0 read_next_src = true read_next_dest = true yield(:update, src_record) if update_row?(src_record, dest_record) elsif compare_proc.call(src_record, dest_record) > 0 read_next_src = false read_next_dest = true yield :delete, dest_record else read_next_src = true read_next_dest = false yield :create, src_record end end src.close dest.close end |