類別 CSV::FieldsConverter
注意:請勿直接使用此類別。這是一個內部類別。
公開類別方法
new(options={}) 按一下以切換來源
一個 CSV::FieldsConverter
是用於儲存欄位轉換器屬性的資料結構,這些屬性會在剖析新檔案時傳遞為參數(例如 CSV::Parser.new
(@io, parser_options))
# File lib/csv/fields_converter.rb, line 13 def initialize(options={}) @converters = [] @nil_value = options[:nil_value] @empty_value = options[:empty_value] @empty_value_is_empty_string = (@empty_value == "") @accept_nil = options[:accept_nil] @builtin_converters_name = options[:builtin_converters_name] @need_static_convert = need_static_convert? end
公開實例方法
add_converter(name=nil, &converter) 按一下以切換來源
# File lib/csv/fields_converter.rb, line 23 def add_converter(name=nil, &converter) if name.nil? # custom converter @converters << converter else # named converter combo = builtin_converters[name] case combo when Array # combo converter combo.each do |sub_name| add_converter(sub_name) end else # individual named converter @converters << combo end end end
convert(fields, headers, lineno, quoted_fields) 按一下以切換來源
# File lib/csv/fields_converter.rb, line 47 def convert(fields, headers, lineno, quoted_fields) return fields unless need_convert? fields.collect.with_index do |field, index| if field.nil? field = @nil_value elsif field.is_a?(String) and field.empty? field = @empty_value unless @empty_value_is_empty_string end @converters.each do |converter| break if field.nil? and @accept_nil if converter.arity == 1 # straight field converter field = converter[field] else # FieldInfo converter if headers header = headers[index] else header = nil end quoted = quoted_fields[index] field = converter[field, FieldInfo.new(index, lineno, header, quoted)] end break unless field.is_a?(String) # short-circuit pipeline for speed end field # final state of each field, converted or original end end
each(&block) 按一下以切換來源
# File lib/csv/fields_converter.rb, line 39 def each(&block) @converters.each(&block) end
empty?() 按一下以切換來源
# File lib/csv/fields_converter.rb, line 43 def empty? @converters.empty? end
私人實例方法
builtin_converters() 按一下以切換來源
# File lib/csv/fields_converter.rb, line 85 def builtin_converters @builtin_converters ||= ::CSV.const_get(@builtin_converters_name) end
need_convert?() 按一下以切換來源
# File lib/csv/fields_converter.rb, line 80 def need_convert? @need_static_convert or (not @converters.empty?) end
need_static_convert?() 按一下以切換來源
# File lib/csv/fields_converter.rb, line 76 def need_static_convert? not (@nil_value.nil? and @empty_value_is_empty_string) end