Module: RemoteTable::Delimited
- Defined in:
- lib/remote_table/delimited.rb
Overview
Parses plaintext comma-separated (CSV), tab-separated (TSV), or really anything-delimited files using Ruby’s CSV parser.
Constant Summary collapse
- Engine =
::FasterCSV
- PASSTHROUGH_CSV_SETTINGS =
[ :unconverted_fields, :col_sep, :row_sep, :return_headers, :header_converters, :quote_char, :converters, :force_quotes, ]
Class Method Summary collapse
-
.extended(base) ⇒ Object
Delimited uses Plaintext.
Instance Method Summary collapse
-
#_each ⇒ Object
Yield each row using Ruby’s CSV parser (FasterCSV on Ruby 1.8).
-
#csv_options ⇒ Hash
Passes user-specified options in PASSTHROUGH_CSV_SETTINGS.
Class Method Details
.extended(base) ⇒ Object
Delimited uses Plaintext.
5 6 7 |
# File 'lib/remote_table/delimited.rb', line 5 def self.extended(base) base.extend Plaintext end |
Instance Method Details
#_each ⇒ Object
Yield each row using Ruby’s CSV parser (FasterCSV on Ruby 1.8).
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/remote_table/delimited.rb', line 29 def _each delete_harmful! convert_eol_to_unix! transliterate_whole_file_to_utf8! skip_rows! Engine.new(local_copy.encoded_io, ).each do |row| some_value_present = false if not headers # represent the row as an array array = row.map do |v| v = v.to_s if not some_value_present and not keep_blank_rows and v.present? some_value_present = true end v end if some_value_present or keep_blank_rows yield array end else # represent the row as a hash hash = ::ActiveSupport::OrderedHash.new row.each do |k, v| next unless k.present? v = v.to_s if not some_value_present and not keep_blank_rows and v.present? some_value_present = true end hash[k] = v end if some_value_present or keep_blank_rows yield hash end end end ensure local_copy.cleanup end |
#csv_options ⇒ Hash
Passes user-specified options in PASSTHROUGH_CSV_SETTINGS.
Also maps:
-
:headers
directly -
:keep_blank_rows
to the CSV option:skip_blanks
-
:delimiter
to the CSV option:col_sep
83 84 85 86 87 88 89 |
# File 'lib/remote_table/delimited.rb', line 83 def memo = .slice(*PASSTHROUGH_CSV_SETTINGS) memo[:skip_blanks] = !keep_blank_rows memo[:headers] ||= headers memo[:col_sep] ||= delimiter memo end |