Module: FastererCSV
- Defined in:
- lib/fasterer_csv.rb
Defined Under Namespace
Classes: IOWriter, NoConversion, NumericConversion, Row, Table
Class Method Summary collapse
- .append(data, quot = '~', sep = ',', quotenum = false, &block) ⇒ Object
- .convread(file, 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
- .out(data, mode = 'w', quot = '~', sep = ',', quotenum = false, &block) ⇒ Object
- .parse(io, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block) ⇒ 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
- .write(data, quot = '~', sep = ',', quotenum = false, &block) ⇒ Object
Class Method Details
.append(data, quot = '~', sep = ',', quotenum = false, &block) ⇒ Object
415 416 417 |
# File 'lib/fasterer_csv.rb', line 415 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
307 308 309 310 311 |
# File 'lib/fasterer_csv.rb', line 307 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
405 406 407 408 409 |
# File 'lib/fasterer_csv.rb', line 405 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
297 298 299 |
# File 'lib/fasterer_csv.rb', line 297 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
419 420 421 422 423 424 425 426 427 |
# File 'lib/fasterer_csv.rb', line 419 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
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 383 |
# File 'lib/fasterer_csv.rb', line 317 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
313 314 315 |
# File 'lib/fasterer_csv.rb', line 313 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
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/fasterer_csv.rb', line 385 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
301 302 303 304 305 |
# File 'lib/fasterer_csv.rb', line 301 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
411 412 413 |
# File 'lib/fasterer_csv.rb', line 411 def write(data, quot = '~', sep = ',', quotenum = false, &block) out(data, 'w', quot, sep, quotenum, &block) end |