csv 3.1.3 → 3.1.8
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.
- checksums.yaml +4 -4
- data/NEWS.md +110 -0
- data/README.md +5 -0
- data/doc/csv/arguments/io.rdoc +5 -0
- data/doc/csv/options/common/col_sep.rdoc +57 -0
- data/doc/csv/options/common/quote_char.rdoc +42 -0
- data/doc/csv/options/common/row_sep.rdoc +91 -0
- data/doc/csv/options/generating/force_quotes.rdoc +17 -0
- data/doc/csv/options/generating/quote_empty.rdoc +12 -0
- data/doc/csv/options/generating/write_converters.rdoc +25 -0
- data/doc/csv/options/generating/write_empty_value.rdoc +15 -0
- data/doc/csv/options/generating/write_headers.rdoc +29 -0
- data/doc/csv/options/generating/write_nil_value.rdoc +14 -0
- data/doc/csv/options/parsing/converters.rdoc +46 -0
- data/doc/csv/options/parsing/empty_value.rdoc +13 -0
- data/doc/csv/options/parsing/field_size_limit.rdoc +39 -0
- data/doc/csv/options/parsing/header_converters.rdoc +43 -0
- data/doc/csv/options/parsing/headers.rdoc +63 -0
- data/doc/csv/options/parsing/liberal_parsing.rdoc +19 -0
- data/doc/csv/options/parsing/nil_value.rdoc +12 -0
- data/doc/csv/options/parsing/return_headers.rdoc +22 -0
- data/doc/csv/options/parsing/skip_blanks.rdoc +31 -0
- data/doc/csv/options/parsing/skip_lines.rdoc +37 -0
- data/doc/csv/options/parsing/strip.rdoc +15 -0
- data/doc/csv/options/parsing/unconverted_fields.rdoc +27 -0
- data/doc/csv/recipes/filtering.rdoc +158 -0
- data/doc/csv/recipes/generating.rdoc +298 -0
- data/doc/csv/recipes/parsing.rdoc +545 -0
- data/doc/csv/recipes/recipes.rdoc +6 -0
- data/lib/csv.rb +1724 -568
- data/lib/csv/fields_converter.rb +1 -1
- data/lib/csv/parser.rb +1 -1
- data/lib/csv/row.rb +477 -132
- data/lib/csv/table.rb +750 -108
- data/lib/csv/version.rb +1 -1
- data/lib/csv/writer.rb +45 -4
- metadata +41 -6
data/lib/csv/version.rb
CHANGED
data/lib/csv/writer.rb
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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.
|
|
4
|
+
version: 3.1.8
|
|
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: 2020-
|
|
12
|
+
date: 2020-11-17 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: bundler
|
|
@@ -75,11 +75,44 @@ 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
|
|
82
|
+
- doc/csv/recipes/filtering.rdoc
|
|
83
|
+
- doc/csv/recipes/generating.rdoc
|
|
84
|
+
- doc/csv/recipes/parsing.rdoc
|
|
85
|
+
- doc/csv/recipes/recipes.rdoc
|
|
79
86
|
files:
|
|
80
87
|
- LICENSE.txt
|
|
81
88
|
- NEWS.md
|
|
82
89
|
- README.md
|
|
90
|
+
- doc/csv/arguments/io.rdoc
|
|
91
|
+
- doc/csv/options/common/col_sep.rdoc
|
|
92
|
+
- doc/csv/options/common/quote_char.rdoc
|
|
93
|
+
- doc/csv/options/common/row_sep.rdoc
|
|
94
|
+
- doc/csv/options/generating/force_quotes.rdoc
|
|
95
|
+
- doc/csv/options/generating/quote_empty.rdoc
|
|
96
|
+
- doc/csv/options/generating/write_converters.rdoc
|
|
97
|
+
- doc/csv/options/generating/write_empty_value.rdoc
|
|
98
|
+
- doc/csv/options/generating/write_headers.rdoc
|
|
99
|
+
- doc/csv/options/generating/write_nil_value.rdoc
|
|
100
|
+
- doc/csv/options/parsing/converters.rdoc
|
|
101
|
+
- doc/csv/options/parsing/empty_value.rdoc
|
|
102
|
+
- doc/csv/options/parsing/field_size_limit.rdoc
|
|
103
|
+
- doc/csv/options/parsing/header_converters.rdoc
|
|
104
|
+
- doc/csv/options/parsing/headers.rdoc
|
|
105
|
+
- doc/csv/options/parsing/liberal_parsing.rdoc
|
|
106
|
+
- doc/csv/options/parsing/nil_value.rdoc
|
|
107
|
+
- doc/csv/options/parsing/return_headers.rdoc
|
|
108
|
+
- doc/csv/options/parsing/skip_blanks.rdoc
|
|
109
|
+
- doc/csv/options/parsing/skip_lines.rdoc
|
|
110
|
+
- doc/csv/options/parsing/strip.rdoc
|
|
111
|
+
- doc/csv/options/parsing/unconverted_fields.rdoc
|
|
112
|
+
- doc/csv/recipes/filtering.rdoc
|
|
113
|
+
- doc/csv/recipes/generating.rdoc
|
|
114
|
+
- doc/csv/recipes/parsing.rdoc
|
|
115
|
+
- doc/csv/recipes/recipes.rdoc
|
|
83
116
|
- lib/csv.rb
|
|
84
117
|
- lib/csv/core_ext/array.rb
|
|
85
118
|
- lib/csv/core_ext/string.rb
|
|
@@ -96,21 +129,23 @@ licenses:
|
|
|
96
129
|
- BSD-2-Clause
|
|
97
130
|
metadata: {}
|
|
98
131
|
post_install_message:
|
|
99
|
-
rdoc_options:
|
|
132
|
+
rdoc_options:
|
|
133
|
+
- "--main"
|
|
134
|
+
- README.md
|
|
100
135
|
require_paths:
|
|
101
136
|
- lib
|
|
102
137
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
138
|
requirements:
|
|
104
139
|
- - ">="
|
|
105
140
|
- !ruby/object:Gem::Version
|
|
106
|
-
version: 2.
|
|
141
|
+
version: 2.5.0
|
|
107
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
143
|
requirements:
|
|
109
144
|
- - ">="
|
|
110
145
|
- !ruby/object:Gem::Version
|
|
111
146
|
version: '0'
|
|
112
147
|
requirements: []
|
|
113
|
-
rubygems_version: 3.2.0.
|
|
148
|
+
rubygems_version: 3.2.0.rc.2
|
|
114
149
|
signing_key:
|
|
115
150
|
specification_version: 4
|
|
116
151
|
summary: CSV Reading and Writing
|