Class: Loady::CsvLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/loady/csv_loader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCsvLoader

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, options = {}, &block)
  @logger = options.delete(:logger) || default_logger

  file = File.new(filename)
  file.autoclose = true
  file.gets if options.delete(:skip_first_row)

  file.each do |line|
    readline line, options, &block
  end        

  @logger.info "Finished. Loaded #{@success} rows. #{@warning} unprocessed rows."
end