Module: SmartCsvImport::Processor::ImportResultBuilder
- Defined in:
- lib/smart_csv_import/processor/import_result_builder.rb
Class Method Summary collapse
- .abort_result(imported:, failed:, parse_errors:, warnings:, header_mappings:, import_id:, bad_row_limit:) ⇒ Object
- .build_abort_warning(parse_error_count:, total:, bad_row_limit:) ⇒ Object
- .build_blank_row_warnings(blank_count) ⇒ Object
- .build_parse_errors(bad_rows) ⇒ Object
- .build_row_error_attributes(errors:, parse_errors:, import_id:) ⇒ Object
- .final_result(imported:, failed:, errors:, parse_errors:, warnings:, header_mappings:, import_id:, dry_run:) ⇒ Object
Class Method Details
.abort_result(imported:, failed:, parse_errors:, warnings:, header_mappings:, import_id:, bad_row_limit:) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/smart_csv_import/processor/import_result_builder.rb', line 83 def abort_result(imported:, failed:, parse_errors:, warnings:, header_mappings:, import_id:, bad_row_limit:) total = imported + failed + parse_errors.size abort_warning = build_abort_warning( parse_error_count: parse_errors.size, total: total, bad_row_limit: bad_row_limit ) Result.partial_failure( imported: imported, failed: failed, total: total, errors: [], header_mappings: header_mappings, import_id: import_id, warnings: [*warnings, abort_warning], parse_errors: parse_errors ) end |
.build_abort_warning(parse_error_count:, total:, bad_row_limit:) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/smart_csv_import/processor/import_result_builder.rb', line 55 def build_abort_warning(parse_error_count:, total:, bad_row_limit:) ratio = total.positive? ? parse_error_count.to_f / total : 0 pct = (ratio * 100).round(1) limit_pct = (bad_row_limit * 100).round(1) RowWarning.new( row: 0, message: "Import aborted: #{pct}% of rows were malformed (limit: #{limit_pct}%)" ) end |
.build_blank_row_warnings(blank_count) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/smart_csv_import/processor/import_result_builder.rb', line 47 def build_blank_row_warnings(blank_count) return [] if blank_count.zero? noun = blank_count == 1 ? 'row' : 'rows' verb = blank_count == 1 ? 'was' : 'were' [RowWarning.new(row: 0, message: "#{blank_count} blank #{noun} #{verb} skipped", type: :blank_rows)] end |
.build_parse_errors(bad_rows) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/smart_csv_import/processor/import_result_builder.rb', line 8 def build_parse_errors(bad_rows) bad_rows.map do |bad_row| ParseError.new( line_number: bad_row[:csv_line_number] || bad_row[:file_line_number], raw_line: bad_row[:raw_logical_line].to_s.chomp, error_message: bad_row[:error_message].to_s ) end end |
.build_row_error_attributes(errors:, parse_errors:, import_id:) ⇒ Object
18 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 |
# File 'lib/smart_csv_import/processor/import_result_builder.rb', line 18 def build_row_error_attributes(errors:, parse_errors:, import_id:) now = Time.current validation_attrs = errors.map do |err| { import_id: import_id, row_number: err.row, error_type: 'validation', column_name: err.column.to_s, messages: err., raw_line: nil, error_message: nil, created_at: now } end parse_attrs = parse_errors.map do |pe| { import_id: import_id, row_number: pe.line_number, error_type: 'parse', column_name: nil, messages: [], raw_line: pe.raw_line, error_message: pe., created_at: now } end validation_attrs + parse_attrs end |
.final_result(imported:, failed:, errors:, parse_errors:, warnings:, header_mappings:, import_id:, dry_run:) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/smart_csv_import/processor/import_result_builder.rb', line 65 def final_result(imported:, failed:, errors:, parse_errors:, warnings:, header_mappings:, import_id:, dry_run:) total = imported + failed + parse_errors.size all_failed = failed + parse_errors.size if dry_run Result.dry_run(imported: imported, failed: failed, total: total, errors: errors, header_mappings: header_mappings, warnings: warnings, parse_errors: parse_errors) elsif all_failed.positive? Result.partial_failure(imported: imported, failed: failed, total: total, errors: errors, header_mappings: header_mappings, import_id: import_id, warnings: warnings, parse_errors: parse_errors) else Result.completed(imported: imported, failed: failed, total: total, errors: errors, header_mappings: header_mappings, import_id: import_id, warnings: warnings, parse_errors: parse_errors) end end |