Class: Eco::CSV

Inherits:
CSV show all
Extended by:
Data::Files
Defined in:
lib/eco/csv.rb,
lib/eco/csv/split.rb,
lib/eco/csv/table.rb,
lib/eco/csv/stream.rb

Defined Under Namespace

Classes: Split, Stream, Table

Constant Summary

Constants included from Data::Files

Data::Files::DEFAULT_TIMESTAMP_PATTERN

Constants included from Data::Files::Encoding

Data::Files::Encoding::BOM_BYTES

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Class Method Summary collapse

Methods included from Data::Files::ClassMethods

#copy_file, #create_directory, #csv_files, #dir_exists?, #file_basename, #file_empty?, #file_exists?, #file_fullpath, #file_name, #folder_files, #script_subfolder, #split, #timestamp, #timestamp_file

Methods included from Data::Files::Encoding

#encoding, #file_empty?, #file_exists?, #get_file_content_with_encoding, #has_bom?, #remove_bom, #scoped_encoding

Methods included from Language::AuxiliarLogger

#log

Methods included from Data::Files::InstanceMethods

#get_file_content, #read_with_tolerance

Class Method Details

.count(filename, start_at: nil, **kargs) ⇒ Integer

Note:

it excludes headers by default

Returns the number of rows of the file.

Returns:

  • (Integer)

    the number of rows of the file



46
47
48
49
50
51
52
53
54
55
# File 'lib/eco/csv.rb', line 46

def count(filename, start_at: nil, **kargs)
  count = 0
  streamer = Eco::CSV::Stream.new(filename, **kargs)
  streamer.for_each(start_at_idx: start_at) do |row|
    included = true
    included = yield(row) if block_given?
    count += 1            if included
  end
  count
end

.parse(data, **kargs, &block) ⇒ Eco::CSV::Table

Returns:



8
9
10
11
# File 'lib/eco/csv.rb', line 8

def parse(data, **kargs, &block)
  kargs = {headers: true, skip_blanks: true}.merge(kargs)
  Eco::CSV::Table.new(super(data, **kargs, &block))
end

.read(file, **kargs) ⇒ Eco::CSV::Table

Returns:



14
15
16
17
18
19
# File 'lib/eco/csv.rb', line 14

def read(file, **kargs)
  params = {}.tap do |prms|
    prms.merge!(encoding: kargs.delete(:encoding)) if kargs.key?(:encoding)
  end
  parse(get_file_content(file, **params), **kargs)
end

.split(filename, max_rows:, start_at: nil, **kargs) {|row, ridx, fidx, file| ... } ⇒ Eco::CSV::Split

Splits the csv filename into max_rows

Parameters:

  • filename (String)

    the orignal file

  • max_rows (Integer)

    number of rows per file

  • start_at (Integer) (defaults to: nil)

    row that sets the starting point. Leave empty for the full set of rows.

Yields:

  • (row, ridx, fidx, file)

Yield Parameters:

  • row (Integer)

    the row

  • ridx (Integer)

    the index of the row in the source file

  • fidx (Integer)

    the number of the file

  • file (String)

    the default name of the file

Yield Returns:

  • (Bollean)

    whether the row should be included

Returns:



33
34
35
36
37
38
39
40
41
42
# File 'lib/eco/csv.rb', line 33

def split(filename, max_rows:, start_at: nil, **kargs, &block)
  Eco::CSV::Split.new(
    filename,
    max_rows: max_rows,
    start_at: start_at,
    **kargs
  ).tap do |splitter|
    splitter.call(&block)
  end
end