Class: Loady::CsvLoader
- Inherits:
-
Object
- Object
- Loady::CsvLoader
- Defined in:
- lib/loady/csv_loader.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ CsvLoader
constructor
A new instance of CsvLoader.
-
#read(filename, options = {}, &block) ⇒ Object
valid options: skip_first_row: true – default = false logger: Logger.new(‘/somewhere/file.log’) – default = Logger.new(STDOUT) plus any valid options you can pass to CSV.new: see www.ruby-doc.org/stdlib/libdoc/csv/rdoc/classes/CSV.html#M000190.
Constructor Details
#initialize ⇒ CsvLoader
Returns a new instance of CsvLoader.
6 7 8 9 10 |
# File 'lib/loady/csv_loader.rb', line 6 def initialize @success = 0 @warning = 0 @line_number = 0 end |
Class Method Details
.read(*args, &block) ⇒ Object
32 33 34 |
# File 'lib/loady/csv_loader.rb', line 32 def read(*args, &block) self.new.read(*args, &block) end |
Instance Method Details
#read(filename, options = {}, &block) ⇒ Object
valid options:
skip_first_row: true -- default = false
logger: Logger.new('/somewhere/file.log') -- default = Logger.new(STDOUT)
plus any valid options you can pass to CSV.new:
see http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/classes/CSV.html#M000190
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/loady/csv_loader.rb', line 17 def read(filename, = {}, &block) @logger = .delete(:logger) || default_logger file = File.new(filename) file.autoclose = true file.gets if .delete(:skip_first_row) file.each do |line| readline line, , &block end @logger.info "Finished. Loaded #{@success} rows. #{@warning} unprocessed rows." end |