Module: FastererCSV
- Defined in:
- lib/fasterer_csv.rb
Defined Under Namespace
Classes: IOWriter, NoConversion, NumericConversion, Row, Table
Class Method Summary collapse
- .convread(file, quot = '~', sep = ',', fail_on_malformed = true, column = NumericConversion.new, &block) ⇒ Object
- .convread_stream(io, stream_length, quot = '~', sep = ',', fail_on_malformed = true, column = NumericConversion.new, &block) ⇒ Object
- .generate(quot = '~', sep = ',', &block) ⇒ Object
- .headers(file, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ Object
- .parse(data, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new) ⇒ Object
- .parse_headers(data, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ Object
- .quot_row(row, q = '~', s = ',', numquot = false) ⇒ Object
- .read(file, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ Object
- .read_stream(io, stream_length, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ Object
- .write(out, quot = '~', sep = ',', quotenum = false, &block) ⇒ Object
Class Method Details
.convread(file, quot = '~', sep = ',', fail_on_malformed = true, column = NumericConversion.new, &block) ⇒ Object
295 296 297 |
# File 'lib/fasterer_csv.rb', line 295 def convread(file, quot = '~', sep = ',', fail_on_malformed = true, column = NumericConversion.new, &block) convread_stream(File.open(file, 'r'), File.size(file), quot, sep, fail_on_malformed, column, &block) end |
.convread_stream(io, stream_length, quot = '~', sep = ',', fail_on_malformed = true, column = NumericConversion.new, &block) ⇒ Object
303 304 305 |
# File 'lib/fasterer_csv.rb', line 303 def convread_stream(io, stream_length, quot = '~', sep = ',', fail_on_malformed = true, column = NumericConversion.new, &block) parse(io.sysread(stream_length), quot, sep, fail_on_malformed, column, &block) end |
.generate(quot = '~', sep = ',', &block) ⇒ Object
403 404 405 406 407 |
# File 'lib/fasterer_csv.rb', line 403 def generate(quot = '~', sep = ',', &block) builder = StringIO.new write(builder, quot, sep, &block) builder.string end |
.headers(file, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ Object
287 288 289 |
# File 'lib/fasterer_csv.rb', line 287 def headers(file, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) parse_headers(File.open(file, 'r') {|io| io.gets }, quot, sep, fail_on_malformed, column, &block) end |
.parse(data, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/fasterer_csv.rb', line 311 def parse(data, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new) q, s, row, inquot, clean, maybe, table, field, endline = quot[0], sep[0], [], false, true, false, nil, true, false data.each_byte do |c| next if c == ?\r if maybe && c == s row << column.convert(true) column.clear clean, inquot, maybe, field, endline = true, false, false, true, false elsif maybe && c == ?\n && table.nil? row << column.convert(true) unless (column.empty? && endline) column.clear table = Table.new(row, fail_on_malformed) unless row.empty? row, clean, inquot, maybe, field, endline = [], true, false, false, false, true elsif maybe && c == ?\n row << column.convert(true) unless (column.empty? && endline) column.clear table << row unless row.empty? row, clean, inquot, maybe, field, endline = [], true, false, false, false, true elsif clean && c == q inquot, clean, endline = true, false, false elsif maybe && c == q column << c clean, maybe, endline = false, false, false elsif c == q maybe, endline = true, false elsif inquot column << c clean, endline = false, false elsif c == s row << column.convert(false) column.clear clean, field, endline = true, true, false elsif c == ?\n && table.nil? row << column.convert(false) unless column.empty? && endline column.clear table = Table.new(row, fail_on_malformed) unless row.empty? row, clean, inquot, field, endline = [], true, false, false, true elsif c == ?\n row << column.convert(false) unless column.empty? && endline column.clear table << row unless row.empty? row, clean, inquot, field, endline = [], true, false, false, true else column << c clean, endline = false, false end end if !clean row << column.convert(maybe) if table table << row unless row.empty? else table = Table.new(row, fail_on_malformed) unless row.empty? end elsif field row << column.convert(maybe) end table.each do |line| yield(line) end if table && block_given? table end |
.parse_headers(data, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ Object
307 308 309 |
# File 'lib/fasterer_csv.rb', line 307 def parse_headers(data, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) parse(data, quot, sep, fail_on_malformed, column, &block).headers end |
.quot_row(row, q = '~', s = ',', numquot = false) ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/fasterer_csv.rb', line 383 def quot_row(row, q = '~', s = ',', numquot = false) num_quot = /(?:[#{q}#{s}\n]|^\d+$)/ need_quot = /[#{q}#{s}\n]/ row.map do |val| if val.nil? "" elsif val.is_a? Numeric val.to_s else quot = (val.is_a?(Symbol) || !numquot) ? need_quot : num_quot val = String(val) if val.length == 0 q * 2 else val[quot] ? q + val.gsub(q, q * 2) + q : val end end end.join(s) + "\n" end |
.read(file, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ Object
291 292 293 |
# File 'lib/fasterer_csv.rb', line 291 def read(file, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) read_stream(File.open(file, 'r'), File.size(file), quot, sep, fail_on_malformed, column, &block) end |
.read_stream(io, stream_length, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ Object
299 300 301 |
# File 'lib/fasterer_csv.rb', line 299 def read_stream(io, stream_length, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) parse(io.sysread(stream_length), quot, sep, fail_on_malformed, column, &block) end |
.write(out, quot = '~', sep = ',', quotenum = false, &block) ⇒ Object
409 410 411 412 413 414 415 416 417 |
# File 'lib/fasterer_csv.rb', line 409 def write(out, quot = '~', sep = ',', quotenum = false, &block) if out.class == String File.open(out, "w") do |io| write(io, quot, sep, quotenum, &block) end else yield(IOWriter.new(out, quot, sep, quotenum)) end end |