Module: NdrImport::Helpers::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
-
#delimited_rows(path, col_sep = nil) ⇒ Object
Iterate through the file line by line, yielding each one in turn.
-
#delimited_tables(path, col_sep = nil) {|nil, delimited_rows(path, col_sep)| ... } ⇒ Object
Iterate through the file table by table, yielding each one in turn.
-
#each_delimited_row(path, col_sep = nil, &block) ⇒ Object
Deprecated method.
-
#each_delimited_table(path, col_sep = nil, &block) ⇒ Object
Deprecated method.
-
#read_csv_file(path) ⇒ Object
Read a plain text CSV file, return an array of the content.
-
#read_delimited_file(path, col_sep = nil) ⇒ Object
Slurp the entire file into an array of lines.
Instance Method Details
#delimited_rows(path, col_sep = nil) ⇒ Object
Iterate through the file line by line, yielding each one in turn.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ndr_import/helpers/file/delimited.rb', line 38 def delimited_rows(path, col_sep = nil) return enum_for(:delimited_rows, path, col_sep) unless block_given? safe_path = SafeFile.safepath_to_string(path) encodings = determine_encodings!(safe_path, col_sep) # 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) unless line.length <= 5 end end |
#delimited_tables(path, col_sep = nil) {|nil, delimited_rows(path, col_sep)| ... } ⇒ Object
Iterate through the file table by table, yielding each one in turn.
24 25 26 27 28 |
# File 'lib/ndr_import/helpers/file/delimited.rb', line 24 def delimited_tables(path, col_sep = nil) return enum_for(:delimited_tables, path, col_sep) unless block_given? yield nil, delimited_rows(path, col_sep) end |
#each_delimited_row(path, col_sep = nil, &block) ⇒ Object
Deprecated method
52 53 54 55 56 |
# File 'lib/ndr_import/helpers/file/delimited.rb', line 52 def each_delimited_row(path, col_sep = nil, &block) Kernel.warn '[warning] each_delimited_row will be deprecated,' \ ' please use delimited_rows instead.' delimited_rows(path, col_sep, &block) end |
#each_delimited_table(path, col_sep = nil, &block) ⇒ Object
Deprecated method
31 32 33 34 35 |
# File 'lib/ndr_import/helpers/file/delimited.rb', line 31 def each_delimited_table(path, col_sep = nil, &block) Kernel.warn '[warning] each_delimited_table will be deprecated,' \ ' please use delimited_tables instead.' delimited_tables(path, col_sep, &block) end |
#read_csv_file(path) ⇒ 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) # 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) end |
#read_delimited_file(path, col_sep = nil) ⇒ 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) delimited_rows(path, col_sep).to_a end |