Class: Eco::CSV::Split

Inherits:
Object show all
Includes:
Language::AuxiliarLogger
Defined in:
lib/eco/csv/split.rb

Instance Attribute Summary collapse

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Constructor Details

#initialize(filename, max_rows:, **kargs) ⇒ Split

Returns a new instance of Split.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
# File 'lib/eco/csv/split.rb', line 8

def initialize(filename, max_rows:, **kargs)
  raise ArgumentError, "File '#{filename}' does not exist" unless ::File.exist?(filename)
  @filename = filename
  @max_rows = max_rows
  @params   = kargs
  init
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/eco/csv/split.rb', line 6

def filename
  @filename
end

Instance Method Details

#call {|idx, file| ... } ⇒ Array<String>

Returns names of the generated files.

Yields:

  • (idx, file)

    a block to spot the filename

Yield Parameters:

  • idx (Integer)

    the number of the file

  • file (String)

    the default name of the file

Yield Returns:

  • (String)

    the filename of the file idx.

    • If nil it will create its own filename convention

Returns:

  • (Array<String>)

    names of the generated files



22
23
24
25
26
27
28
29
30
# File 'lib/eco/csv/split.rb', line 22

def call(&block)
  stream.for_each do |row, ridx|
    copy_row(row, ridx, &block)
  end
  out_files
ensure
  puts "Close at row #{row_idx}"
  @csv&.close
end