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, #build_env, #create_or_update, #run_validations!, #validate_children

Methods inherited from Base

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

Constructor Details

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

Returns a new instance of File.



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

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

  self.chunk_class = chunk_class
  self.path = path
  self.row_class = row_class
  self.processor_classes = processor_classes

  self.csv_options = csv_options
  precompile_csv_options!

  load_csv
end

Instance Attribute Details

#chunk_classObject

Returns the value of attribute chunk_class.



9
10
11
# File 'lib/csv_step_importer/file.rb', line 9

def chunk_class
  @chunk_class
end

#csv_load_errorObject

Returns the value of attribute csv_load_error.



9
10
11
# File 'lib/csv_step_importer/file.rb', line 9

def csv_load_error
  @csv_load_error
end

#csv_optionsObject

Returns the value of attribute csv_options.



9
10
11
# File 'lib/csv_step_importer/file.rb', line 9

def csv_options
  @csv_options
end

#headersObject

Returns the value of attribute headers.



9
10
11
# File 'lib/csv_step_importer/file.rb', line 9

def headers
  @headers
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/csv_step_importer/file.rb', line 9

def path
  @path
end

#processor_classesObject

Returns the value of attribute processor_classes.



9
10
11
# File 'lib/csv_step_importer/file.rb', line 9

def processor_classes
  @processor_classes
end

#row_classObject

Returns the value of attribute row_class.



9
10
11
# File 'lib/csv_step_importer/file.rb', line 9

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/csv_step_importer/file.rb', line 31

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

  first_row = 2

  ::SmarterCSV.process(path, **csv_options) do |rows|
    add_children chunk_class.new(
      first_row: first_row,
      parent: self,
      processor_classes: processor_classes,
      row_class: row_class,
      rows: rows,
    )
    first_row += rows.size
  end
rescue CSVFileNotFoundError => exception
  # File not found
  self.csv_load_error = exception
rescue ::ArgumentError => exception
  # expected encoding UTF-8, but was Excel
  self.csv_load_error = exception
rescue ::CSV::MalformedCSVError => exception
  # CSV malformed
  self.csv_load_error = exception
rescue ::Encoding::InvalidByteSequenceError => exception
  # expected encoding CP932, but was excel file
  self.csv_load_error = exception
rescue ::EOFError => exception
  # empty file
  self.csv_load_error = exception
end

#validate_csv_load_errorObject



63
64
65
66
# File 'lib/csv_step_importer/file.rb', line 63

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