csv 3.1.2 → 3.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,5 +2,5 @@
2
2
 
3
3
  class CSV
4
4
  # The version of the installed library.
5
- VERSION = "3.1.2"
5
+ VERSION = "3.1.7"
6
6
  end
@@ -43,8 +43,10 @@ class CSV
43
43
 
44
44
  row = @fields_converter.convert(row, nil, lineno) if @fields_converter
45
45
 
46
+ i = -1
46
47
  converted_row = row.collect do |field|
47
- quote(field)
48
+ i += 1
49
+ quote(field, i)
48
50
  end
49
51
  line = converted_row.join(@column_separator) + @row_separator
50
52
  if @output_encoding
@@ -100,6 +102,33 @@ class CSV
100
102
  end
101
103
  end
102
104
 
105
+ def prepare_force_quotes_fields(force_quotes)
106
+ @force_quotes_fields = {}
107
+ force_quotes.each do |name_or_index|
108
+ case name_or_index
109
+ when Integer
110
+ index = name_or_index
111
+ @force_quotes_fields[index] = true
112
+ when String, Symbol
113
+ name = name_or_index.to_s
114
+ if @headers.nil?
115
+ message = ":headers is required when you use field name " +
116
+ "in :force_quotes: " +
117
+ "#{name_or_index.inspect}: #{force_quotes.inspect}"
118
+ raise ArgumentError, message
119
+ end
120
+ index = @headers.index(name)
121
+ next if index.nil?
122
+ @force_quotes_fields[index] = true
123
+ else
124
+ message = ":force_quotes element must be " +
125
+ "field index or field name: " +
126
+ "#{name_or_index.inspect}: #{force_quotes.inspect}"
127
+ raise ArgumentError, message
128
+ end
129
+ end
130
+ end
131
+
103
132
  def prepare_format
104
133
  @column_separator = @options[:column_separator].to_s.encode(@encoding)
105
134
  row_separator = @options[:row_separator]
@@ -109,7 +138,17 @@ class CSV
109
138
  @row_separator = row_separator.to_s.encode(@encoding)
110
139
  end
111
140
  @quote_character = @options[:quote_character]
112
- @force_quotes = @options[:force_quotes]
141
+ force_quotes = @options[:force_quotes]
142
+ if force_quotes.is_a?(Array)
143
+ prepare_force_quotes_fields(force_quotes)
144
+ @force_quotes = false
145
+ elsif force_quotes
146
+ @force_quotes_fields = nil
147
+ @force_quotes = true
148
+ else
149
+ @force_quotes_fields = nil
150
+ @force_quotes = false
151
+ end
113
152
  unless @force_quotes
114
153
  @quotable_pattern =
115
154
  Regexp.new("[\r\n".encode(@encoding) +
@@ -147,16 +186,18 @@ class CSV
147
186
  encoded_quote_character
148
187
  end
149
188
 
150
- def quote(field)
189
+ def quote(field, i)
151
190
  if @force_quotes
152
191
  quote_field(field)
192
+ elsif @force_quotes_fields and @force_quotes_fields[i]
193
+ quote_field(field)
153
194
  else
154
195
  if field.nil? # represent +nil+ fields as empty unquoted fields
155
196
  ""
156
197
  else
157
198
  field = String(field) # Stringify fields
158
199
  # represent empty fields as empty quoted fields
159
- if (@quote_empty and field.empty?) or @quotable_pattern.match?(field)
200
+ if (@quote_empty and field.empty?) or (field.valid_encoding? and @quotable_pattern.match?(field))
160
201
  quote_field(field)
161
202
  else
162
203
  field # unquoted field
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.2
4
+ version: 3.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Edward Gray II
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-10-11 00:00:00.000000000 Z
12
+ date: 2020-08-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -75,11 +75,36 @@ email:
75
75
  - kou@cozmixng.org
76
76
  executables: []
77
77
  extensions: []
78
- extra_rdoc_files: []
78
+ extra_rdoc_files:
79
+ - LICENSE.txt
80
+ - NEWS.md
81
+ - README.md
79
82
  files:
80
83
  - LICENSE.txt
81
84
  - NEWS.md
82
85
  - README.md
86
+ - doc/csv/arguments/io.rdoc
87
+ - doc/csv/options/common/col_sep.rdoc
88
+ - doc/csv/options/common/quote_char.rdoc
89
+ - doc/csv/options/common/row_sep.rdoc
90
+ - doc/csv/options/generating/force_quotes.rdoc
91
+ - doc/csv/options/generating/quote_empty.rdoc
92
+ - doc/csv/options/generating/write_converters.rdoc
93
+ - doc/csv/options/generating/write_empty_value.rdoc
94
+ - doc/csv/options/generating/write_headers.rdoc
95
+ - doc/csv/options/generating/write_nil_value.rdoc
96
+ - doc/csv/options/parsing/converters.rdoc
97
+ - doc/csv/options/parsing/empty_value.rdoc
98
+ - doc/csv/options/parsing/field_size_limit.rdoc
99
+ - doc/csv/options/parsing/header_converters.rdoc
100
+ - doc/csv/options/parsing/headers.rdoc
101
+ - doc/csv/options/parsing/liberal_parsing.rdoc
102
+ - doc/csv/options/parsing/nil_value.rdoc
103
+ - doc/csv/options/parsing/return_headers.rdoc
104
+ - doc/csv/options/parsing/skip_blanks.rdoc
105
+ - doc/csv/options/parsing/skip_lines.rdoc
106
+ - doc/csv/options/parsing/strip.rdoc
107
+ - doc/csv/options/parsing/unconverted_fields.rdoc
83
108
  - lib/csv.rb
84
109
  - lib/csv/core_ext/array.rb
85
110
  - lib/csv/core_ext/string.rb
@@ -96,22 +121,23 @@ licenses:
96
121
  - BSD-2-Clause
97
122
  metadata: {}
98
123
  post_install_message:
99
- rdoc_options: []
124
+ rdoc_options:
125
+ - "--main"
126
+ - README.md
100
127
  require_paths:
101
128
  - lib
102
129
  required_ruby_version: !ruby/object:Gem::Requirement
103
130
  requirements:
104
131
  - - ">="
105
132
  - !ruby/object:Gem::Version
106
- version: 2.3.0
133
+ version: 2.5.0
107
134
  required_rubygems_version: !ruby/object:Gem::Requirement
108
135
  requirements:
109
136
  - - ">="
110
137
  - !ruby/object:Gem::Version
111
138
  version: '0'
112
139
  requirements: []
113
- rubyforge_project:
114
- rubygems_version: 2.7.6.2
140
+ rubygems_version: 3.2.0.rc.1
115
141
  signing_key:
116
142
  specification_version: 4
117
143
  summary: CSV Reading and Writing