Sha256: 85e40cf8ec3fc1bedb4f8c96741bba5aa3cd84a194de459dd0bd50d3672e545b

Contents?: true

Size: 879 Bytes

Versions: 6

Compression:

Stored size: 879 Bytes

Contents

#encoding: utf-8

class CsvBuilder::TransliteratingFilter
  # Transliterate into the required encoding if necessary
  def initialize(faster_csv, input_encoding = 'UTF-8', output_encoding = 'LATIN1')
    @faster_csv = faster_csv

    # TODO: do some checking to make sure iconv works correctly in
    # current environment. See ActiveSupport::Inflector#transliterate
    # definition for details
    #
    # Not using the more standard //IGNORE//TRANSLIT because it raises
    # Iconv::IllegalSequence for some inputs
    @iconv = Iconv.new("#{output_encoding}//TRANSLIT//IGNORE", input_encoding) if input_encoding != output_encoding
  end

  # Transliterate before passing to FasterCSV so that the right characters (e.g. quotes) get escaped
  def <<(row)
    @faster_csv << if @iconv then row.map { |value| @iconv.iconv(value.to_s) } else row end
  end

  alias :add_row :<<
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
csv_builder-2.1.0 lib/csv_builder/transliterating_filter.rb
csv_builder-2.0.2 lib/csv_builder/transliterating_filter.rb
csv_streamer-0.0.3 lib/csv_streamer/transliterating_filter.rb
csv_streamer-0.0.1 lib/csv_builder/transliterating_filter.rb
csv_builder-2.0.1 lib/csv_builder/transliterating_filter.rb
csv_builder-2.0.0 lib/csv_builder/transliterating_filter.rb