Class: CsvBuilder::TransliteratingFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_builder/transliterating_filter.rb

Overview

Transliterate into the required encoding if necessary

We can’t rely on the encoding option in the Ruby 1.9 version CSV because this is ignored when it is ‘compatible’ (see Encoding.compatible?with the input for example:

CSV.generate(:encoding => 'ASCII') { |csv| '£12.34'.encoding('UTF-8') }

will generate a UTF-8 encoded string.

Instance Method Summary collapse

Constructor Details

#initialize(csv, input_encoding = 'UTF-8', output_encoding = 'ISO-8859-1') ⇒ TransliteratingFilter

Transliterate into the required encoding if necessary



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/csv_builder/transliterating_filter.rb', line 14

def initialize(csv, input_encoding = 'UTF-8', output_encoding = 'ISO-8859-1')
  self.csv = csv

  if RUBY_VERSION.to_f < 1.9
    # TODO: do some checking to make sure iconv works correctly in
    # current environment. See <tt>ActiveSupport::Inflector#transliterate</tt>
    # definition for details
    #
    # Not using the more standard //IGNORE//TRANSLIT because it raises
    # <tt>Iconv::IllegalSequence,/tt> for some inputs
    self.iconv = Iconv.new("#{output_encoding}//TRANSLIT//IGNORE", input_encoding) if input_encoding != output_encoding
  else
    # <tt>input_encoding</tt> is ignored because we know what this it is
    self.output_encoding = output_encoding
  end
end

Instance Method Details

#<<(row) ⇒ Object Also known as: add_row

Transliterate before passing to CSV so that the right characters (e.g. quotes) get escaped



33
34
35
# File 'lib/csv_builder/transliterating_filter.rb', line 33

def <<(row)
  @csv << convert_row(row)
end