Class: Smuggle::Services::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/smuggle/services/import.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolver: Smuggle::Importer::Resolver.new) ⇒ Import

Returns a new instance of Import.



10
11
12
# File 'lib/smuggle/services/import.rb', line 10

def initialize(resolver: Smuggle::Importer::Resolver.new)
  @resolver = resolver
end

Class Method Details

.call(**args) ⇒ Object



6
7
8
# File 'lib/smuggle/services/import.rb', line 6

def self.call(**args)
  new.call(args)
end

Instance Method Details

#call(model:, filepath:, **options) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/smuggle/services/import.rb', line 14

def call(model:, filepath:, **options)
  importer = options.fetch(:importer) { @resolver.call(model: model) }

  CSV.read(filepath, headers: true, **importer.csv_converters).map do |row|
    importer.new(row, model).persist
  end
end