dsv 0.13.2 → 0.14.0
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/CHANGELOG +54 -41
- data/TODO +46 -15
- data/lib/DSV/File.rb +7 -7
- data/lib/DSV/String.rb +5 -5
- data/lib/DSV/VERSION.rb +1 -1
- data/lib/dsv.rb +146 -150
- data/test/dsv_test.rb +6 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 52c5aba99e308ac5ec49ca71cd9ff802841b9cc33e67290b6947bbc5ec0c0956
|
|
4
|
+
data.tar.gz: c12693bbcc29d81f5a1527b9e88a6f83194e84c75f42e6ed04f265a01d79ec21
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f3f677737daf0fb1d2a12453142b2bda9e0caf21dd6baa1ef1ad286f8c9f4ac422a923663fa5ee6272e5bc4dd49d99c8f7813572fd56e0a0d6d80d97915fcd77
|
|
7
|
+
data.tar.gz: ac3b055dde0446e5a1997170d875289a0767ecae0e4e8af52f63e7cb1626be22e79b2af52659fa169f69691f7fddf9b0eb54e86103134ca01344cc0558233534
|
data/CHANGELOG
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
## 20261126
|
|
4
4
|
|
|
5
|
+
0.14.0: The internals are private, and split_double_quoted() is renamed split_wholly_quoted().
|
|
6
|
+
|
|
7
|
+
1. ~ DSV: The instance methods it had as SimpleCSV are public, as is repeated_names, which the README documents; the rest are private, below initialize.
|
|
8
|
+
2. ~ DSV, DSV::File, DSV::String: No blank line opens or closes a class body, and in DSV::File and DSV::String initialize is at the head of a private section at the foot of the class.
|
|
9
|
+
3. /split_double_quoted()/split_wholly_quoted()/: Its contract is every field quoted, whatever the quote character.
|
|
10
|
+
4. ~ split_on(), split_quoted(), split_wholly_quoted(): Each comment says what its level of row splitting does.
|
|
11
|
+
5. ~ test/dsv_test.rb: Which instance methods are public and which private is tested.
|
|
12
|
+
6. ~ TODO: Each decided job is listed here alone, those for 1.0 and 1.x included, under a heading naming its release as ROADMAP.md does, without its version. The job for the internals is gone.
|
|
13
|
+
7. ~ ROADMAP.md: Each release says only why it is made, its jobs being in TODO. 0.13.0, 0.13.x and 0.14.0 are gone. 0.15.0 adds filter, 0.16.0 the declarations, and 0.17.0 streaming.
|
|
14
|
+
8. ~ PROPOSED.md: The version of filter is settled: no release has had it, so it is a minor. The Date line is gone.
|
|
15
|
+
9. ~ CHANGELOG: Each version line from 0.12.2 names DSV::VERSION, and no line of glyphs ends in a full stop.
|
|
16
|
+
10. ~ DSV::VERSION: /0.13.2/0.14.0/
|
|
17
|
+
|
|
5
18
|
0.13.2: Create a fast path, where no column name repeats, for writing, to_a, and reading as_array or by name, so that a write takes a third less time.
|
|
6
19
|
|
|
7
20
|
1. ~ values_in_order(): Where no name repeats, the row's values are taken with values_at, no field being looked at. From 0.11.5 every field of every row was checked for an Array, which took about 40% of a write.
|
|
@@ -33,7 +46,7 @@
|
|
|
33
46
|
9. ~ TODO: Only the jobs in hand are kept, in order and without their releases; the rest are in ROADMAP.md. The readers are no longer renamed as nouns, split_double_quoted() alone becoming split_wholly_quoted(). Jobs are added for write keeping a repeated name, and for attributes= setting columns.
|
|
34
47
|
10. ~ ROADMAP.md: Speed is given as the reason for the modes and for no fallback. 0.13.0 undoes the fallback, naming 0.12.5's errors; 0.14.0 makes the internals private; the declarations are 0.15.0 and streaming 0.16.0. Path or text is no longer a theme, and speed against other languages is one.
|
|
35
48
|
11. ~ PROPOSED.md: The exception raised is settled. :collect and :infer are asked after, and the version rule of filter alone. The quote character notes what a quote longer than a byte would cost.
|
|
36
|
-
12. ~
|
|
49
|
+
12. ~ DSV::VERSION: /0.12.15/0.13.0/
|
|
37
50
|
|
|
38
51
|
## 20261120
|
|
39
52
|
|
|
@@ -46,7 +59,7 @@
|
|
|
46
59
|
5. ~ TODO: - 1, 2. The fallback under :double is undone by 0.13.0's declarations, not by a patch back to 0.12.4, whose reading misread rows silently. :unspecified, not :mixed, names the default. Jobs are added: :unquoted as the name of the mode reading no quotes; the reader chosen once, brought forward from 1.x; and .read and .parse taking a path and text apart, into which the String under a write mode taken as a path is folded. :single moves to 0.13.0, once :double raises, since no release has read it.
|
|
47
60
|
6. ~ ROADMAP.md: The design philosophy opens with fail fast, otherwise go fast, and the default is the one reading which interprets. The layout is done, :double back to 0.12.4's reading is no longer listed under 0.12.x, and :single moves to 0.13.0. :unspecified names the default. 0.13.0 lists :unquoted, the reader chosen once and the source given as a path or text, and 2.0 is gone.
|
|
48
61
|
7. ~ PROPOSED.md: The version rule is no longer asked of :single, an addition. :unspecified replaces :mixed in the examples. Path or text asks only how DSV.new is told which a String is.
|
|
49
|
-
8. ~
|
|
62
|
+
8. ~ DSV::VERSION: /0.12.14/0.12.15/
|
|
50
63
|
|
|
51
64
|
## 20261108
|
|
52
65
|
|
|
@@ -59,7 +72,7 @@
|
|
|
59
72
|
5. ~ TODO: One job to a line, each marked with its release, the reasons moved to ROADMAP.md and what is undecided to PROPOSED.md. The space separator is done, and the string predicates were done in 0.12.0. The layout of normalised_mode, row_renderer and write_row, and the renaming of split_quoted() and split_double_quoted(), are added.
|
|
60
73
|
6. ~ ROADMAP.md: Each release is stated as what it does and why; what is undecided is moved to PROPOSED.md.
|
|
61
74
|
7. + PROPOSED.md: What is undecided, under a heading for each theme or change.
|
|
62
|
-
8. ~
|
|
75
|
+
8. ~ DSV::VERSION: /0.12.13/0.12.14/
|
|
63
76
|
|
|
64
77
|
## 20261103
|
|
65
78
|
|
|
@@ -69,7 +82,7 @@
|
|
|
69
82
|
2. ~ DSV.parse_line: passes quote: on, rather than always reading as unspecified; its options to DSV.new written key: value, not with hashrockets.
|
|
70
83
|
3. ~ test/dsv_test.rb: + quote: given as a String on read; + parse_line with quote:.
|
|
71
84
|
4. ~ TODO: - 1.
|
|
72
|
-
5. ~
|
|
85
|
+
5. ~ DSV::VERSION: /0.12.12/0.12.13/
|
|
73
86
|
|
|
74
87
|
0.12.12: as_array: with a column selection gives the columns selected.
|
|
75
88
|
|
|
@@ -78,7 +91,7 @@
|
|
|
78
91
|
3. ~ test/dsv_test.rb: + selections under as_array:, by name, by position, by a repeated name, and from rows already read.
|
|
79
92
|
4. ~ README.md: + as_array:, and a selection under it.
|
|
80
93
|
5. ~ TODO: - 1.
|
|
81
|
-
6. ~
|
|
94
|
+
6. ~ DSV::VERSION: /0.12.11/0.12.12/
|
|
82
95
|
|
|
83
96
|
0.12.11: the class methods close the files they open, and return an Enumerator without a block.
|
|
84
97
|
|
|
@@ -88,14 +101,14 @@
|
|
|
88
101
|
4. ~ test/dsv_test.rb: + files_opened(); + the class methods closing what they open, on an early exit too, and leaving an IO open; + the Enumerators.
|
|
89
102
|
5. ~ README.md: Enumerating from the class, closing and the Enumerator.
|
|
90
103
|
6. ~ TODO: - 1.
|
|
91
|
-
7. ~
|
|
104
|
+
7. ~ DSV::VERSION: /0.12.10/0.12.11/
|
|
92
105
|
|
|
93
106
|
0.12.10: ~ TODO, now grouped by release; + ROADMAP.md.
|
|
94
107
|
|
|
95
108
|
1. ~ TODO: the jobs decided, by release: the fixes left for 0.12.x; modes as declarations in 0.13.0; streaming in 0.14.0; what 1.0 needs; speed and new options in 1.x.
|
|
96
109
|
2. ~ TODO: 11, strict:, and 15, the malformed-quoting option, superseded by field_count: and :rfc_4180; 13, chunks, by a streaming each; 1, 6, 7 and the undecided parts of 2, to ROADMAP.md.
|
|
97
110
|
3. + ROADMAP.md: the design, the boundary for 1.0, the version rule, the release sequence, and the themes after 1.0 with what is undecided in each.
|
|
98
|
-
4. ~
|
|
111
|
+
4. ~ DSV::VERSION: /0.12.9/0.12.10/
|
|
99
112
|
|
|
100
113
|
|
|
101
114
|
## 20261028
|
|
@@ -108,7 +121,7 @@
|
|
|
108
121
|
4. ~ test/dsv_test.rb: - the skip on a quoted field which does not close; + the line it names; the malformed examples now state the default rule.
|
|
109
122
|
5. ~ README.md: Quoting, the rule for input which isn't RFC 4180.
|
|
110
123
|
6. ~ TODO: 12, as UnterminatedQuote finds its line; + 15, the rule as an option.
|
|
111
|
-
7. ~
|
|
124
|
+
7. ~ DSV::VERSION: /0.12.8/0.12.9/
|
|
112
125
|
|
|
113
126
|
0.12.8: a stray quote no longer takes in the next line.
|
|
114
127
|
|
|
@@ -117,7 +130,7 @@
|
|
|
117
130
|
3. ~ split_double_quoted(): takes a row as wholly quoted only where it ends in a closing quote.
|
|
118
131
|
4. + parse_fields(), from parse_row(), so that the read loop splits each row once.
|
|
119
132
|
5. ~ test/dsv_test.rb: - the skip on a stray quote; + a quoted field across lines after a stray quote.
|
|
120
|
-
6. ~
|
|
133
|
+
6. ~ DSV::VERSION: /0.12.7/0.12.8/
|
|
121
134
|
|
|
122
135
|
|
|
123
136
|
## 20261027
|
|
@@ -126,12 +139,12 @@
|
|
|
126
139
|
|
|
127
140
|
1. ~ reassemble_quoted_fields(): a piece closes a field where it ends in an odd run of quotes; + trailing_quotes().
|
|
128
141
|
2. ~ test/dsv_test.rb: - the skip on a doubled quote before a separator.
|
|
129
|
-
3. ~
|
|
142
|
+
3. ~ DSV::VERSION: /0.12.6/0.12.7/
|
|
130
143
|
|
|
131
144
|
0.12.6: + quoting examples.
|
|
132
145
|
|
|
133
146
|
1. ~ test/dsv_test.rb: + a doubled quote before a separator, skipped; + a quote not opening a field read as data; + a stray quote not taking in the next line, skipped; + an unclosed quoted field raising UnterminatedQuote, skipped.
|
|
134
|
-
2. ~
|
|
147
|
+
2. ~ DSV::VERSION: /0.12.5/0.12.6/
|
|
135
148
|
|
|
136
149
|
|
|
137
150
|
## 20261016
|
|
@@ -145,7 +158,7 @@
|
|
|
145
158
|
5. ~ test/dsv_test.rb: - the skip on a Regexp separator with a quoted field; + examples of the above.
|
|
146
159
|
6. ~ README.md: writing with a Regexp raises; how :double reads a row it does not promise.
|
|
147
160
|
7. ~ TODO: 5, - the Regexp separator as a reason for the scanner parser.
|
|
148
|
-
8. ~
|
|
161
|
+
8. ~ DSV::VERSION: /0.12.4/0.12.5/
|
|
149
162
|
|
|
150
163
|
|
|
151
164
|
## 20260913, 1005
|
|
@@ -156,7 +169,7 @@
|
|
|
156
169
|
2. + test/helper.rb, which test/dsv_test.rb now requires in place of its own preamble; + test/VERSION_test.rb and test/gemspec_test.rb, upon bitget.rb's pattern. Run as `rake test`, or a file at a time as before.
|
|
157
170
|
3. test/gemspec_test.rb checks both gemspecs, each against its own file: that each is valid, pins no date, takes its version from DSV::VERSION, names the gem after its own file, ships its own gemspec and not the other, declares no runtime dependency, and declares minitest-mock. Run to 125.
|
|
158
171
|
4. ~ this CHANGELOG: /# CHANGELOG/# dsv\/CHANGELOG/, the name a file states being the file it is rather than the kind of file it is, and - the paragraph upon how the history is kept.
|
|
159
|
-
5. ~
|
|
172
|
+
5. ~ DSV::VERSION: /0.12.3/0.12.4/
|
|
160
173
|
6. Of the work the heading states, the gemspec, the packaging and the three test files fell upon the 13th; the second registration, minitest-mock, both gemspecs under test and this file's reformatting upon the 5th. The commit carries the 13th, where the greater part of it was done.
|
|
161
174
|
|
|
162
175
|
|
|
@@ -166,7 +179,7 @@
|
|
|
166
179
|
|
|
167
180
|
1. + README.md, written once under the DSV name: what it is, reading, selecting columns, enumerating from the class, any delimiter, quoting, writing, r+, repeated header names, parse_line, and what it is not. It claims small, keyed access and any delimiter; speed is measured in 0.13.0 and reported there.
|
|
168
181
|
2. ~ TODO: the rename and the README done.
|
|
169
|
-
3. ~
|
|
182
|
+
3. ~ DSV::VERSION: /0.12.2/0.12.3/
|
|
170
183
|
|
|
171
184
|
0.12.2: + DSV::VERSION.
|
|
172
185
|
|
|
@@ -179,13 +192,13 @@
|
|
|
179
192
|
|
|
180
193
|
1. - the twenty-seven supporting files (_meta, Array, Hash, Object, OpenStruct, Struct, Thoran, and the blank? family), and their requires. Of them the library used four things: Array#extract_options!, now DSV.extract_options, the same line as a class method; Array#peek_options, now DSV::File#options; Object#blank?, asked of a Hash of columns, an Array of names and a selection, now the private blank?(value), nil? || empty?; and String#split_csv, the parser, now the private split_row taking the quote mode and separators from the instance, its reassembly loop as reassemble_quoted_fields, the same string operations, start_with? and end_with? in place of the four regex matches per piece. Array#to_csv and Hash#to_csv had not been called since 0.11.6 and 0.11.2; the other seventeen were reached only through Array#to_csv's object path, which the library never used. Nothing goes under Thoran::; there is nothing left to namespace. The originals stay in ~/lib/ruby for the live SimpleCSV and its other users.
|
|
181
194
|
2. ~ test/dsv_test.rb: - the examples of Array#to_csv and Hash#to_csv, which tested the removed files, and with them the skip on Finding 20, closed by the deletion. Run to 108, 2 skipped.
|
|
182
|
-
3. ~ lib/dsv.rb: /0.12.0/0.12.1
|
|
195
|
+
3. ~ lib/dsv.rb: /0.12.0/0.12.1/
|
|
183
196
|
|
|
184
197
|
|
|
185
198
|
0.12.0: /SimpleCSV/DSV/
|
|
186
199
|
|
|
187
200
|
1. /SimpleCSV/DSV/, /SimpleCSV::File/DSV::File/, /SimpleCSV::String/DSV::String/; lib/SimpleCSV.rb --> lib/dsv.rb, lib/SimpleCSV/ --> lib/DSV/, test/SimpleCSV_test.rb --> test/dsv_test.rb; the requires to match. The supporting files are still required under their old paths, and 0.11.18 being the last SimpleCSV that worked in full, this is that library under its new name: the suite passes as before with only the names changed. Run to 111, 3 skipped.
|
|
188
|
-
2. ~ lib/dsv.rb: /0.11.18/0.12.0
|
|
201
|
+
2. ~ lib/dsv.rb: /0.11.18/0.12.0/
|
|
189
202
|
|
|
190
203
|
0.11.18: + TODO, and the header's Todo, Ideas and Bugs sections retired into it.
|
|
191
204
|
|
|
@@ -196,62 +209,62 @@
|
|
|
196
209
|
|
|
197
210
|
1. ~ SimpleCSV.header_row: returns the header row parsed into its names, as .attributes does, or nil where there is no header row; it had returned the instance's header_row accessor, true or false, the only thing the name could mean at the class level being the row (Finding 9). .first_row remains the raw first line. The instance-level accessor of the same name, the headers: option's value, is unchanged and is a 0.12.0 naming question.
|
|
198
211
|
2. ~ test/SimpleCSV_test.rb: + the class-level header_row with and without a header. Run to 111, 3 skipped.
|
|
199
|
-
3. ~ lib/SimpleCSV.rb: /0.11.16/0.11.17
|
|
212
|
+
3. ~ lib/SimpleCSV.rb: /0.11.16/0.11.17/
|
|
200
213
|
|
|
201
214
|
0.11.16: ~ SimpleCSV#read and #each, selected_columns: given at construction is the selection when a call gives none.
|
|
202
215
|
|
|
203
216
|
1. + selection(): a call's own selection, or the instance's selected_columns: where the call gives none; #read and #each take theirs through it, and #parse, #to_a and the class methods follow. selected_columns: had been stored by #initialize and read by nothing (Finding 12). A single name or position is accepted as well as a list, and a call's own selection wins. Whether the selection becomes a keyword on the calls themselves is a 0.12.0 question; for now the option works under its name.
|
|
204
217
|
2. ~ test/SimpleCSV_test.rb: + the option at construction, a call overriding it, an Enumerator under it, and positions through the class-level read. Run to 110, 3 skipped.
|
|
205
|
-
3. ~ lib/SimpleCSV.rb: /0.11.15/0.11.16
|
|
218
|
+
3. ~ lib/SimpleCSV.rb: /0.11.15/0.11.16/
|
|
206
219
|
|
|
207
220
|
0.11.15: ~ SimpleCSV#read, a second read gives the rows once.
|
|
208
221
|
|
|
209
222
|
1. ~ read(): the rows are those of this read, the source being rewound and read again; a second read had appended them to the first's, doubling them.
|
|
210
223
|
2. ~ test/SimpleCSV_test.rb: - the skip on the read-twice example. Run to 109, 3 skipped.
|
|
211
|
-
3. ~ lib/SimpleCSV.rb: /0.11.14/0.11.15
|
|
224
|
+
3. ~ lib/SimpleCSV.rb: /0.11.14/0.11.15/
|
|
212
225
|
|
|
213
226
|
0.11.14: ~ SimpleCSV.open, the instance is a local, not a class-level variable.
|
|
214
227
|
|
|
215
228
|
1. ~ SimpleCSV.open: the instance it makes is held in a local for the block's duration and returned; it had been kept in the class's own @csv_file, shared across every call and every thread (Finding 22).
|
|
216
229
|
2. ~ test/SimpleCSV_test.rb: - the skip on the open example. Run to 109, 4 skipped.
|
|
217
|
-
3. ~ lib/SimpleCSV.rb: /0.11.13/0.11.14
|
|
230
|
+
3. ~ lib/SimpleCSV.rb: /0.11.13/0.11.14/
|
|
218
231
|
|
|
219
232
|
0.11.13: ~ SimpleCSV::File#initialize, the path is expanded on construction.
|
|
220
233
|
|
|
221
234
|
1. ~ SimpleCSV::File#initialize: the filename is ::File.expand_path of what was given, so #filename is absolute and the file is opened by it (Finding 21). #filename had been @filename ||= File.expand_path(@filename), which never ran, @filename being set already, and which would have called SimpleCSV::File.expand_path had it run, File resolving inside the class to the class itself. - #filename, + attr_reader :filename.
|
|
222
235
|
2. ~ test/SimpleCSV_test.rb: - the skip on the expansion example. Run to 109, 5 skipped.
|
|
223
|
-
3. ~ lib/SimpleCSV.rb: /0.11.12/0.11.13
|
|
236
|
+
3. ~ lib/SimpleCSV.rb: /0.11.12/0.11.13/
|
|
224
237
|
|
|
225
238
|
0.11.12: ~ SimpleCSV#each, an Enumerator without a block.
|
|
226
239
|
|
|
227
240
|
1. ~ each(): returns an Enumerator over the rows, the selection included, when no block is given (Finding 11); it had yielded regardless and raised LocalJumpError, so Enumerable's methods that call each without a block, and each('a').to_a, failed. include Enumerable is honest now.
|
|
228
241
|
2. ~ test/SimpleCSV_test.rb: - the skip on the enumerator example. Run to 109, 6 skipped.
|
|
229
|
-
3. ~ lib/SimpleCSV.rb: /0.11.11/0.11.12
|
|
242
|
+
3. ~ lib/SimpleCSV.rb: /0.11.11/0.11.12/
|
|
230
243
|
|
|
231
244
|
0.11.11: ~ SimpleCSV#to_a, rows keyed by position come back as arrays in position order.
|
|
232
245
|
|
|
233
246
|
1. ~ to_a(): with no columns each row's values in their order, a position-keyed row holding them in position order (Finding 10); the branch had built an array in an inject and thrown it away, giving an empty array per row.
|
|
234
247
|
2. ~ test/SimpleCSV_test.rb: - the skip on the to_a example. Run to 109, 7 skipped.
|
|
235
|
-
3. ~ lib/SimpleCSV.rb: /0.11.10/0.11.11
|
|
248
|
+
3. ~ lib/SimpleCSV.rb: /0.11.10/0.11.11/
|
|
236
249
|
|
|
237
250
|
0.11.10: ~ SimpleCSV.detect, nil when no row matches.
|
|
238
251
|
|
|
239
252
|
1. ~ SimpleCSV.detect: returns nil when the block is true for no row (Finding 8); the fall-through had been .each's return value, every row.
|
|
240
253
|
2. ~ test/SimpleCSV_test.rb: - the skip on the detect example. Run to 109, 8 skipped.
|
|
241
|
-
3. ~ lib/SimpleCSV.rb: /0.11.9/0.11.10
|
|
254
|
+
3. ~ lib/SimpleCSV.rb: /0.11.9/0.11.10/
|
|
242
255
|
|
|
243
256
|
0.11.9: ~ SimpleCSV.read, .parse and .each, a column selection given before the options is honoured.
|
|
244
257
|
|
|
245
258
|
1. ~ SimpleCSV.read, ~ .each: the arguments before the options are the selection and go to the instance's #read or #each, as the two comments of 20240826 asked: SimpleCSV.read(source, 'a', headers: true) reads the a column alone (Finding 7). The names had been passed into new, which discarded them. .parse, .collect, .select, .reject and .detect go through .each and gain it likewise; the two comments go.
|
|
246
259
|
2. ~ test/SimpleCSV_test.rb: - the skip on the selection example, + the selection through .each and .parse. Run to 109, 9 skipped.
|
|
247
|
-
3. ~ lib/SimpleCSV.rb: /0.11.8/0.11.9
|
|
260
|
+
3. ~ lib/SimpleCSV.rb: /0.11.8/0.11.9/
|
|
248
261
|
|
|
249
262
|
0.11.8: ~ SimpleCSV#initialize, the mode is normalised to Ruby's spelling whatever spelling it was given.
|
|
250
263
|
|
|
251
264
|
1. + SimpleCSV.normalised_mode(): the table that SimpleCSV::File#mode held, r, r+, w, w+, a and a+ as themselves and the long names read_only, rw, read_write, write_only and append to them, a Symbol as its String, unspecified as r. #initialize keeps @mode as that, so #columns's test of the mode against 'r', 'r+' and 'a+' holds for every spelling: mode: :r, :read_only and 'rw' read the header row (Finding 6). The parent had kept the option as given and SimpleCSV::File had normalised only its own copy, which the parent's #initialize then overwrote.
|
|
252
265
|
2. ~ SimpleCSV::File#mode: the same table by the same method; #mode reports Ruby's spelling.
|
|
253
266
|
3. ~ test/SimpleCSV_test.rb: - the skip on the mode example, + the reported mode for three spellings. Run to 109, 10 skipped.
|
|
254
|
-
4. ~ lib/SimpleCSV.rb: /0.11.7/0.11.8
|
|
267
|
+
4. ~ lib/SimpleCSV.rb: /0.11.7/0.11.8/
|
|
255
268
|
|
|
256
269
|
0.11.7: ~ SimpleCSV#write_row, a nil value is an empty field, and a row with no columns defined is written from its own keys.
|
|
257
270
|
|
|
@@ -259,7 +272,7 @@
|
|
|
259
272
|
2. ~ write(): with no columns defined and rows keyed by name, the columns, and the header when headers: is on, are taken from the first row's keys; rows keyed by position write positionally and have no header to write.
|
|
260
273
|
3. ~ columns(): memoised upon whether the header has been looked for rather than upon what it gave, a source with no header row yielding nil legitimately; ||= had looked again on every call, reading and rewinding the source, which when the source is the file being written read the rows just written back as a header. ~ columns=(): clears attributes and repeated_names, which are derived from it.
|
|
261
274
|
4. ~ test/SimpleCSV_test.rb: - the skip on the nil example, + nil under :none, + position-keyed and name-keyed rows written with no columns defined. Run to 109, 11 skipped.
|
|
262
|
-
5. ~ lib/SimpleCSV.rb: /0.11.6/0.11.7
|
|
275
|
+
5. ~ lib/SimpleCSV.rb: /0.11.6/0.11.7/
|
|
263
276
|
|
|
264
277
|
0.11.6: ~ SimpleCSV#write_values, a row is rendered inside the class with the instance's separators, a quote inside a value doubled.
|
|
265
278
|
|
|
@@ -268,7 +281,7 @@
|
|
|
268
281
|
3. The spacey modes, :spacey_double, :spacey_single, :spacey_none and :spacey_unquoted, put a space after each separator as Array#to_csv_row did; all seven spellings are kept.
|
|
269
282
|
4. The library no longer calls Array#to_csv; the require stays until Phase 4's scoping, with Hash#to_csv's.
|
|
270
283
|
5. ~ test/SimpleCSV_test.rb: - two skips (Finding 23 and the doubled quote), + a round trip through a pipe and CRLF, + the spacey modes. Run to 107, 12 skipped.
|
|
271
|
-
6. ~ lib/SimpleCSV.rb: /0.11.5/0.11.6
|
|
284
|
+
6. ~ lib/SimpleCSV.rb: /0.11.5/0.11.6/
|
|
272
285
|
|
|
273
286
|
0.11.5: ~ SimpleCSV#columns, a repeated header name keeps every position, and a row holds its values as an Array.
|
|
274
287
|
|
|
@@ -278,7 +291,7 @@
|
|
|
278
291
|
4. + values_in_order(): a row's values in position order, an Array under a repeated name spread back over its positions and a single value under one written at each; to_a, as_array and write_row all use it, so a repeated column round-trips.
|
|
279
292
|
5. The rule, for the README: a value is a String, or an Array where the header repeats the name; columns says which. Keying the repeats as distinct suffixed names (a, a.1), and keeping only the first or the last or refusing, are derivations of the same columns map and arrive later as options.
|
|
280
293
|
6. ~ test/SimpleCSV_test.rb: the empty-name example generalised to any repeated name, + reading, selecting, to_a, as_array and writing under one. Run to 106, 14 skipped.
|
|
281
|
-
7. ~ lib/SimpleCSV.rb: /0.11.4/0.11.5
|
|
294
|
+
7. ~ lib/SimpleCSV.rb: /0.11.4/0.11.5/
|
|
282
295
|
|
|
283
296
|
0.11.4: ~ String#split_csv, the smallest changes that make it read RFC 4180 quoting correctly.
|
|
284
297
|
|
|
@@ -295,7 +308,7 @@
|
|
|
295
308
|
|
|
296
309
|
1. - SimpleCSV::File.open, - SimpleCSV::String.open: each built an instance, kept it in @csv_file, and called super, which built a second one and used that; the first was never closed, so a file was opened twice (Finding 19). SimpleCSV.open already constructs through new on whichever class it is called on, so the overrides added nothing but the leak.
|
|
297
310
|
2. ~ test/SimpleCSV_test.rb: - the two skips, one per class; each example now counts one construction. Run to 103, 22 skipped.
|
|
298
|
-
3. ~ lib/SimpleCSV.rb: /0.11.2/0.11.3
|
|
311
|
+
3. ~ lib/SimpleCSV.rb: /0.11.2/0.11.3/
|
|
299
312
|
|
|
300
313
|
0.11.2: ~ SimpleCSV#write_header, the header row is the column names, selected or all, written as a row.
|
|
301
314
|
|
|
@@ -303,14 +316,14 @@
|
|
|
303
316
|
2. + write_values(): writes one row of values, quoted as @quote says; write_row() and write_header() both end here, so that the write path has one place to take the separators from when it does (Finding 23).
|
|
304
317
|
3. The library no longer calls Hash#to_csv anywhere; the require stays until Phase 4's scoping.
|
|
305
318
|
4. ~ test/SimpleCSV_test.rb: - the skip on the selected header example, and + the same under quote: :none. Run to 103, 24 skipped.
|
|
306
|
-
5. ~ lib/SimpleCSV.rb: /0.11.1/0.11.2
|
|
319
|
+
5. ~ lib/SimpleCSV.rb: /0.11.1/0.11.2/
|
|
307
320
|
|
|
308
321
|
0.11.1: ~ SimpleCSV#write, the file is emptied at the first write under r+, not at the end of every read.
|
|
309
322
|
|
|
310
323
|
1. + prepare_to_rewrite(): under r+ reads the rows if they have not been read, then rewinds and truncates the file, once, before the header and rows are written. This is what the 2006 rewind and truncate at the end of #read were for, a read-then-rewrite of one file, moved to the write that the read was preparing for.
|
|
311
324
|
2. ~ SimpleCSV#read: - the rewind and truncate under r+. Reading no longer alters the file on disk, whether by #read, #each, #to_a, #count or any other Enumerable method, nor does a second #read find an empty file (Finding 15). A #write with the rows not yet read no longer loses its header to a truncate that followed it.
|
|
312
325
|
3. ~ test/SimpleCSV_test.rb: - the skip on the r+ example, and + two examples: reading under r+ by #read and by #count leaves the file as it was; #write under r+ with the rows unread rewrites the file with its header, and with rows set replaces it. Run to 103, 25 skipped.
|
|
313
|
-
4. ~ lib/SimpleCSV.rb: /0.11.0/0.11.1
|
|
326
|
+
4. ~ lib/SimpleCSV.rb: /0.11.0/0.11.1/
|
|
314
327
|
|
|
315
328
|
0.11.0: + test/SimpleCSV_test.rb, and the header's Changes section retired to CHANGELOG.
|
|
316
329
|
|
|
@@ -446,7 +459,7 @@
|
|
|
446
459
|
1. ~ SimpleCSV#initialize, so as the default quoting is :none, not :double.
|
|
447
460
|
2. + SimpleCSV#read_header, which I'd mistakenly taken out in the recent cull... for use with SimpleCSV#read.
|
|
448
461
|
3. ~ SimpleCSV#read, calls read_header, so the first line isn't pulled in as data.
|
|
449
|
-
4. ~ SimpleCSV#initialize, /use_array/as_array
|
|
462
|
+
4. ~ SimpleCSV#initialize, /use_array/as_array/
|
|
450
463
|
5. ~ SimpleCSV#initialize, compressed a couple of the if statements, since they weren't complicated enough to be over 7 lines each.
|
|
451
464
|
6. ~ SimpleCSV#parse_row, logic was inverted for when @columns.blank? after a change in the logic for at 0.9.0!
|
|
452
465
|
7. ~ SimpleCSV#attributes, /columns/@columns.blank?/, and switched the logic order(!), since this is a little more robust and probably slightly faster too.
|
|
@@ -514,7 +527,7 @@
|
|
|
514
527
|
|
|
515
528
|
0.9.0
|
|
516
529
|
|
|
517
|
-
1. /CSVFile/SimpleCSV
|
|
530
|
+
1. /CSVFile/SimpleCSV/
|
|
518
531
|
2. Reset the Todo list and rolled in the Goals to that.
|
|
519
532
|
3. Moved the loader stuff (Array, Hash, String) in here.
|
|
520
533
|
4. More changes to interfaces to reflect the change in 0.8.0 to interface arguments.
|
|
@@ -551,7 +564,7 @@
|
|
|
551
564
|
2. ~ #read...
|
|
552
565
|
3. + #set_columns.
|
|
553
566
|
4. + #readrow.
|
|
554
|
-
5. /write_csv/write
|
|
567
|
+
5. /write_csv/write/
|
|
555
568
|
6. alias_method :write_csv, :write
|
|
556
569
|
7. + #each_csv.
|
|
557
570
|
8. + #csv_each.
|
|
@@ -568,7 +581,7 @@
|
|
|
568
581
|
4. + .first_row
|
|
569
582
|
5. + .attributes
|
|
570
583
|
6. + .columns
|
|
571
|
-
7. /#read_csv/#read
|
|
584
|
+
7. /#read_csv/#read/
|
|
572
585
|
8. ~ #do_read to use parse_row instead of parse_line.
|
|
573
586
|
9. + alias_method :parse_row, :read_row
|
|
574
587
|
10. ~ #columns=, fixed when taking a hash.
|
|
@@ -579,8 +592,8 @@
|
|
|
579
592
|
1. Removed all the debug stuff.
|
|
580
593
|
2. require'ing of String placed in Array.rb.
|
|
581
594
|
3. include Enumerable.
|
|
582
|
-
4. /line/row
|
|
583
|
-
5. /separator/row_separator
|
|
595
|
+
4. /line/row/
|
|
596
|
+
5. /separator/row_separator/
|
|
584
597
|
6. ~ #read_csv.
|
|
585
598
|
7. + #do_read.
|
|
586
599
|
8. + #read_header.
|
|
@@ -634,7 +647,7 @@
|
|
|
634
647
|
|
|
635
648
|
0.5.10
|
|
636
649
|
|
|
637
|
-
1. /read/read_csv
|
|
650
|
+
1. /read/read_csv/
|
|
638
651
|
2. Dropped in some class methods which are File/IO interface similarity/compatibility stuff (open, readlines, read, write, writelines) written between 0.6.4 and 0.6.5 as per notes2.txt.
|
|
639
652
|
3. Added some aliases for read and write: read_csv and write_csv.
|
|
640
653
|
4. Removed self.class from expand_path in self.open.
|
|
@@ -971,10 +984,10 @@
|
|
|
971
984
|
|
|
972
985
|
0.2.0
|
|
973
986
|
|
|
974
|
-
1. /from_csv/parse
|
|
987
|
+
1. /from_csv/parse/
|
|
975
988
|
2. /lines/rows/ only because I'm using the term columns and it seems to fit in with that better---although columns are the column names, not the column values. I'm not committed to it and may change this back.
|
|
976
989
|
3. Added < File to the CSVFile class definition.
|
|
977
|
-
4. /@file_handle/self
|
|
990
|
+
4. /@file_handle/self/
|
|
978
991
|
5. I decided to only use the term rows to designate parsed data and so is essentially restricted to @rows and related.
|
|
979
992
|
6. /lines/rows/. I changed it back again, because lines seems more natural. Still not sure about this, but so as to accommodat rows, I'm providing a rows method which returns @lines.
|
|
980
993
|
|
data/TODO
CHANGED
|
@@ -1,18 +1,49 @@
|
|
|
1
1
|
# TODO
|
|
2
2
|
|
|
3
|
+
## `filter`
|
|
4
|
+
|
|
3
5
|
1. filter, a class-level alias of select.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
|
|
7
|
+
## Modes as declarations
|
|
8
|
+
|
|
9
|
+
2. An unknown quote mode, file mode or option raises ArgumentError.
|
|
10
|
+
3. :unspecified, the name of the default.
|
|
11
|
+
4. :unquoted, the name of the mode reading no quotes, :none kept as an alias.
|
|
12
|
+
5. The reader chosen once, when the quote mode is set, as row_renderer is for writing.
|
|
13
|
+
6. .read and .open taking a path, and .parse text; a path which does not exist raising, and nothing inferred from whether a file exists.
|
|
14
|
+
7. :single read as :double is, with a single quote.
|
|
15
|
+
8. attributes= setting columns, attributes always worked out from columns; rows read afterwards keyed by those names.
|
|
16
|
+
|
|
17
|
+
## Streaming
|
|
18
|
+
|
|
19
|
+
9. each yields each row as it is read.
|
|
20
|
+
10. each returns its receiver.
|
|
21
|
+
11. rows no longer filled by each.
|
|
22
|
+
12. Chunks from each_slice.
|
|
23
|
+
13. The source read once, start to end, with no rewind; the header from the first row read.
|
|
24
|
+
14. The line of an error found by seeking only where the source can.
|
|
25
|
+
15. A rewind left to r+ rewriting and to first_row called alone.
|
|
26
|
+
|
|
27
|
+
## Checked and measured
|
|
28
|
+
|
|
29
|
+
16. A corpus in the tests, a day of NEMWEB's dispatch files, trimmed, and the RFC 4180 cases, each checked against stdlib CSV's rows.
|
|
30
|
+
17. A benchmark script against stdlib CSV, and the README's speed sentence written from its output.
|
|
31
|
+
18. The README showing every mode by example, and a table matching each to stdlib CSV's and Python's settings.
|
|
32
|
+
|
|
33
|
+
## Speed, results unchanged
|
|
34
|
+
|
|
35
|
+
19. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full.
|
|
36
|
+
20. as_array: returning the split fields, rather than building a Hash and then an Array from it.
|
|
37
|
+
21. Rows built by zip.to_h.
|
|
38
|
+
22. The scanner-based parser, at the tag scanner-parser-20261006, judged against the string operations used now.
|
|
39
|
+
|
|
40
|
+
## Features
|
|
41
|
+
|
|
42
|
+
23. :rfc_4180, a quote mode reading what RFC 4180 allows as :unspecified does and raising on what it forbids.
|
|
43
|
+
24. field_count:, :header or an Integer, off by default; a row of another width raising.
|
|
44
|
+
25. Writing with minimal quoting: a field quoted only where it holds a separator, a quote or a row terminator.
|
|
45
|
+
26. A line number in each parse error, and line_numbers:.
|
|
46
|
+
27. headers?, the instance-level header_row renamed, header_row kept as an alias.
|
|
47
|
+
28. <<, to append a row, and generate, to build a string.
|
|
48
|
+
29. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys.
|
|
49
|
+
30. Duplicate headers by option: all kept as an Array, the default; indexed, a, a.1; first; last; error.
|
data/lib/DSV/File.rb
CHANGED
|
@@ -5,13 +5,6 @@ require_relative '../dsv'
|
|
|
5
5
|
|
|
6
6
|
class DSV
|
|
7
7
|
class File < DSV
|
|
8
|
-
|
|
9
|
-
def initialize(filename, *args)
|
|
10
|
-
@filename = ::File.expand_path(filename)
|
|
11
|
-
@args = args
|
|
12
|
-
super(source, *args)
|
|
13
|
-
end
|
|
14
|
-
|
|
15
8
|
def source
|
|
16
9
|
@source ||= ::File.new(filename, mode, permissions)
|
|
17
10
|
end
|
|
@@ -31,5 +24,12 @@ class DSV
|
|
|
31
24
|
|
|
32
25
|
attr_reader :filename
|
|
33
26
|
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def initialize(filename, *args)
|
|
30
|
+
@filename = ::File.expand_path(filename)
|
|
31
|
+
@args = args
|
|
32
|
+
super(source, *args)
|
|
33
|
+
end
|
|
34
34
|
end
|
|
35
35
|
end
|
data/lib/DSV/String.rb
CHANGED
|
@@ -5,15 +5,15 @@ require_relative '../dsv'
|
|
|
5
5
|
|
|
6
6
|
class DSV
|
|
7
7
|
class String < DSV
|
|
8
|
+
def source
|
|
9
|
+
@source ||= StringIO.new(@string)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
8
13
|
|
|
9
14
|
def initialize(string, *args)
|
|
10
15
|
@string = string
|
|
11
16
|
super(source, *args)
|
|
12
17
|
end
|
|
13
|
-
|
|
14
|
-
def source
|
|
15
|
-
@source ||= StringIO.new(@string)
|
|
16
|
-
end
|
|
17
|
-
|
|
18
18
|
end
|
|
19
19
|
end
|
data/lib/DSV/VERSION.rb
CHANGED
data/lib/dsv.rb
CHANGED
|
@@ -14,9 +14,7 @@ require 'DSV/UnexpectedRow'
|
|
|
14
14
|
require 'DSV/UnquotedField'
|
|
15
15
|
|
|
16
16
|
class DSV
|
|
17
|
-
|
|
18
17
|
class << self
|
|
19
|
-
|
|
20
18
|
# The trailing Hash of an argument list, removed from it; or none.
|
|
21
19
|
def extract_options(args)
|
|
22
20
|
args.last.is_a?(::Hash) ? args.pop : {}
|
|
@@ -141,46 +139,16 @@ class DSV
|
|
|
141
139
|
ensure
|
|
142
140
|
dsv.close if dsv && source.is_a?(::String)
|
|
143
141
|
end
|
|
144
|
-
|
|
145
142
|
end # class << self
|
|
146
143
|
|
|
147
144
|
include Enumerable
|
|
148
145
|
|
|
149
146
|
attr_accessor :header_row, :mode, :quote, :row_separator, :selected_columns, :as_array, :rows
|
|
150
147
|
|
|
151
|
-
def initialize(source, *args)
|
|
152
|
-
@source = (
|
|
153
|
-
if source.is_a?(::String)
|
|
154
|
-
DSV.source_type(source).new(source, *args).source
|
|
155
|
-
else
|
|
156
|
-
source
|
|
157
|
-
end
|
|
158
|
-
)
|
|
159
|
-
options = DSV.extract_options(args)
|
|
160
|
-
@header_row = options[:header_row] || options[:headers] || options[:header] || false
|
|
161
|
-
@mode = DSV.normalised_mode(options[:mode])
|
|
162
|
-
@quote = options[:quote] || nil
|
|
163
|
-
@row_separator = options[:row_separator] || options[:row_sep] || "\n"
|
|
164
|
-
@column_separator = options[:column_separator] || options[:col_sep] || ','
|
|
165
|
-
@selected_columns = options[:selected_columns]
|
|
166
|
-
@as_array = options[:as_array] || false
|
|
167
|
-
if options[:columns]
|
|
168
|
-
self.columns = options[:columns]
|
|
169
|
-
else
|
|
170
|
-
self.columns
|
|
171
|
-
end
|
|
172
|
-
@rows = []
|
|
173
|
-
end
|
|
174
|
-
|
|
175
148
|
def close
|
|
176
149
|
@source.close
|
|
177
150
|
end
|
|
178
151
|
|
|
179
|
-
# A call's own selection, or the instance's selected_columns: where the call gives none.
|
|
180
|
-
def selection(selected_columns)
|
|
181
|
-
selected_columns.empty? ? Array(@selected_columns) : selected_columns
|
|
182
|
-
end
|
|
183
|
-
|
|
184
152
|
def read(*selected_columns, &block)
|
|
185
153
|
selected_columns = selection(selected_columns)
|
|
186
154
|
if block
|
|
@@ -249,6 +217,148 @@ class DSV
|
|
|
249
217
|
@repeated_names ||= (columns || {}).select{|name, position| position.is_a?(::Array)}.keys
|
|
250
218
|
end
|
|
251
219
|
|
|
220
|
+
def parse_row(raw_row, *selected_columns)
|
|
221
|
+
parse_fields(split_row(raw_row) || raise(UnterminatedQuote.new(1)), *selected_columns)
|
|
222
|
+
rescue UnexpectedRow => error
|
|
223
|
+
error.line = 1
|
|
224
|
+
raise
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# With no columns defined, rows keyed by name supply them, and the header, from the first row's keys, a name whose value is an Array once for each of its values; rows keyed by position write positionally and have no header to write.
|
|
228
|
+
def write(*selected_columns)
|
|
229
|
+
prepare_to_rewrite if @mode == 'r+'
|
|
230
|
+
self.columns = @rows[0].flat_map{|name, value| value.is_a?(::Array) ? ::Array.new(value.size, name) : name} if blank?(columns) && @rows[0].is_a?(::Hash) && @rows[0].keys[0].is_a?(::String)
|
|
231
|
+
write_header(*selected_columns) if header_row? && attributes
|
|
232
|
+
each{|row| write_row(row, *selected_columns)}
|
|
233
|
+
end
|
|
234
|
+
alias_method :write_csv, :write
|
|
235
|
+
|
|
236
|
+
def write_header(*selected_columns)
|
|
237
|
+
selected_columns.flatten!
|
|
238
|
+
names = selected_columns.empty? ? attributes : selected_columns
|
|
239
|
+
write_values(names)
|
|
240
|
+
end
|
|
241
|
+
alias_method :write_csv_header, :write_header
|
|
242
|
+
|
|
243
|
+
# A nil value is an empty field, so the columns after it keep their places; with no columns defined a row's values are written in the row's own order.
|
|
244
|
+
def write_row(row, *selected_columns)
|
|
245
|
+
selected_columns.flatten!
|
|
246
|
+
values = (
|
|
247
|
+
if !selected_columns.empty?
|
|
248
|
+
selected_columns.collect{|column| row[column]}
|
|
249
|
+
elsif attributes
|
|
250
|
+
values_in_order(row)
|
|
251
|
+
else
|
|
252
|
+
row.values
|
|
253
|
+
end
|
|
254
|
+
)
|
|
255
|
+
write_values(values)
|
|
256
|
+
end
|
|
257
|
+
alias_method :write_csv_row, :write_row
|
|
258
|
+
|
|
259
|
+
def each(*selected_columns)
|
|
260
|
+
return to_enum(:each, *selected_columns) unless block_given?
|
|
261
|
+
selected_columns = selection(selected_columns)
|
|
262
|
+
selected_columns.flatten!
|
|
263
|
+
if @rows[0]
|
|
264
|
+
if selected_columns.empty?
|
|
265
|
+
@rows.each{|row| yield row}
|
|
266
|
+
elsif @as_array
|
|
267
|
+
@rows.each{|row| yield row.values_at(*positions(selected_columns))}
|
|
268
|
+
else
|
|
269
|
+
@rows.each do |row|
|
|
270
|
+
yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
else
|
|
274
|
+
if selected_columns.empty? || @as_array
|
|
275
|
+
read_csv(selected_columns).each{|row| yield row}
|
|
276
|
+
else
|
|
277
|
+
read_csv(selected_columns).each do |row|
|
|
278
|
+
yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
alias_method :each_row, :each
|
|
284
|
+
|
|
285
|
+
def attributes
|
|
286
|
+
@attributes ||= (
|
|
287
|
+
if blank?(columns)
|
|
288
|
+
nil
|
|
289
|
+
else
|
|
290
|
+
a = []
|
|
291
|
+
columns.each do |k,v|
|
|
292
|
+
case v
|
|
293
|
+
when Array
|
|
294
|
+
v.each{|e| a << [k, e]}
|
|
295
|
+
else
|
|
296
|
+
a << [k, v]
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
a.sort{|a,b| a[1] <=> b[1]}.collect{|a| a[0]}
|
|
300
|
+
end
|
|
301
|
+
)
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def attributes=(attributes)
|
|
305
|
+
@attributes = attributes
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def header_row?
|
|
309
|
+
@header_row
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
def first_row
|
|
313
|
+
@source.rewind
|
|
314
|
+
return_value = @source.gets(@row_separator)
|
|
315
|
+
@source.rewind
|
|
316
|
+
return_value
|
|
317
|
+
end
|
|
318
|
+
alias_method :first_row?, :first_row
|
|
319
|
+
|
|
320
|
+
def to_a
|
|
321
|
+
read_csv unless @rows[0]
|
|
322
|
+
if @as_array
|
|
323
|
+
@rows
|
|
324
|
+
elsif blank?(@columns)
|
|
325
|
+
@rows.collect(&:values)
|
|
326
|
+
else
|
|
327
|
+
@rows.collect{|row| values_in_order(row)}
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
private
|
|
332
|
+
|
|
333
|
+
def initialize(source, *args)
|
|
334
|
+
@source = (
|
|
335
|
+
if source.is_a?(::String)
|
|
336
|
+
DSV.source_type(source).new(source, *args).source
|
|
337
|
+
else
|
|
338
|
+
source
|
|
339
|
+
end
|
|
340
|
+
)
|
|
341
|
+
options = DSV.extract_options(args)
|
|
342
|
+
@header_row = options[:header_row] || options[:headers] || options[:header] || false
|
|
343
|
+
@mode = DSV.normalised_mode(options[:mode])
|
|
344
|
+
@quote = options[:quote] || nil
|
|
345
|
+
@row_separator = options[:row_separator] || options[:row_sep] || "\n"
|
|
346
|
+
@column_separator = options[:column_separator] || options[:col_sep] || ','
|
|
347
|
+
@selected_columns = options[:selected_columns]
|
|
348
|
+
@as_array = options[:as_array] || false
|
|
349
|
+
if options[:columns]
|
|
350
|
+
self.columns = options[:columns]
|
|
351
|
+
else
|
|
352
|
+
self.columns
|
|
353
|
+
end
|
|
354
|
+
@rows = []
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
# A call's own selection, or the instance's selected_columns: where the call gives none.
|
|
358
|
+
def selection(selected_columns)
|
|
359
|
+
selected_columns.empty? ? Array(@selected_columns) : selected_columns
|
|
360
|
+
end
|
|
361
|
+
|
|
252
362
|
# A row's value for a name, keyed for the row: assigned where the name occurs once, gathered where it repeats.
|
|
253
363
|
def store_field(parsed_row, name, value)
|
|
254
364
|
if repeated_names.include?(name)
|
|
@@ -282,13 +392,13 @@ class DSV
|
|
|
282
392
|
when :none, :unquoted
|
|
283
393
|
split_on(row, @column_separator)
|
|
284
394
|
when :double, :double_quoted, :double_quotes
|
|
285
|
-
|
|
395
|
+
split_wholly_quoted(row)
|
|
286
396
|
else
|
|
287
397
|
split_quoted(row)
|
|
288
398
|
end
|
|
289
399
|
end
|
|
290
400
|
|
|
291
|
-
# A row
|
|
401
|
+
# A row cut at its separators, the one place that knows a separator may be a String, a single space or a Regexp. A Regexp's matches divide it, since String#split would also return any groups it captures. A single space is split on as / /, since String#split takes ' ' to mean runs of whitespace, any leading run dropped.
|
|
292
402
|
def split_on(row, separator)
|
|
293
403
|
case separator
|
|
294
404
|
when ::Regexp; pieces_and_separators(row, separator).first
|
|
@@ -309,7 +419,7 @@ class DSV
|
|
|
309
419
|
[pieces << row[position..], separators]
|
|
310
420
|
end
|
|
311
421
|
|
|
312
|
-
#
|
|
422
|
+
# The fields of a row read with no quote mode named: the row cut at its separators, and a quoted field that held one joined back with the separator's own text.
|
|
313
423
|
def split_quoted(row)
|
|
314
424
|
if @column_separator.is_a?(::Regexp)
|
|
315
425
|
reassemble_quoted_fields(*pieces_and_separators(row, @column_separator))
|
|
@@ -319,7 +429,7 @@ class DSV
|
|
|
319
429
|
end
|
|
320
430
|
|
|
321
431
|
# The :double contract, every field quoted: the row split on quote-separator-quote, a closed last field ending in an odd run of quotes, counted from the last piece, whose opening quote the split has already taken. Read in full only where the last field seems open, as it is where it goes on to the next line, or where the split leaves a lone quote in a field, as a field holding quote-separator-quote does. A row not starting with a quote raises UnquotedField, at the cost of one byte compared. nil where the last field does not close.
|
|
322
|
-
def
|
|
432
|
+
def split_wholly_quoted(row)
|
|
323
433
|
raise UnquotedField.new(:double) unless row.start_with?('"')
|
|
324
434
|
fields = split_on(row.delete_prefix('"'), quoted_separator)
|
|
325
435
|
return reassemble_wholly_quoted_fields(row) unless fields.last && trailing_quotes(fields.last, -1).odd?
|
|
@@ -425,13 +535,6 @@ class DSV
|
|
|
425
535
|
@source.read(start).to_s.scan(@row_separator).length + 1
|
|
426
536
|
end
|
|
427
537
|
|
|
428
|
-
def parse_row(raw_row, *selected_columns)
|
|
429
|
-
parse_fields(split_row(raw_row) || raise(UnterminatedQuote.new(1)), *selected_columns)
|
|
430
|
-
rescue UnexpectedRow => error
|
|
431
|
-
error.line = 1
|
|
432
|
-
raise
|
|
433
|
-
end
|
|
434
|
-
|
|
435
538
|
def parse_fields(fields, *selected_columns)
|
|
436
539
|
selected_columns.flatten!
|
|
437
540
|
return fields.values_at(*positions(selected_columns)) if @as_array && !selected_columns.empty?
|
|
@@ -473,15 +576,6 @@ class DSV
|
|
|
473
576
|
selected_columns.flat_map{|column| column.is_a?(::Integer) ? column : Array((columns || {})[column])}.sort
|
|
474
577
|
end
|
|
475
578
|
|
|
476
|
-
# With no columns defined, rows keyed by name supply them, and the header, from the first row's keys, a name whose value is an Array once for each of its values; rows keyed by position write positionally and have no header to write.
|
|
477
|
-
def write(*selected_columns)
|
|
478
|
-
prepare_to_rewrite if @mode == 'r+'
|
|
479
|
-
self.columns = @rows[0].flat_map{|name, value| value.is_a?(::Array) ? ::Array.new(value.size, name) : name} if blank?(columns) && @rows[0].is_a?(::Hash) && @rows[0].keys[0].is_a?(::String)
|
|
480
|
-
write_header(*selected_columns) if header_row? && attributes
|
|
481
|
-
each{|row| write_row(row, *selected_columns)}
|
|
482
|
-
end
|
|
483
|
-
alias_method :write_csv, :write
|
|
484
|
-
|
|
485
579
|
# Under r+ a write replaces the file: the rows are read first if they have not been, then the file is emptied, once, here, rather than at the end of every read.
|
|
486
580
|
def prepare_to_rewrite
|
|
487
581
|
read_csv unless @rows[0]
|
|
@@ -489,29 +583,6 @@ class DSV
|
|
|
489
583
|
@source.truncate(0)
|
|
490
584
|
end
|
|
491
585
|
|
|
492
|
-
def write_header(*selected_columns)
|
|
493
|
-
selected_columns.flatten!
|
|
494
|
-
names = selected_columns.empty? ? attributes : selected_columns
|
|
495
|
-
write_values(names)
|
|
496
|
-
end
|
|
497
|
-
alias_method :write_csv_header, :write_header
|
|
498
|
-
|
|
499
|
-
# A nil value is an empty field, so the columns after it keep their places; with no columns defined a row's values are written in the row's own order.
|
|
500
|
-
def write_row(row, *selected_columns)
|
|
501
|
-
selected_columns.flatten!
|
|
502
|
-
values = (
|
|
503
|
-
if !selected_columns.empty?
|
|
504
|
-
selected_columns.collect{|column| row[column]}
|
|
505
|
-
elsif attributes
|
|
506
|
-
values_in_order(row)
|
|
507
|
-
else
|
|
508
|
-
row.values
|
|
509
|
-
end
|
|
510
|
-
)
|
|
511
|
-
write_values(values)
|
|
512
|
-
end
|
|
513
|
-
alias_method :write_csv_row, :write_row
|
|
514
|
-
|
|
515
586
|
def write_values(values)
|
|
516
587
|
@render_row ||= row_renderer
|
|
517
588
|
@source.write(@render_row.call(values))
|
|
@@ -536,80 +607,6 @@ class DSV
|
|
|
536
607
|
end
|
|
537
608
|
end
|
|
538
609
|
|
|
539
|
-
def each(*selected_columns)
|
|
540
|
-
return to_enum(:each, *selected_columns) unless block_given?
|
|
541
|
-
selected_columns = selection(selected_columns)
|
|
542
|
-
selected_columns.flatten!
|
|
543
|
-
if @rows[0]
|
|
544
|
-
if selected_columns.empty?
|
|
545
|
-
@rows.each{|row| yield row}
|
|
546
|
-
elsif @as_array
|
|
547
|
-
@rows.each{|row| yield row.values_at(*positions(selected_columns))}
|
|
548
|
-
else
|
|
549
|
-
@rows.each do |row|
|
|
550
|
-
yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
|
|
551
|
-
end
|
|
552
|
-
end
|
|
553
|
-
else
|
|
554
|
-
if selected_columns.empty? || @as_array
|
|
555
|
-
read_csv(selected_columns).each{|row| yield row}
|
|
556
|
-
else
|
|
557
|
-
read_csv(selected_columns).each do |row|
|
|
558
|
-
yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
|
|
559
|
-
end
|
|
560
|
-
end
|
|
561
|
-
end
|
|
562
|
-
end
|
|
563
|
-
alias_method :each_row, :each
|
|
564
|
-
|
|
565
|
-
def attributes
|
|
566
|
-
@attributes ||= (
|
|
567
|
-
if blank?(columns)
|
|
568
|
-
nil
|
|
569
|
-
else
|
|
570
|
-
a = []
|
|
571
|
-
columns.each do |k,v|
|
|
572
|
-
case v
|
|
573
|
-
when Array
|
|
574
|
-
v.each{|e| a << [k, e]}
|
|
575
|
-
else
|
|
576
|
-
a << [k, v]
|
|
577
|
-
end
|
|
578
|
-
end
|
|
579
|
-
a.sort{|a,b| a[1] <=> b[1]}.collect{|a| a[0]}
|
|
580
|
-
end
|
|
581
|
-
)
|
|
582
|
-
end
|
|
583
|
-
|
|
584
|
-
def attributes=(attributes)
|
|
585
|
-
@attributes = attributes
|
|
586
|
-
end
|
|
587
|
-
|
|
588
|
-
def header_row?
|
|
589
|
-
@header_row
|
|
590
|
-
end
|
|
591
|
-
|
|
592
|
-
def first_row
|
|
593
|
-
@source.rewind
|
|
594
|
-
return_value = @source.gets(@row_separator)
|
|
595
|
-
@source.rewind
|
|
596
|
-
return_value
|
|
597
|
-
end
|
|
598
|
-
alias_method :first_row?, :first_row
|
|
599
|
-
|
|
600
|
-
def to_a
|
|
601
|
-
read_csv unless @rows[0]
|
|
602
|
-
if @as_array
|
|
603
|
-
@rows
|
|
604
|
-
elsif blank?(@columns)
|
|
605
|
-
@rows.collect(&:values)
|
|
606
|
-
else
|
|
607
|
-
@rows.collect{|row| values_in_order(row)}
|
|
608
|
-
end
|
|
609
|
-
end
|
|
610
|
-
|
|
611
|
-
private
|
|
612
|
-
|
|
613
610
|
def quoted_row_renderer(quote, separator)
|
|
614
611
|
doubled_quote = quote * 2
|
|
615
612
|
->(values){values.collect{|value| quote + value.to_s.gsub(quote, doubled_quote) + quote}.join(separator) + @row_separator}
|
|
@@ -618,5 +615,4 @@ class DSV
|
|
|
618
615
|
def unquoted_row_renderer(separator)
|
|
619
616
|
->(values){values.join(separator) + @row_separator}
|
|
620
617
|
end
|
|
621
|
-
|
|
622
618
|
end # class DSV
|
data/test/dsv_test.rb
CHANGED
|
@@ -385,6 +385,12 @@ describe DSV do
|
|
|
385
385
|
_(DSV.new(DATA).quote).must_be_nil
|
|
386
386
|
end
|
|
387
387
|
|
|
388
|
+
it "has its interface public and its internals private" do
|
|
389
|
+
csv = DSV.new(DATA, headers: true)
|
|
390
|
+
[:close, :read, :read_header, :parse, :columns, :columns=, :repeated_names, :parse_row, :write, :write_header, :write_row, :each, :attributes, :attributes=, :header_row?, :first_row, :to_a].each{|name| _(csv.public_methods).must_include name}
|
|
391
|
+
[:selection, :split_row, :split_on, :split_quoted, :split_wholly_quoted, :reassemble_wholly_quoted_fields, :parse_fields, :row_renderer].each{|name| _(csv.private_methods).must_include name}
|
|
392
|
+
end
|
|
393
|
+
|
|
388
394
|
it "reads the header row under mode: given as a symbol or a long name" do
|
|
389
395
|
_(DSV.new(DATA, headers: true, mode: :r).read).must_equal KEYED
|
|
390
396
|
with_file{|path| _(DSV.new(path, headers: true, mode: :read_only).read).must_equal KEYED}
|