Class: CsvRecord::Base
- Inherits:
-
Record::Base
- Object
- Record::Base
- CsvRecord::Base
- Defined in:
- lib/csvrecord/base.rb
Overview
Base
Class Method Summary collapse
- .foreach(path, sep: nil, headers: true) ⇒ Object
-
.parse(txt_or_rows, sep: nil, headers: true) ⇒ Object
note: returns an (lazy) enumarator.
-
.read(path, sep: nil, headers: true) ⇒ Object
not returns an enumarator.
Instance Method Summary collapse
-
#to_csv ⇒ Object
use/rename/alias to to_row too - why? why not?.
Class Method Details
.foreach(path, sep: nil, headers: true) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# 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) ⇒ Object
note: returns an (lazy) enumarator
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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) ⇒ Object
not returns an enumarator
63 64 65 66 |
# 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 |
Instance Method Details
#to_csv ⇒ Object
use/rename/alias to to_row too - why? why not?
70 71 72 73 |
# 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 |