Module: MotionCSV

Defined in:
lib/motion-csv/version.rb,
lib/motion-csv/motion-csv.rb

Defined Under Namespace

Classes: IOWriter, NoConversion, NumericConversion, Row, Table

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

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



412
413
414
# File 'lib/motion-csv/motion-csv.rb', line 412

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



304
305
306
307
308
# File 'lib/motion-csv/motion-csv.rb', line 304

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



402
403
404
405
406
# File 'lib/motion-csv/motion-csv.rb', line 402

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



294
295
296
# File 'lib/motion-csv/motion-csv.rb', line 294

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



416
417
418
419
420
421
422
423
424
# File 'lib/motion-csv/motion-csv.rb', line 416

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



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
# File 'lib/motion-csv/motion-csv.rb', line 314

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

  io.each_byte do |c|
    next if c == ?\r.ord

    if maybe && c == s
      row << column.convert(true)
      column.clear
      clean, inquot, maybe, field, endline = true, false, false, true, false
    elsif maybe && c == ?\n.ord && 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.ord
      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.ord && 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.ord

      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



310
311
312
# File 'lib/motion-csv/motion-csv.rb', line 310

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



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/motion-csv/motion-csv.rb', line 382

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



298
299
300
301
302
# File 'lib/motion-csv/motion-csv.rb', line 298

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



408
409
410
# File 'lib/motion-csv/motion-csv.rb', line 408

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