Class: CsvImportMagic::Importer

Inherits:
Object
  • Object
show all
Defined in:
app/services/csv_import_magic/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(importer_id, resources = nil) ⇒ Importer

Returns a new instance of Importer.



5
6
7
8
9
10
11
# File 'app/services/csv_import_magic/importer.rb', line 5

def initialize(importer_id, resources = nil)
  @importer = ::Importer.find(importer_id)
  @source_klass = importer.source_klass
  @csv_parser_class = importer.parser_klass
  @resources = resources.try(:symbolize_keys!)
  @model = model_with_relation || @source_klass
end

Instance Attribute Details

#csv_parser_classObject (readonly)

Returns the value of attribute csv_parser_class.



3
4
5
# File 'app/services/csv_import_magic/importer.rb', line 3

def csv_parser_class
  @csv_parser_class
end

#importerObject (readonly)

Returns the value of attribute importer.



3
4
5
# File 'app/services/csv_import_magic/importer.rb', line 3

def importer
  @importer
end

#resourcesObject (readonly)

Returns the value of attribute resources.



3
4
5
# File 'app/services/csv_import_magic/importer.rb', line 3

def resources
  @resources
end

#source_klassObject (readonly)

Returns the value of attribute source_klass.



3
4
5
# File 'app/services/csv_import_magic/importer.rb', line 3

def source_klass
  @source_klass
end

Instance Method Details

#callObject



13
14
15
16
17
# File 'app/services/csv_import_magic/importer.rb', line 13

def call
  csv_parsed.run!
  csv_parsed.report
  csv_parsed
end

#column_separatorObject



19
20
21
22
# File 'app/services/csv_import_magic/importer.rb', line 19

def column_separator
  header = content.lines("\r")[0].gsub(/[^,;\t]/, '_')
  header.scan(/[,;\t]/).first
end