Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby27/share/ruby/csv
File: writer.rb
# frozen_string_literal: true
[0] Fix | Delete
[1] Fix | Delete
require_relative "match_p"
[2] Fix | Delete
require_relative "row"
[3] Fix | Delete
[4] Fix | Delete
using CSV::MatchP if CSV.const_defined?(:MatchP)
[5] Fix | Delete
[6] Fix | Delete
class CSV
[7] Fix | Delete
# Note: Don't use this class directly. This is an internal class.
[8] Fix | Delete
class Writer
[9] Fix | Delete
#
[10] Fix | Delete
# A CSV::Writer receives an output, prepares the header, format and output.
[11] Fix | Delete
# It allows us to write new rows in the object and rewind it.
[12] Fix | Delete
#
[13] Fix | Delete
attr_reader :lineno
[14] Fix | Delete
attr_reader :headers
[15] Fix | Delete
[16] Fix | Delete
def initialize(output, options)
[17] Fix | Delete
@output = output
[18] Fix | Delete
@options = options
[19] Fix | Delete
@lineno = 0
[20] Fix | Delete
@fields_converter = nil
[21] Fix | Delete
prepare
[22] Fix | Delete
if @options[:write_headers] and @headers
[23] Fix | Delete
self << @headers
[24] Fix | Delete
end
[25] Fix | Delete
@fields_converter = @options[:fields_converter]
[26] Fix | Delete
end
[27] Fix | Delete
[28] Fix | Delete
#
[29] Fix | Delete
# Adds a new row
[30] Fix | Delete
#
[31] Fix | Delete
def <<(row)
[32] Fix | Delete
case row
[33] Fix | Delete
when Row
[34] Fix | Delete
row = row.fields
[35] Fix | Delete
when Hash
[36] Fix | Delete
row = @headers.collect {|header| row[header]}
[37] Fix | Delete
end
[38] Fix | Delete
[39] Fix | Delete
@headers ||= row if @use_headers
[40] Fix | Delete
@lineno += 1
[41] Fix | Delete
[42] Fix | Delete
row = @fields_converter.convert(row, nil, lineno) if @fields_converter
[43] Fix | Delete
[44] Fix | Delete
converted_row = row.collect do |field|
[45] Fix | Delete
quote(field)
[46] Fix | Delete
end
[47] Fix | Delete
line = converted_row.join(@column_separator) + @row_separator
[48] Fix | Delete
if @output_encoding
[49] Fix | Delete
line = line.encode(@output_encoding)
[50] Fix | Delete
end
[51] Fix | Delete
@output << line
[52] Fix | Delete
[53] Fix | Delete
self
[54] Fix | Delete
end
[55] Fix | Delete
[56] Fix | Delete
#
[57] Fix | Delete
# Winds back to the beginning
[58] Fix | Delete
#
[59] Fix | Delete
def rewind
[60] Fix | Delete
@lineno = 0
[61] Fix | Delete
@headers = nil if @options[:headers].nil?
[62] Fix | Delete
end
[63] Fix | Delete
[64] Fix | Delete
private
[65] Fix | Delete
def prepare
[66] Fix | Delete
@encoding = @options[:encoding]
[67] Fix | Delete
[68] Fix | Delete
prepare_header
[69] Fix | Delete
prepare_format
[70] Fix | Delete
prepare_output
[71] Fix | Delete
end
[72] Fix | Delete
[73] Fix | Delete
def prepare_header
[74] Fix | Delete
headers = @options[:headers]
[75] Fix | Delete
case headers
[76] Fix | Delete
when Array
[77] Fix | Delete
@headers = headers
[78] Fix | Delete
@use_headers = true
[79] Fix | Delete
when String
[80] Fix | Delete
@headers = CSV.parse_line(headers,
[81] Fix | Delete
col_sep: @options[:column_separator],
[82] Fix | Delete
row_sep: @options[:row_separator],
[83] Fix | Delete
quote_char: @options[:quote_character])
[84] Fix | Delete
@use_headers = true
[85] Fix | Delete
when true
[86] Fix | Delete
@headers = nil
[87] Fix | Delete
@use_headers = true
[88] Fix | Delete
else
[89] Fix | Delete
@headers = nil
[90] Fix | Delete
@use_headers = false
[91] Fix | Delete
end
[92] Fix | Delete
return unless @headers
[93] Fix | Delete
[94] Fix | Delete
converter = @options[:header_fields_converter]
[95] Fix | Delete
@headers = converter.convert(@headers, nil, 0)
[96] Fix | Delete
@headers.each do |header|
[97] Fix | Delete
header.freeze if header.is_a?(String)
[98] Fix | Delete
end
[99] Fix | Delete
end
[100] Fix | Delete
[101] Fix | Delete
def prepare_format
[102] Fix | Delete
@column_separator = @options[:column_separator].to_s.encode(@encoding)
[103] Fix | Delete
row_separator = @options[:row_separator]
[104] Fix | Delete
if row_separator == :auto
[105] Fix | Delete
@row_separator = $INPUT_RECORD_SEPARATOR.encode(@encoding)
[106] Fix | Delete
else
[107] Fix | Delete
@row_separator = row_separator.to_s.encode(@encoding)
[108] Fix | Delete
end
[109] Fix | Delete
@quote_character = @options[:quote_character]
[110] Fix | Delete
@force_quotes = @options[:force_quotes]
[111] Fix | Delete
unless @force_quotes
[112] Fix | Delete
@quotable_pattern =
[113] Fix | Delete
Regexp.new("[\r\n".encode(@encoding) +
[114] Fix | Delete
Regexp.escape(@column_separator) +
[115] Fix | Delete
Regexp.escape(@quote_character.encode(@encoding)) +
[116] Fix | Delete
"]".encode(@encoding))
[117] Fix | Delete
end
[118] Fix | Delete
@quote_empty = @options.fetch(:quote_empty, true)
[119] Fix | Delete
end
[120] Fix | Delete
[121] Fix | Delete
def prepare_output
[122] Fix | Delete
@output_encoding = nil
[123] Fix | Delete
return unless @output.is_a?(StringIO)
[124] Fix | Delete
[125] Fix | Delete
output_encoding = @output.internal_encoding || @output.external_encoding
[126] Fix | Delete
if @encoding != output_encoding
[127] Fix | Delete
if @options[:force_encoding]
[128] Fix | Delete
@output_encoding = output_encoding
[129] Fix | Delete
else
[130] Fix | Delete
compatible_encoding = Encoding.compatible?(@encoding, output_encoding)
[131] Fix | Delete
if compatible_encoding
[132] Fix | Delete
@output.set_encoding(compatible_encoding)
[133] Fix | Delete
@output.seek(0, IO::SEEK_END)
[134] Fix | Delete
end
[135] Fix | Delete
end
[136] Fix | Delete
end
[137] Fix | Delete
end
[138] Fix | Delete
[139] Fix | Delete
def quote_field(field)
[140] Fix | Delete
field = String(field)
[141] Fix | Delete
encoded_quote_character = @quote_character.encode(field.encoding)
[142] Fix | Delete
encoded_quote_character +
[143] Fix | Delete
field.gsub(encoded_quote_character,
[144] Fix | Delete
encoded_quote_character * 2) +
[145] Fix | Delete
encoded_quote_character
[146] Fix | Delete
end
[147] Fix | Delete
[148] Fix | Delete
def quote(field)
[149] Fix | Delete
if @force_quotes
[150] Fix | Delete
quote_field(field)
[151] Fix | Delete
else
[152] Fix | Delete
if field.nil? # represent +nil+ fields as empty unquoted fields
[153] Fix | Delete
""
[154] Fix | Delete
else
[155] Fix | Delete
field = String(field) # Stringify fields
[156] Fix | Delete
# represent empty fields as empty quoted fields
[157] Fix | Delete
if (@quote_empty and field.empty?) or @quotable_pattern.match?(field)
[158] Fix | Delete
quote_field(field)
[159] Fix | Delete
else
[160] Fix | Delete
field # unquoted field
[161] Fix | Delete
end
[162] Fix | Delete
end
[163] Fix | Delete
end
[164] Fix | Delete
end
[165] Fix | Delete
end
[166] Fix | Delete
end
[167] Fix | Delete
[168] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function