class CsvReader
Constants
- DEFAULT
pre-define
CsvReader
(built-in) formats/dialect- EXCEL
- FIXED
- HUMAN
- JSON
- MYSQL
- NUMERIC
- POSTGRES
- POSTGRES_TEXT
- RFC4180
- STRICT
- TAB
- TABLE
- VERSION
- YAML
Public Class Methods
default()
click to toggle source
# File lib/csvreader/base.rb, line 133 def self.default() DEFAULT; end
excel()
click to toggle source
# File lib/csvreader/base.rb, line 143 def self.excel() EXCEL; end
f()
click to toggle source
# File lib/csvreader/base.rb, line 154 def self.f() fixed; end
fix()
click to toggle source
# File lib/csvreader/base.rb, line 153 def self.fix() fixed; end
fixed()
click to toggle source
# File lib/csvreader/base.rb, line 152 def self.fixed() FIXED; end
foreach( path, sep: nil, converters: nil, parser: nil, **kwargs, &block )
click to toggle source
# File lib/csvreader/reader.rb, line 59 def self.foreach( path, sep: nil, converters: nil, parser: nil, **kwargs, &block ) csv = open( path, sep: sep, converters: converters, parser: parser, **kwargs ) if block_given? begin csv.each( &block ) ensure csv.close end else csv.to_enum ## note: caller (responsible) must close file!!! ## remove version without block given - why? why not? ## use Csv.open().to_enum or Csv.open().each ## or Csv.new( File.new() ).to_enum or Csv.new( File.new() ).each ??? end end
header( path, sep: nil, parser: nil, **kwargs )
click to toggle source
# File lib/csvreader/reader.rb, line 40 def self.header( path, sep: nil, parser: nil, **kwargs ) ## use header or headers - or use both (with alias)? # read first lines (only) # and parse with csv to get header from csv library itself records = [] open( path, sep: sep, parser: parser, **kwargs ) do |csv| csv.each do |record| records << record break ## only parse/read first record end end ## unwrap record if empty return nil - why? why not? ## return empty record e.g. [] - why? why not? ## returns nil for empty (for now) - why? why not? records.size == 0 ? nil : records.first end
hum()
click to toggle source
# File lib/csvreader/base.rb, line 138 def self.hum() human; end
human()
click to toggle source
# File lib/csvreader/base.rb, line 137 def self.human() HUMAN; end
hxl()
click to toggle source
# File lib/csvreader/base.rb, line 139 def self.hxl() human; end
j()
click to toggle source
# File lib/csvreader/base.rb, line 157 def self.j() json; end
json()
click to toggle source
# File lib/csvreader/base.rb, line 156 def self.json() JSON; end
mysql()
click to toggle source
# File lib/csvreader/base.rb, line 144 def self.mysql() MYSQL; end
n()
click to toggle source
# File lib/csvreader/base.rb, line 136 def self.n() numeric; end
new( str_or_readable, sep: nil, converters: nil, parser: nil, **kwargs )
click to toggle source
# File lib/csvreader/reader.rb, line 110 def initialize( str_or_readable, sep: nil, converters: nil, parser: nil, **kwargs ) raise ArgumentError.new( "Cannot parse nil as CSV" ) if str_or_readable.nil? ## todo: use (why? why not) - raise ArgumentError, "Cannot parse nil as CSV" if data.nil? # create the IO object we will read from @io = str_or_readable.is_a?(String) ? StringIO.new(str_or_readable) : str_or_readable @sep = sep # (optional) for ParserStd, ParserStrict @kwargs = kwargs # e.g. (optional) :width for ParserFixed @converters = Converter.create_converters( converters ) @parser = parser.nil? ? Parser::DEFAULT : parser end
num()
click to toggle source
# File lib/csvreader/base.rb, line 135 def self.num() numeric; end
numeric()
click to toggle source
# File lib/csvreader/base.rb, line 134 def self.numeric() NUMERIC; end
open( path, mode=nil, sep: nil, converters: nil, parser: nil, **kwargs, &block )
click to toggle source
# File lib/csvreader/reader.rb, line 5 def self.open( path, mode=nil, sep: nil, converters: nil, parser: nil, **kwargs, &block ) ## rename path to filename or name - why? why not? ## note: default mode (if nil/not passed in) to 'r:bom|utf-8' f = File.open( path, mode ? mode : 'r:bom|utf-8' ) csv = new(f, sep: sep, converters: converters, parser: parser, **kwargs ) # handle blocks like Ruby's open(), not like the (old old) CSV library if block_given? begin block.call( csv ) ensure csv.close end else csv end end
parse( str_or_readable, sep: nil, converters: nil, parser: nil, **kwargs, &block )
click to toggle source
# File lib/csvreader/reader.rb, line 78 def self.parse( str_or_readable, sep: nil, converters: nil, parser: nil, **kwargs, &block ) csv = new( str_or_readable, sep: sep, converters: converters, parser: parser, **kwargs ) if block_given? csv.each( &block ) ## note: caller (responsible) must close file!!! - add autoclose - why? why not? else # slurp contents, if no block is given csv.read ## note: caller (responsible) must close file!!! - add autoclose - why? why not? end end
parse_line( str_or_readable, sep: nil, converters: nil, **kwargs )
click to toggle source
note: only add parse_line
convenience helper for default
always use parse (do NOT/NOT/NOT use parse_line) - why? why not? todo/fix: remove parse_line!!!
# File lib/csvreader/reader.rb, line 97 def self.parse_line( str_or_readable, sep: nil, converters: nil, **kwargs ) records = [] parse( str_or_readable, sep: sep, converters: converters, **kwargs ) do |record| records << record break # only parse first record end records.size == 0 ? nil : records.first end
postgres()
click to toggle source
# File lib/csvreader/base.rb, line 146 def self.postgres() postgresql; end
postgres_text()
click to toggle source
# File lib/csvreader/base.rb, line 148 def self.postgres_text() postgresql_text; end
postgresql()
click to toggle source
# File lib/csvreader/base.rb, line 145 def self.postgresql() POSTGRESQL; end
postgresql_text()
click to toggle source
# File lib/csvreader/base.rb, line 147 def self.postgresql_text() POSTGRESQL_TEXT; end
read( path, sep: nil, converters: nil, parser: nil, **kwargs )
click to toggle source
# File lib/csvreader/reader.rb, line 29 def self.read( path, sep: nil, converters: nil, parser: nil, **kwargs ) open( path, sep: sep, converters: converters, parser: parser, **kwargs ) { |csv| csv.read } end
rfc4180()
click to toggle source
# File lib/csvreader/base.rb, line 142 def self.rfc4180() RFC4180; end
root()
click to toggle source
# File lib/csvreader/version.rb, line 28 def self.root File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) ) end
strict()
click to toggle source
# File lib/csvreader/base.rb, line 141 def self.strict() STRICT; end
tab()
click to toggle source
# File lib/csvreader/base.rb, line 150 def self.tab() TAB; end
table()
click to toggle source
# File lib/csvreader/base.rb, line 151 def self.table() TABLE; end
version()
click to toggle source
# File lib/csvreader/version.rb, line 18 def self.version ## keep (as an alternative to VERSION) - why? why not? VERSION end
y()
click to toggle source
# File lib/csvreader/base.rb, line 159 def self.y() yaml; end
yaml()
click to toggle source
# File lib/csvreader/base.rb, line 158 def self.yaml() YAML; end
Public Instance Methods
each( &block )
click to toggle source
# File lib/csvreader/reader.rb, line 143 def each( &block ) if block_given? kwargs = {} ## note: only add separator if present/defined (not nil) ## todo/fix: change sep keyword to "known" classes!!!! kwargs[:sep] = @sep if @sep && @parser.respond_to?( :'sep=' ) kwargs[:width] = @kwargs[:width] if @parser.is_a?( ParserFixed ) ## todo/fix: print warning about unused / unknown kwargs!!!!! ## check array / pipeline of converters is empty (size=0 e.g. is []) if @converters.empty? @parser.parse( @io, kwargs, &block ) else ## add "post"-processing with converters pipeline ## that is, convert all strings to integer, float, date, ... if wanted @parser.parse( @io, kwargs ) do |raw_record| record = [] raw_record.each_with_index do | value, i | record << @converters.convert( value, i ) end block.call( record ) end end else to_enum end end
read()
click to toggle source
# File lib/csvreader/reader.rb, line 175 def read() to_a; end