Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby27/share/ruby/csv
File: fields_converter.rb
# frozen_string_literal: true
[0] Fix | Delete
[1] Fix | Delete
class CSV
[2] Fix | Delete
# Note: Don't use this class directly. This is an internal class.
[3] Fix | Delete
class FieldsConverter
[4] Fix | Delete
include Enumerable
[5] Fix | Delete
#
[6] Fix | Delete
# A CSV::FieldsConverter is a data structure for storing the
[7] Fix | Delete
# fields converter properties to be passed as a parameter
[8] Fix | Delete
# when parsing a new file (e.g. CSV::Parser.new(@io, parser_options))
[9] Fix | Delete
#
[10] Fix | Delete
[11] Fix | Delete
def initialize(options={})
[12] Fix | Delete
@converters = []
[13] Fix | Delete
@nil_value = options[:nil_value]
[14] Fix | Delete
@empty_value = options[:empty_value]
[15] Fix | Delete
@empty_value_is_empty_string = (@empty_value == "")
[16] Fix | Delete
@accept_nil = options[:accept_nil]
[17] Fix | Delete
@builtin_converters = options[:builtin_converters]
[18] Fix | Delete
@need_static_convert = need_static_convert?
[19] Fix | Delete
end
[20] Fix | Delete
[21] Fix | Delete
def add_converter(name=nil, &converter)
[22] Fix | Delete
if name.nil? # custom converter
[23] Fix | Delete
@converters << converter
[24] Fix | Delete
else # named converter
[25] Fix | Delete
combo = @builtin_converters[name]
[26] Fix | Delete
case combo
[27] Fix | Delete
when Array # combo converter
[28] Fix | Delete
combo.each do |sub_name|
[29] Fix | Delete
add_converter(sub_name)
[30] Fix | Delete
end
[31] Fix | Delete
else # individual named converter
[32] Fix | Delete
@converters << combo
[33] Fix | Delete
end
[34] Fix | Delete
end
[35] Fix | Delete
end
[36] Fix | Delete
[37] Fix | Delete
def each(&block)
[38] Fix | Delete
@converters.each(&block)
[39] Fix | Delete
end
[40] Fix | Delete
[41] Fix | Delete
def empty?
[42] Fix | Delete
@converters.empty?
[43] Fix | Delete
end
[44] Fix | Delete
[45] Fix | Delete
def convert(fields, headers, lineno)
[46] Fix | Delete
return fields unless need_convert?
[47] Fix | Delete
[48] Fix | Delete
fields.collect.with_index do |field, index|
[49] Fix | Delete
if field.nil?
[50] Fix | Delete
field = @nil_value
[51] Fix | Delete
elsif field.empty?
[52] Fix | Delete
field = @empty_value unless @empty_value_is_empty_string
[53] Fix | Delete
end
[54] Fix | Delete
@converters.each do |converter|
[55] Fix | Delete
break if field.nil? and @accept_nil
[56] Fix | Delete
if converter.arity == 1 # straight field converter
[57] Fix | Delete
field = converter[field]
[58] Fix | Delete
else # FieldInfo converter
[59] Fix | Delete
if headers
[60] Fix | Delete
header = headers[index]
[61] Fix | Delete
else
[62] Fix | Delete
header = nil
[63] Fix | Delete
end
[64] Fix | Delete
field = converter[field, FieldInfo.new(index, lineno, header)]
[65] Fix | Delete
end
[66] Fix | Delete
break unless field.is_a?(String) # short-circuit pipeline for speed
[67] Fix | Delete
end
[68] Fix | Delete
field # final state of each field, converted or original
[69] Fix | Delete
end
[70] Fix | Delete
end
[71] Fix | Delete
[72] Fix | Delete
private
[73] Fix | Delete
def need_static_convert?
[74] Fix | Delete
not (@nil_value.nil? and @empty_value_is_empty_string)
[75] Fix | Delete
end
[76] Fix | Delete
[77] Fix | Delete
def need_convert?
[78] Fix | Delete
@need_static_convert or
[79] Fix | Delete
(not @converters.empty?)
[80] Fix | Delete
end
[81] Fix | Delete
end
[82] Fix | Delete
end
[83] Fix | Delete
[84] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function