Class: SmartCsvImport::Strategies::Lookup

Inherits:
SmartCsvImport::Strategy show all
Defined in:
lib/smart_csv_import/strategies/lookup.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defined_mappingsObject



15
16
17
# File 'lib/smart_csv_import/strategies/lookup.rb', line 15

def defined_mappings
  @defined_mappings ||= {}
end

.inherited(subclass) ⇒ Object



19
20
21
22
# File 'lib/smart_csv_import/strategies/lookup.rb', line 19

def inherited(subclass)
  super
  subclass.instance_variable_set(:@defined_mappings, {})
end

.mappings(hash = nil) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/smart_csv_import/strategies/lookup.rb', line 7

def mappings(hash = nil)
  return @defined_mappings ||= {} unless hash

  @defined_mappings = hash.each_with_object({}) do |(header, field), acc|
    acc[header.downcase] = field
  end
end

Instance Method Details

#match(csv_headers:, form_class:, sample_rows: []) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/smart_csv_import/strategies/lookup.rb', line 25

def match(csv_headers:, form_class:, sample_rows: [])
  mappings = self.class.defined_mappings

  csv_headers.each_with_object({}) do |header, results|
    field = mappings[header.downcase]
    next unless field

    results[header] = MatchResult.matched(
      target_field: field,
      confidence: 1.0,
      strategy_name: "lookup"
    )
  end
end