Module: FastererCSV

Defined in:
lib/fasterer_csv.rb

Defined Under Namespace

Classes: IOWriter, NoConversion, NumericConversion, Row, Table

Class Method Summary collapse

Class Method Details

.append(data, quot = '~', sep = ',', quotenum = false, &block) ⇒ Object



414
415
416
# File 'lib/fasterer_csv.rb', line 414

def append(data, quot = '~', sep = ',', quotenum = false, &block)
  out(data, 'a', quot, sep, quotenum, &block)
end

.convread(file, quot = '~', sep = ',', fail_on_malformed = true, column = NumericConversion.new, &block) ⇒ Object



306
307
308
309
310
# File 'lib/fasterer_csv.rb', line 306

def convread(file, quot = '~', sep = ',', fail_on_malformed = true, column = NumericConversion.new, &block)
  File.open(file, 'r') do |io|
    parse(io, quot, sep, fail_on_malformed, column, &block)
  end
end

.generate(quot = '~', sep = ',', &block) ⇒ Object



404
405
406
407
408
# File 'lib/fasterer_csv.rb', line 404

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



296
297
298
# File 'lib/fasterer_csv.rb', line 296

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

.out(data, mode = 'w', quot = '~', sep = ',', quotenum = false, &block) ⇒ Object



418
419
420
421
422
423
424
425
426
# File 'lib/fasterer_csv.rb', line 418

def out(data, mode = 'w', quot = '~', sep = ',', quotenum = false, &block)
  if data.class == String
    File.open(data, mode) do |io|
      out(io, mode, quot, sep, quotenum, &block)
    end
  else
    yield(IOWriter.new(data, quot, sep, quotenum))
  end
end

.parse(io, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ Object



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
382
# File 'lib/fasterer_csv.rb', line 316

def parse(io, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block)
  q, s, row, inquot, clean, maybe, table, field, endline = quot[0], sep[0], [], false, true, false, nil, true, false

  io.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, &block) 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, &block) 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, &block) unless row.empty?
    end
  elsif field
    row << column.convert(maybe)
  end

  table
end

.parse_headers(data, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ Object



312
313
314
# File 'lib/fasterer_csv.rb', line 312

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



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/fasterer_csv.rb', line 384

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



300
301
302
303
304
# File 'lib/fasterer_csv.rb', line 300

def read(file, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block)
  File.open(file, 'r') do |io|
    parse(io, quot, sep, fail_on_malformed, column, &block)
  end
end

.write(data, quot = '~', sep = ',', quotenum = false, &block) ⇒ Object



410
411
412
# File 'lib/fasterer_csv.rb', line 410

def write(data, quot = '~', sep = ',', quotenum = false, &block)
  out(data, 'w', quot, sep, quotenum, &block)
end