Class: CA::CSVWriter::Processor
- Inherits:
-
Object
- Object
- CA::CSVWriter::Processor
- Defined in:
- lib/carray/io/csv.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#body(strict: true, format: nil) ⇒ Object
write value If option :strict is set, do csv_quote for string element.
- #csv_quote(text) ⇒ Object
-
#header(list = @names) ⇒ Object
puts header.
-
#initialize(table, io, sep, rs, fill, &block) ⇒ Processor
constructor
A new instance of Processor.
-
#names(list) ⇒ Object
set @names.
-
#process(namelist = @names) ⇒ Object
pre processing data.
-
#puts(*argv) ⇒ Object
puts any strings.
- #run ⇒ Object
Constructor Details
#initialize(table, io, sep, rs, fill, &block) ⇒ Processor
Returns a new instance of Processor.
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/carray/io/csv.rb', line 408 def initialize (table, io, sep, rs, fill, &block) @io = io @sep = sep @rs = rs @fill = fill @block = block || proc { body } if table.has_data_class? @names = table.members @table = CArray.merge(CA_OBJECT, table[nil].fields) else @names = table.instance_exec{ @names } if @names.nil? @names = table.instance_exec{ @column_names } end case when table.rank > 2 @table = table.reshape(false,nil).object when table.rank == 1 @table = table[:%,1].object ### convert to CA_OBJECT else @table = table.object ### convert to CA_OBJECT end end if @table.has_mask? @table.unmask(@fill) end @regexp_simple = /#{@sep}/o end |
Instance Method Details
#body(strict: true, format: nil) ⇒ Object
write value If option :strict is set, do csv_quote for string element
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/carray/io/csv.rb', line 475 def body (strict: true, format: nil) if strict case @table.data_type when CA_OBJECT table = @table.to_ca table[:is_kind_of, String].map! { |s| csv_quote(s) } when CA_FIXLEN table = @table.object table.map! { |s| csv_quote(s) } else table = @table.object end else table = @table end if format table.dim0.times do |i| @io.write Kernel::format(format,*table[i,nil].to_a) @io.write(@rs) end else table.dim0.times do |i| @io.write table[i,nil].to_a.join(@sep) @io.write(@rs) end end end |
#csv_quote(text) ⇒ Object
437 438 439 440 441 442 443 |
# File 'lib/carray/io/csv.rb', line 437 def csv_quote (text) text = text.dup if text.gsub!(/"/, '""') or text =~ @regexp_simple ### /#{@sep}|"/ text = '"' + text + '"' end return text end |
#header(list = @names) ⇒ Object
puts header
462 463 464 465 |
# File 'lib/carray/io/csv.rb', line 462 def header (list = @names) @io.write list.map{|s| csv_quote(s)}.join(@sep) @io.write(@rs) end |
#names(list) ⇒ Object
set @names
457 458 459 |
# File 'lib/carray/io/csv.rb', line 457 def names (list) @names = list end |
#process(namelist = @names) ⇒ Object
pre processing data
504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/carray/io/csv.rb', line 504 def process (namelist = @names) if namelist namelist.each_with_index do |name, i| yield(name, @table[nil, i]) end else @table.dim1.times do |i| yield(i, @table[nil,i]) end end end |
#puts(*argv) ⇒ Object
puts any strings
468 469 470 471 |
# File 'lib/carray/io/csv.rb', line 468 def puts (*argv) @io.print(*argv) @io.write(@rs) end |
#run ⇒ Object
445 446 447 448 449 450 451 452 453 454 |
# File 'lib/carray/io/csv.rb', line 445 def run case @block.arity when 1 @block.call(self) when -1, 0 instance_exec(&@block) else raise "invalid block parameter" end end |