Class: TinOpener::DataFileProcessors::CsvProcessor
- Inherits:
-
TinOpener::DataFileProcessor
- Object
- TinOpener::DataFileProcessor
- TinOpener::DataFileProcessors::CsvProcessor
- Defined in:
- lib/tin_opener/data_file_processors/csv_processor.rb
Instance Method Summary collapse
- #headers ⇒ Object
-
#initialize(args = {}) ⇒ CsvProcessor
constructor
A new instance of CsvProcessor.
- #rows ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ CsvProcessor
Returns a new instance of CsvProcessor.
4 5 6 7 |
# File 'lib/tin_opener/data_file_processors/csv_processor.rb', line 4 def initialize(args = {}) super @separator = args.fetch(:separator) { ';' } end |
Instance Method Details
#headers ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/tin_opener/data_file_processors/csv_processor.rb', line 9 def headers @headers ||= rows.first.try do |row| row.transform_values do |value| value ? value.class.name : 'String' end end end |
#rows ⇒ Object
17 18 19 20 21 |
# File 'lib/tin_opener/data_file_processors/csv_processor.rb', line 17 def rows @csv_data ||= CSV.parse(@file, col_sep: @separator, headers: true).map do |row| row.to_hash.transform_keys{ |a| a.gsub(/\s+/, ' ').strip.gsub(/\s/, '_').underscore.to_sym } end end |