Class: Csvbuilder::Import::Csv
- Inherits:
-
Object
- Object
- Csvbuilder::Import::Csv
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/csvbuilder/importer/internal/import/csv.rb
Overview
Abstraction of Ruby’s CSV library. Keeps current row and line_number, skips empty rows, handles errors.
Instance Attribute Summary collapse
-
#current_row ⇒ Array?
readonly
The current row, or nil at the beginning or end of file.
-
#file_path ⇒ String
readonly
The file path of the CSV.
-
#line_number ⇒ Integer?
readonly
Return ‘0` at start of file, `1 to infinity` is line_number of row_model, `nil` is end of file (row is also `nil`).
Instance Method Summary collapse
-
#end_of_file? ⇒ Boolean
True, if the current position is at the end of the file.
-
#headers ⇒ Array?
Returns the header __without__ changing the position of the CSV.
-
#initialize(file_path) ⇒ Csv
constructor
A new instance of Csv.
-
#next_row ⇒ Array?
Returns the next row __without__ changing the position of the CSV.
-
#read_row ⇒ Array?
Returns the next row, while changing the position of the CSV.
-
#reset ⇒ Object
Resets the file to the start of file.
- #size ⇒ Integer
-
#skip_headers ⇒ Boolean, Array
If the current position is at the headers, skip it and return it.
-
#start_of_file? ⇒ Boolean
True, if the current position is at the start of the file.
Constructor Details
#initialize(file_path) ⇒ Csv
Returns a new instance of Csv.
18 19 20 21 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 18 def initialize(file_path) @file_path = file_path reset end |
Instance Attribute Details
#current_row ⇒ Array? (readonly)
Returns the current row, or nil at the beginning or end of file.
12 13 14 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 12 def current_row @current_row end |
#file_path ⇒ String (readonly)
Returns the file path of the CSV.
8 9 10 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 8 def file_path @file_path end |
#line_number ⇒ Integer? (readonly)
Return ‘0` at start of file, `1 to infinity` is line_number of row_model, `nil` is end of file (row is also `nil`)
10 11 12 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 10 def line_number @line_number end |
Instance Method Details
#end_of_file? ⇒ Boolean
Returns true, if the current position is at the end of the file.
60 61 62 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 60 def end_of_file? line_number.nil? end |
#headers ⇒ Array?
Returns the header __without__ changing the position of the CSV
37 38 39 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 37 def headers @headers ||= next_row end |
#next_row ⇒ Array?
Returns the next row __without__ changing the position of the CSV
66 67 68 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 66 def next_row @next_row ||= _read_row end |
#read_row ⇒ Array?
Returns the next row, while changing the position of the CSV
72 73 74 75 76 77 78 79 80 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 72 def read_row return if end_of_file? @current_row = @next_row || _read_row @line_number = current_row.nil? ? nil : @line_number + 1 @next_row = nil current_row end |
#reset ⇒ Object
Resets the file to the start of file
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 42 def reset return false unless valid? @line_number = 0 @headers = @current_row = @next_row = @skipped_rows = @next_skipped_rows = nil @ruby_csv.try(:close) @ruby_csv = _ruby_csv true end |
#size ⇒ Integer
25 26 27 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 25 def size @size ||= ::File.readlines(file_path).length end |
#skip_headers ⇒ Boolean, Array
If the current position is at the headers, skip it and return it. Otherwise, only return false.
31 32 33 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 31 def skip_headers start_of_file? ? (@headers = read_row) : false end |
#start_of_file? ⇒ Boolean
Returns true, if the current position is at the start of the file.
55 56 57 |
# File 'lib/csvbuilder/importer/internal/import/csv.rb', line 55 def start_of_file? line_number.zero? end |