# frozen_string_literal: true
# Note: Don't use this class directly. This is an internal class.
# A CSV::FieldsConverter is a data structure for storing the
# fields converter properties to be passed as a parameter
# when parsing a new file (e.g. CSV::Parser.new(@io, parser_options))
def initialize(options={})
@nil_value = options[:nil_value]
@empty_value = options[:empty_value]
@empty_value_is_empty_string = (@empty_value == "")
@accept_nil = options[:accept_nil]
@builtin_converters = options[:builtin_converters]
@need_static_convert = need_static_convert?
def add_converter(name=nil, &converter)
if name.nil? # custom converter
combo = @builtin_converters[name]
when Array # combo converter
else # individual named converter
def convert(fields, headers, lineno)
return fields unless need_convert?
fields.collect.with_index do |field, index|
field = @empty_value unless @empty_value_is_empty_string
@converters.each do |converter|
break if field.nil? and @accept_nil
if converter.arity == 1 # straight field converter
else # FieldInfo converter
field = converter[field, FieldInfo.new(index, lineno, header)]
break unless field.is_a?(String) # short-circuit pipeline for speed
field # final state of each field, converted or original
not (@nil_value.nil? and @empty_value_is_empty_string)