Class: CsvRecord::Base

Inherits:
Record::Base
  • Object
show all
Defined in:
lib/csvrecord/base.rb

Overview

Base

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.foreach(path, sep: Csv.config.sep, headers: true) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/csvrecord/base.rb', line 11

def self.foreach( path, sep: Csv.config.sep, headers: true )
  CsvReader.foreach( path, sep: sep, headers: headers ) do |row|
    rec = new
    values = CsvReader.unwrap( row )
    rec.parse( values )

    yield( rec )    ## check: use block.class( rec ) - why? why not?

  end
end

.parse(txt_or_rows, sep: Csv.config.sep, headers: true) ⇒ Object

note: returns an (lazy) enumarator



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/csvrecord/base.rb', line 22

def self.parse( txt_or_rows, sep: Csv.config.sep, headers: true )  ## note: returns an (lazy) enumarator

  if txt_or_rows.is_a? String
    txt = txt_or_rows
    rows = CsvReader.parse( txt, sep: sep, headers: headers )
  else
    ### todo/fix: use only self.create( array-like ) for array-like data  - why? why not?

    rows = txt_or_rows    ## assume array-like records that responds to :each

  end

  pp rows

  Enumerator.new do |yielder|
    rows.each do |row|
      rec = new
      values = CsvReader.unwrap( row )
      rec.parse( values )

      yielder.yield( rec )
    end
  end
end

.read(path, sep: Csv.config.sep, headers: true) ⇒ Object

not returns an enumarator



45
46
47
48
# File 'lib/csvrecord/base.rb', line 45

def self.read( path, sep: Csv.config.sep, 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_csvObject

use/rename/alias to to_row too - why? why not?



52
53
54
55
# File 'lib/csvrecord/base.rb', line 52

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