Class: CSVStepImporter::File
- Defined in:
- lib/csv_step_importer/file.rb
Defined Under Namespace
Classes: CSVFileNotFoundError
Instance Attribute Summary collapse
-
#chunk_class ⇒ Object
Returns the value of attribute chunk_class.
-
#csv_load_error ⇒ Object
Returns the value of attribute csv_load_error.
-
#csv_options ⇒ Object
Returns the value of attribute csv_options.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#path ⇒ Object
Returns the value of attribute path.
-
#processor_classes ⇒ Object
Returns the value of attribute processor_classes.
-
#row_class ⇒ Object
Returns the value of attribute row_class.
Attributes inherited from Node
Instance Method Summary collapse
-
#initialize(path:, chunk_class: nil, row_class: nil, csv_options: {}, processor_classes: nil, **attributes) ⇒ File
constructor
A new instance of File.
- #load_csv ⇒ Object
- #validate_csv_load_error ⇒ Object
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. = { chunk_size: 1000, file_encoding: "CP932:UTF-8", }.merge() load_csv end |
Instance Attribute Details
#chunk_class ⇒ Object
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_error ⇒ Object
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_options ⇒ Object
Returns the value of attribute csv_options.
7 8 9 |
# File 'lib/csv_step_importer/file.rb', line 7 def @csv_options end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/csv_step_importer/file.rb', line 7 def headers @headers end |
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/csv_step_importer/file.rb', line 7 def path @path end |
#processor_classes ⇒ Object
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_class ⇒ Object
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_csv ⇒ Object
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, **.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_error ⇒ Object
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 |