class CsvRecord::Base
Public Class Methods
foreach( path, sep: nil, headers: true ) { |rec| ... }
click to toggle source
# File lib/csvrecord/base.rb, line 11 def self.foreach( path, sep: nil, headers: true ) ## note: always use reader w/o headers to get row/record values as array of strings ## if headers: true -> skip first row names = nil CsvReader.foreach( path, sep: sep ) do |row| if headers && names.nil? names = row ## store header row / a.k.a. field/column names else rec = new rec.parse( row ) yield( rec ) ## check: use block.class( rec ) - why? why not? end end end
parse( txt_or_rows, sep: nil, headers: true )
click to toggle source
# File lib/csvrecord/base.rb, line 32 def self.parse( txt_or_rows, sep: nil, headers: true ) ## note: returns an (lazy) enumarator if txt_or_rows.is_a? String txt = txt_or_rows ## note: always use reader w/o headers to get row/record values as array of strings ## if headers: true -> skip first row rows = CsvReader.parse( txt, sep: sep ) else ### todo/fix: use only self.create( array-like ) for array-like data - why? why not? rows = txt_or_rows end ## pp rows names = nil Enumerator.new do |yielder| rows.each do |row| if headers && names.nil? names = row ## store header row / a.k.a. field/column names else rec = new rec.parse( row ) yielder.yield( rec ) end end end end
read( path, sep: nil, headers: true )
click to toggle source
# File lib/csvrecord/base.rb, line 63 def self.read( path, sep: nil, headers: true ) ## not returns an enumarator txt = File.open( path, 'r:utf-8' ).read parse( txt, sep: sep, headers: headers ) end
Public Instance Methods
to_csv()
click to toggle source
# File lib/csvrecord/base.rb, line 70 def to_csv ## use/rename/alias to to_row too - why? why not? ## todo/fix: check for date and use own date to string format!!!! @values.map{ |value| value.to_s } end