Class: CSVStepImporter::File

Inherits:
Node
  • Object
show all
Defined in:
lib/csv_step_importer/file.rb

Defined Under Namespace

Classes: CSVFileNotFoundError

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #env, #parent

Instance Method Summary collapse

Methods inherited from Node

#add_children, #create_or_update, #validate_children

Methods inherited from Base

#assign_attributes, #create_or_update, #inspect, #persisted?, #save, #save!, #to_s, #update

Constructor Details

#initialize(path:, chunk_class: nil, row_class: nil, csv_options: {}, processor_classes: nil, **attributes) ⇒ File

Returns a new instance of File.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/csv_step_importer/file.rb', line 15

def initialize(path:, chunk_class: nil, row_class: nil, csv_options: {}, processor_classes: nil, **attributes)
  super **attributes

  self.chunk_class = chunk_class || CSVStepImporter::Chunk
  self.path = path
  self.row_class = row_class || CSVStepImporter::Row
  self.processor_classes = processor_classes

  self.csv_options = {
    chunk_size: 1000,
    file_encoding: "CP932:UTF-8",
  }.merge(csv_options)

  load_csv
end

Instance Attribute Details

#chunk_classObject

Returns the value of attribute chunk_class.



7
8
9
# File 'lib/csv_step_importer/file.rb', line 7

def chunk_class
  @chunk_class
end

#csv_load_errorObject

Returns the value of attribute csv_load_error.



7
8
9
# File 'lib/csv_step_importer/file.rb', line 7

def csv_load_error
  @csv_load_error
end

#csv_optionsObject

Returns the value of attribute csv_options.



7
8
9
# File 'lib/csv_step_importer/file.rb', line 7

def csv_options
  @csv_options
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/csv_step_importer/file.rb', line 7

def headers
  @headers
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/csv_step_importer/file.rb', line 7

def path
  @path
end

#processor_classesObject

Returns the value of attribute processor_classes.



7
8
9
# File 'lib/csv_step_importer/file.rb', line 7

def processor_classes
  @processor_classes
end

#row_classObject

Returns the value of attribute row_class.



7
8
9
# File 'lib/csv_step_importer/file.rb', line 7

def row_class
  @row_class
end

Instance Method Details

#load_csvObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/csv_step_importer/file.rb', line 31

def load_csv
  raise CSVFileNotFoundError.new unless ::File.exists? path

  ::SmarterCSV.process(path, **csv_options.deep_dup) do |rows|
    self.headers ||= rows.first&.keys
    add_children chunk_class.new parent: self, rows: rows, row_class: row_class, processor_classes: processor_classes
  end
rescue EOFError => exception
  self.csv_load_error = exception
rescue CSVFileNotFoundError => exception
  self.csv_load_error = exception
rescue ::CSV::MalformedCSVError => exception
  self.csv_load_error = exception
end

#validate_csv_load_errorObject



46
47
48
49
# File 'lib/csv_step_importer/file.rb', line 46

def validate_csv_load_error
  return unless csv_load_error
  errors[:csv_file] << I18n.t("csv_step_importer.errors.#{csv_load_error.class.name.underscore.gsub(/\//, '_')}")
end