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:, start_at: nil, **kargs) ⇒ Split

Returns a new instance of Split.

Raises:

  • (ArgumentError)


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

def initialize(filename, max_rows:, start_at: nil, **kargs)
  msg = "File '#{filename}' does not exist"
  raise ArgumentError, msg unless ::File.exist?(filename)

  @filename = filename
  @max_rows = max_rows
  @start_at = start_at
  @params   = kargs

  init
end

Instance Attribute Details

#copy_countInteger

Returns number of total output rows.

Returns:

  • (Integer)

    number of total output rows



21
22
23
# File 'lib/eco/csv/split.rb', line 21

def copy_count
  @copy_count ||= 0
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#total_countInteger

Returns number of total input rows.

Returns:

  • (Integer)

    number of total input rows



27
28
29
# File 'lib/eco/csv/split.rb', line 27

def total_count
  @total_count ||= 0
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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/eco/csv/split.rb', line 43

def call(&block)
  stream.for_each(start_at_idx: start_at) do |row, ridx|
    self.total_count += 1
    copy_row(row, ridx, &block)
  end

  out_files
ensure
  puts "Close at row #{row_idx}"
  @csv&.close
end

#out_filesArray<String>

Returns list of created files.

Returns:

  • (Array<String>)

    list of created files



33
34
35
# File 'lib/eco/csv/split.rb', line 33

def out_files
  @out_files ||= []
end