Module: NdrImport::Helpers::File::Delimited

Included in:
File::Delimited
Defined in:
lib/ndr_import/helpers/file/delimited.rb

Overview

This mixin adds delimited file functionality to unified importers.

Instance Method Summary collapse

Instance Method Details

#delimited_rows(path, col_sep = nil, liberal = false) ⇒ Object

Iterate through the file line by line, yielding each one in turn.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ndr_import/helpers/file/delimited.rb', line 31

def delimited_rows(path, col_sep = nil, liberal = false)
  return enum_for(:delimited_rows, path, col_sep, liberal) unless block_given?

  safe_path = SafeFile.safepath_to_string(path)
  encodings = determine_encodings!(safe_path, col_sep, liberal)

  # By now, we know `encodings` should let us read the whole
  # file succesfully; if there are problems, we should crash.
  CSVLibrary.foreach(safe_path, encodings) do |line|
    yield line.map(&:to_s)
  end
end

#delimited_tables(path, col_sep = nil, liberal = false) {|nil, delimited_rows(path, col_sep, liberal)| ... } ⇒ Object

Iterate through the file table by table, yielding each one in turn.

Yields:



24
25
26
27
28
# File 'lib/ndr_import/helpers/file/delimited.rb', line 24

def delimited_tables(path, col_sep = nil, liberal = false)
  return enum_for(:delimited_tables, path, col_sep, liberal) unless block_given?

  yield nil, delimited_rows(path, col_sep, liberal)
end

#read_csv_file(path, liberal = false) ⇒ Object

Read a plain text CSV file, return an array of the content



10
11
12
13
14
15
16
# File 'lib/ndr_import/helpers/file/delimited.rb', line 10

def read_csv_file(path, liberal = false)
  # Read the page below when encountering "CSV::IllegalFormatError" error caused by CSV
  # file generated at MAC OS
  # http://stackoverflow.com/questions/1549139/ruby-cannot-parse-excel-file-exported-as-csv-in-os-x

  read_delimited_file(path, nil, liberal)
end

#read_delimited_file(path, col_sep = nil, liberal = false) ⇒ Object

Slurp the entire file into an array of lines.



19
20
21
# File 'lib/ndr_import/helpers/file/delimited.rb', line 19

def read_delimited_file(path, col_sep = nil, liberal = false)
  delimited_rows(path, col_sep, liberal).to_a
end