Class: CsvReader

Inherits:
Object
  • Object
show all
Defined in:
lib/csvreader/reader.rb,
lib/csvreader/buffer.rb,
lib/csvreader/parser.rb,
lib/csvreader/version.rb

Overview

note: uses a class for now - change to module - why? why not?

Defined Under Namespace

Classes: BufferIO, Parser

Constant Summary collapse

MAJOR =

todo: namespace inside version or something - why? why not??

0
MINOR =
4
PATCH =
0
VERSION =
[MAJOR,MINOR,PATCH].join('.')

Class Method Summary collapse

Class Method Details



16
17
18
# File 'lib/csvreader/version.rb', line 16

def self.banner
  "csvreader/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
end

.foreach(path, sep: Csv.config.sep, &block) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/csvreader/reader.rb', line 156

def self.foreach( path, sep: Csv.config.sep, &block )
  csv_options = Csv.config.default_options.merge(
                   col_sep: sep
  )

  Parser.foreach( path, &block ) ###, csv_options )
end

.header(path, sep: Csv.config.sep) ⇒ Object

use header or headers - or use both (with alias)?



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/csvreader/reader.rb', line 165

def self.header( path, sep: Csv.config.sep )   ## use header or headers - or use both (with alias)?
  # read first lines (only)
  #  and parse with csv to get header from csv library itself
  #
  #  check - if there's an easier or built-in way for the csv library

  ## readlines until
  ##  - NOT a comments line or
  ##  - NOT a blank line

 record = nil
 File.open( path, 'r:bom|utf-8' ) do |file|
    record = Parser.parse_line( file )
 end

 record  ## todo/fix: return nil for empty - why? why not?
end

.parse(txt, sep: Csv.config.sep) ⇒ Object

todo/fix: “unify” parse and parse_lines !!!

check for block_given? - why? why not?


131
132
133
134
135
136
137
# File 'lib/csvreader/reader.rb', line 131

def self.parse( txt, sep: Csv.config.sep )
  csv_options = Csv.config.default_options.merge(
                   col_sep: sep
  )
  ## pp csv_options
  Parser.parse( txt )  ###, csv_options )
end

.parse_line(txt, sep: Csv.config.sep, trim: Csv.config.trim?, na: Csv.config.na, dialect: Csv.config.dialect, converters: nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/csvreader/reader.rb', line 113

def self.parse_line( txt, sep:        Csv.config.sep,
                          trim:       Csv.config.trim?,
                          na:         Csv.config.na,
                          dialect:    Csv.config.dialect,
                          converters: nil)
  ## note: do NOT include headers option (otherwise single row gets skipped as first header row :-)
  csv_options = Csv.config.default_options.merge(
                  col_sep: sep
  )
  ## pp csv_options
  Parser.parse_line( txt )  ##, csv_options )
end

.parse_lines(txt, sep: Csv.config.sep, &block) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/csvreader/reader.rb', line 139

def self.parse_lines( txt, sep: Csv.config.sep, &block )
  csv_options = Csv.config.default_options.merge(
                   col_sep: sep
  )
  ## pp csv_options
  Parser.parse_lines( txt, &block )  ###, csv_options )
end

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



147
148
149
150
151
152
153
# File 'lib/csvreader/reader.rb', line 147

def self.read( path, sep: Csv.config.sep )
  ## note: use our own file.open
  ##   always use utf-8 for now
  ##    check/todo: add skip option bom too - why? why not?
  txt = File.open( path, 'r:bom|utf-8' ).read
  parse( txt, sep: sep )
end

.rootObject



20
21
22
# File 'lib/csvreader/version.rb', line 20

def self.root
  File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )
end

.versionObject



12
13
14
# File 'lib/csvreader/version.rb', line 12

def self.version
  VERSION
end