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: CSVStepImporter::Chunk, row_class: CSVStepImporter::Row, 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, #build_env, #create_or_update, #run_validations!, #validate_children
Methods inherited from Base
#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. = load_csv end |
Instance Attribute Details
#chunk_class ⇒ Object
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_error ⇒ Object
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_options ⇒ Object
Returns the value of attribute csv_options.
9 10 11 |
# File 'lib/csv_step_importer/file.rb', line 9 def @csv_options end |
#headers ⇒ Object
Returns the value of attribute headers.
9 10 11 |
# File 'lib/csv_step_importer/file.rb', line 9 def headers @headers end |
#path ⇒ Object
Returns the value of attribute path.
9 10 11 |
# File 'lib/csv_step_importer/file.rb', line 9 def path @path end |
#processor_classes ⇒ Object
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_class ⇒ Object
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_csv ⇒ Object
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, **) 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_error ⇒ Object
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 |