Class: CSVFile
- Inherits:
-
Object
- Object
- CSVFile
- Defined in:
- lib/csv_file.rb
Overview
CSVFile loads csv data from a given path or file handle and provides methods to iterate over the data.
Instance Method Summary collapse
-
#each_row(import_manager, rows = nil & block) ⇒ Object
yields the rows of the csv file as CSVRow objects.
-
#each_row_hash(rows = nil, &block) ⇒ Object
yields the rows of the csv file as simple hashes.
-
#initialize(path_or_file, row_class, col_sep: ",", encoding: "utf-8", headers: false) ⇒ CSVFile
constructor
A new instance of CSVFile.
- #row_count ⇒ Object
Constructor Details
#initialize(path_or_file, row_class, col_sep: ",", encoding: "utf-8", headers: false) ⇒ CSVFile
Returns a new instance of CSVFile.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/csv_file.rb', line 6 def initialize path_or_file, row_class, col_sep: ",", encoding: "utf-8", headers: false raise ArgumentError, "no row class given" unless row_class.is_a?(Class) raise ArgumentError, "#{row_class} must inherit from CSVRow" unless row_class < CSVRow @row_class = row_class @col_sep = col_sep @encoding = encoding read_csv path_or_file initialize_column_map headers end |
Instance Method Details
#each_row(import_manager, rows = nil & block) ⇒ Object
yields the rows of the csv file as CSVRow objects
18 19 20 21 22 |
# File 'lib/csv_file.rb', line 18 def each_row import_manager, rows=nil & block each_row_hash rows do |row_hash, index| yield @row_class.new(row_hash, index, import_manager) end end |
#each_row_hash(rows = nil, &block) ⇒ Object
yields the rows of the csv file as simple hashes
25 26 27 28 29 30 31 |
# File 'lib/csv_file.rb', line 25 def each_row_hash rows=nil, &block if rows selected_rows rows, &block else all_rows &block end end |
#row_count ⇒ Object
33 34 35 |
# File 'lib/csv_file.rb', line 33 def row_count @rows.size end |