Class: LightCsv
Overview
CSV parser
Defined Under Namespace
Classes: InvalidFormat
Instance Attribute Summary collapse
-
#bufsize ⇒ Object
Returns the value of attribute bufsize.
Class Method Summary collapse
- .foreach(filename) {|row| ... } ⇒ void
- .open(filename) {|csv| ... } ⇒ LightCsv, Object
- .parse(string) {|row| ... } ⇒ Array<Array<String>>?
-
.readlines(filename) ⇒ Array<Array<String>>
All records.
Instance Method Summary collapse
-
#close ⇒ void
close file.
-
#each {|row| ... } ⇒ Object
iterator.
-
#initialize(src) ⇒ LightCsv
constructor
A new instance of LightCsv.
-
#readlines ⇒ Array<Array<String>>
Array of record.
-
#shift ⇒ Array<String>?
return one record.
Constructor Details
#initialize(src) ⇒ LightCsv
Returns a new instance of LightCsv.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lightcsv.rb', line 70 def initialize(src) if src.kind_of? String @file = nil @ss = StringScanner.new(src) else @file = src @ss = StringScanner.new("") end @buf = "" @bufsize = 64*1024 end |
Instance Attribute Details
#bufsize ⇒ Object
Returns the value of attribute bufsize.
81 82 83 |
# File 'lib/lightcsv.rb', line 81 def bufsize @bufsize end |
Class Method Details
.foreach(filename) {|row| ... } ⇒ void
This method returns an undefined value.
21 22 23 24 25 |
# File 'lib/lightcsv.rb', line 21 def self.foreach(filename, &block) self.open(filename) do |f| f.each(&block) end end |
.open(filename) {|csv| ... } ⇒ LightCsv, Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lightcsv.rb', line 55 def self.open(filename, &block) f = File.open(filename) csv = self.new(f) if block begin return block.call(csv) ensure csv.close end else return csv end end |
.parse(string) {|row| ... } ⇒ Array<Array<String>>?
40 41 42 43 44 45 46 47 48 |
# File 'lib/lightcsv.rb', line 40 def self.parse(string, &block) unless block return self.new(string).entries end self.new(string).each do |row| block.call row end return nil end |
.readlines(filename) ⇒ Array<Array<String>>
Returns All records.
29 30 31 32 33 |
# File 'lib/lightcsv.rb', line 29 def self.readlines(filename) self.open(filename) do |f| return f.entries end end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
close file
85 86 87 |
# File 'lib/lightcsv.rb', line 85 def close @file.close if @file end |
#each {|row| ... } ⇒ Object
iterator
120 121 122 123 124 |
# File 'lib/lightcsv.rb', line 120 def each while row = shift yield row end end |
#readlines ⇒ Array<Array<String>>
Array of record
128 129 130 |
# File 'lib/lightcsv.rb', line 128 def readlines return entries end |
#shift ⇒ Array<String>?
return one record.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/lightcsv.rb', line 92 def shift return nil if @ss.eos? and ! read_next_data cols = [] while true if @ss.eos? and ! read_next_data cols << "" break end if @ss.scan(/\"/) until @ss.scan(/((?:\"\"|[^\"])*)\"(,|\r\n|\n|\r|\z)/) read_next_data or raise InvalidFormat, @ss.rest[0,10] end cols << @ss[1].gsub(/\"\"/, '"') else unless @ss.scan(/([^\",\r\n]*)(,|\r\n|\n|\r|\z)/) raise InvalidFormat, @ss.rest[0,10] end cols << @ss[1] end break unless @ss[2] == ',' end cols.clear if cols.size == 1 and cols.first.empty? cols end |