Module: CSVUtils::EncodingOptions
- Defined in:
- lib/csv_utils/encoding_options.rb
Overview
The one place the library decides how files are decoded and encoded. Every class reads with the :encoding CSV option, 'bom|utf-8' by default, so values are UTF-8 strings whatever the locale, and writes in the encoding the values were decoded to, so reading 'Windows-1252:UTF-8' writes UTF-8. File modes never carry an encoding: CSV raises when the mode and the :encoding option both have one.
Constant Summary collapse
- DEFAULT_ENCODING =
Returns the :encoding option used for reading when none is given.
'bom|utf-8'
Class Method Summary collapse
-
.decoded_encoding(encoding) ⇒ String
The encoding of strings read with an :encoding option: the internal encoding of 'external:internal', otherwise the external one, without a 'bom|' prefix.
-
.read(csv_options) ⇒ Hash
Options for reading: the given options with the default :encoding when they have none.
-
.write(csv_options) ⇒ Hash
Options for writing values read with csv_options: the :encoding is the one values were decoded to.
Class Method Details
.decoded_encoding(encoding) ⇒ String
The encoding of strings read with an :encoding option: the internal encoding of 'external:internal', otherwise the external one, without a 'bom|' prefix.
36 37 38 |
# File 'lib/csv_utils/encoding_options.rb', line 36 def self.decoded_encoding(encoding) encoding.to_s.split(':').last.sub(/\Abom\|/i, '') end |
.read(csv_options) ⇒ Hash
Options for reading: the given options with the default :encoding when they have none.
18 19 20 |
# File 'lib/csv_utils/encoding_options.rb', line 18 def self.read() { encoding: DEFAULT_ENCODING }.merge() end |
.write(csv_options) ⇒ Hash
Options for writing values read with csv_options: the :encoding is the one values were decoded to.
25 26 27 |
# File 'lib/csv_utils/encoding_options.rb', line 25 def self.write() .merge(encoding: decoded_encoding(read()[:encoding])) end |