Module: SmartCsvImport::Processor::MappingReviewPolicy

Defined in:
lib/smart_csv_import/processor/mapping_review_policy.rb

Class Method Summary collapse

Class Method Details

.auto_gate?(mappings, confidence_threshold) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'lib/smart_csv_import/processor/mapping_review_policy.rb', line 63

def auto_gate?(mappings, confidence_threshold)
  return true if mappings.values.any?(&:unmatched?)

  mappings.values.any? do |r|
    r.respond_to?(:confidence) && r.confidence < confidence_threshold
  end
end

.build_field_map(mappings) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/smart_csv_import/processor/mapping_review_policy.rb', line 14

def build_field_map(mappings)
  mappings.each_with_object({}) do |(header, result), acc|
    next unless result.respond_to?(:target_field) && result.matched?

    acc[header] = result.target_field
  end
end

.build_proposed_mappings(mappings, confidence_threshold:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/smart_csv_import/processor/mapping_review_policy.rb', line 26

def build_proposed_mappings(mappings, confidence_threshold:)
  mappings.each_with_object({}) do |(header, result), proposed|
    proposed[header] = if result.unmatched?
                         { field: nil, confidence: 0.0, status: :unmatched }
                       elsif result.confidence < confidence_threshold
                         { field: result.target_field, confidence: result.confidence, status: :low_confidence }
                       else
                         { field: result.target_field, confidence: result.confidence, status: :matched }
                       end
  end
end

.collect_warnings(mappings, duplicate_warning: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/smart_csv_import/processor/mapping_review_policy.rb', line 49

def collect_warnings(mappings, duplicate_warning: nil)
  base = duplicate_warning ? [duplicate_warning] : []
  unmatched = unmatched_headers(mappings)
  return base if unmatched.empty?

  per_column = unmatched.map do |header|
    UnmatchedColumnWarning.new(
      column_name: header,
      message: "Column '#{header}' was not imported — no matching field found"
    )
  end
  [*base, *per_column, summary_warning(unmatched)]
end

.required_field_below_threshold?(mappings, confidence_threshold, required_fields) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
# File 'lib/smart_csv_import/processor/mapping_review_policy.rb', line 71

def required_field_below_threshold?(mappings, confidence_threshold, required_fields)
  required_fields.any? do |field_name|
    mapping = mappings.values.find do |result|
      result.respond_to?(:target_field) && result.target_field == field_name
    end
    mapping.nil? || mapping.confidence < confidence_threshold
  end
end

.review_gate_triggered?(mappings, review_mode:, confidence_threshold:, required_fields:) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
# File 'lib/smart_csv_import/processor/mapping_review_policy.rb', line 38

def review_gate_triggered?(mappings, review_mode:, confidence_threshold:, required_fields:)
  case review_mode
  when :always
    true
  when :auto
    auto_gate?(mappings, confidence_threshold)
  when :skip
    required_field_below_threshold?(mappings, confidence_threshold, required_fields)
  end
end

.serialize_mappings(mappings) ⇒ Object



8
9
10
11
12
# File 'lib/smart_csv_import/processor/mapping_review_policy.rb', line 8

def serialize_mappings(mappings)
  mappings.each_with_object({}) do |(header, result), acc|
    acc[header] = result.respond_to?(:target_field) ? result.target_field.to_s : nil
  end
end

.summary_warning(unmatched) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/smart_csv_import/processor/mapping_review_policy.rb', line 80

def summary_warning(unmatched)
  noun = unmatched.size == 1 ? 'column' : 'columns'
  verb = unmatched.size == 1 ? 'was' : 'were'
  RowWarning.new(
    row: 0,
    message: "#{unmatched.size} #{noun} from your file #{verb} not imported: #{unmatched.join(', ')}"
  )
end

.unmatched_headers(mappings) ⇒ Object



22
23
24
# File 'lib/smart_csv_import/processor/mapping_review_policy.rb', line 22

def unmatched_headers(mappings)
  mappings.select { |_, result| result.unmatched? }.keys
end