Class: TabReader
- Inherits:
-
Object
- Object
- TabReader
- Defined in:
- lib/tabreader/version.rb,
lib/tabreader/reader.rb
Overview
note: for now TabReader is a class!!! NOT a module - change - why? why not?
Constant Summary collapse
- MAJOR =
todo: namespace inside version or something - why? why not??
0- MINOR =
1- PATCH =
0- VERSION =
[MAJOR,MINOR,PATCH].join('.')
Class Method Summary collapse
- .banner ⇒ Object
- .foreach(path, headers: false) ⇒ Object
- .header(path) ⇒ Object
-
.parse(txt, headers: false) ⇒ Object
use parse_rows or parse_lines for array or array results.
- .parse_line(line) ⇒ Object
- .read(path, headers: false) ⇒ Object
- .root ⇒ Object
- .version ⇒ Object
Class Method Details
.banner ⇒ Object
16 17 18 |
# File 'lib/tabreader/version.rb', line 16 def self. "tabreader/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]" end |
.foreach(path, headers: false) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tabreader/reader.rb', line 56 def self.foreach( path, headers: false ) if headers.is_a?( Array ) columns = headers else columns = nil ## header row a.k.a. columns / fields end File.open( path, 'r:utf-8' ).each_line do |line| pp line values = parse_line( line ) if headers ## add values as name/value pairs e.g. array of hashes if columns.nil? columns = values ## first row is header row else pairs = columns.zip(values) h = pairs.to_h yield( h ) end else ## add values as is e.g. array of array yield( values ) end end # return nil nil end |
.header(path) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/tabreader/reader.rb', line 97 def self.header( path ) line = File.open( path, 'r:utf-8' ) do |f| if f.eof? ## handle empty file; return empty string; no readline call possible ## todo/check: return nil from header is no header or [] - why? why not? ## or throw exception end of file reached (EOFError) - why? why not? "" else f.readline end end ## note: line includes \n or \r\n at the end ## pp line parse_line( line ) end |
.parse(txt, headers: false) ⇒ Object
use parse_rows or parse_lines for array or array results
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tabreader/reader.rb', line 24 def self.parse( txt, headers: false ) ## use parse_rows or parse_lines for array or array results rows = [] if headers.is_a?( Array ) columns = headers else columns = nil ## header row a.k.a. columns / fields end txt.each_line do |line| values = parse_line( line ) if headers ## add values as name/value pairs e.g. array of hashes if columns.nil? columns = values ## first row is header row else ## note: will cut-off values if values.size > columns.size ## add warning/error - why? why not? ## if values.size <= columns.size will get filled-up with nil pairs = columns.zip(values) ## pp pairs h = pairs.to_h ## pp h rows << h end else ## add values as is e.g. array of array rows << values end end rows end |
.parse_line(line) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/tabreader/reader.rb', line 84 def self.parse_line( line ) ## check - can handle comments and blank lines too - why? why not? ## remove trailing newlines ## note: chomp('') if is an empty string, ## it will remove all trailing newlines from the string. ## use line.sub(/[\n\r]*$/, '') or similar instead - why? why not? line = line.chomp('') values = line.split("\t") values end |
.read(path, headers: false) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/tabreader/reader.rb', line 17 def self.read( path, headers: false ) txt = File.open( path, 'r:utf-8' ).read ## puts "#{path}:" ## pp txt parse( txt, headers: headers ) end |
.root ⇒ Object
20 21 22 |
# File 'lib/tabreader/version.rb', line 20 def self.root File.( File.dirname(File.dirname(File.dirname(__FILE__))) ) end |
.version ⇒ Object
12 13 14 |
# File 'lib/tabreader/version.rb', line 12 def self.version VERSION end |