summaryrefslogtreecommitdiff
path: root/lib/csv
diff options
context:
space:
mode:
authorSutou Kouhei <kou@clear-code.com>2020-07-16 06:10:38 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-07-20 02:32:54 +0900
commit178649e6dcb679f4b42cbf723d4f34f81a591304 (patch)
tree409b3d15f302f31bafd162597cc382ad1c4ea58f /lib/csv
parentd9749b4715168ccce020dd43b4815e365881f73e (diff)
downloadruby-178649e6dcb679f4b42cbf723d4f34f81a591304.tar.gz
[ruby/csv] force_quotes: add support for specifying the target indexes or names
GitHub: fix GH-153 Reported by Aleksandr. Thanks!!! https://github.com/ruby/csv/commit/8812c58a26
Diffstat (limited to 'lib/csv')
-rw-r--r--lib/csv/writer.rb47
1 files changed, 44 insertions, 3 deletions
diff --git a/lib/csv/writer.rb b/lib/csv/writer.rb
index 6c02d7473e..d49115fccf 100644
--- a/lib/csv/writer.rb
+++ b/lib/csv/writer.rb
@@ -43,8 +43,10 @@ class CSV
row = @fields_converter.convert(row, nil, lineno) if @fields_converter
+ i = -1
converted_row = row.collect do |field|
- quote(field)
+ i += 1
+ quote(field, i)
end
line = converted_row.join(@column_separator) + @row_separator
if @output_encoding
@@ -100,6 +102,33 @@ class CSV
end
end
+ def prepare_force_quotes_fields(force_quotes)
+ @force_quotes_fields = {}
+ force_quotes.each do |name_or_index|
+ case name_or_index
+ when Integer
+ index = name_or_index
+ @force_quotes_fields[index] = true
+ when String, Symbol
+ name = name_or_index.to_s
+ if @headers.nil?
+ message = ":headers is required when you use field name " +
+ "in :force_quotes: " +
+ "#{name_or_index.inspect}: #{force_quotes.inspect}"
+ raise ArgumentError, message
+ end
+ index = @headers.index(name)
+ next if index.nil?
+ @force_quotes_fields[index] = true
+ else
+ message = ":force_quotes element must be " +
+ "field index or field name: " +
+ "#{name_or_index.inspect}: #{force_quotes.inspect}"
+ raise ArgumentError, message
+ end
+ end
+ end
+
def prepare_format
@column_separator = @options[:column_separator].to_s.encode(@encoding)
row_separator = @options[:row_separator]
@@ -109,7 +138,17 @@ class CSV
@row_separator = row_separator.to_s.encode(@encoding)
end
@quote_character = @options[:quote_character]
- @force_quotes = @options[:force_quotes]
+ force_quotes = @options[:force_quotes]
+ if force_quotes.is_a?(Array)
+ prepare_force_quotes_fields(force_quotes)
+ @force_quotes = false
+ elsif force_quotes
+ @force_quotes_fields = nil
+ @force_quotes = true
+ else
+ @force_quotes_fields = nil
+ @force_quotes = false
+ end
unless @force_quotes
@quotable_pattern =
Regexp.new("[\r\n".encode(@encoding) +
@@ -147,9 +186,11 @@ class CSV
encoded_quote_character
end
- def quote(field)
+ def quote(field, i)
if @force_quotes
quote_field(field)
+ elsif @force_quotes_fields and @force_quotes_fields[i]
+ quote_field(field)
else
if field.nil? # represent +nil+ fields as empty unquoted fields
""