dsv 0.13.1 → 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 +62 -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 +156 -155
- 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
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# dsv/CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 20261126
|
|
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
|
+
|
|
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.
|
|
19
|
+
|
|
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.
|
|
21
|
+
2. ~ parse_fields(): Under a selection by name, where no name repeats, each field is assigned rather than passed to store_field().
|
|
22
|
+
3. ~ DSV::VERSION: /0.13.1/0.13.2/
|
|
23
|
+
|
|
3
24
|
## 20261125
|
|
4
25
|
|
|
5
26
|
0.13.1: ~ DSV#write: Ensure that rows with header a,a,b don't lose the duplicate column name and are written back as a,a,b.
|
|
@@ -25,7 +46,7 @@
|
|
|
25
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.
|
|
26
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.
|
|
27
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.
|
|
28
|
-
12. ~
|
|
49
|
+
12. ~ DSV::VERSION: /0.12.15/0.13.0/
|
|
29
50
|
|
|
30
51
|
## 20261120
|
|
31
52
|
|
|
@@ -38,7 +59,7 @@
|
|
|
38
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.
|
|
39
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.
|
|
40
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.
|
|
41
|
-
8. ~
|
|
62
|
+
8. ~ DSV::VERSION: /0.12.14/0.12.15/
|
|
42
63
|
|
|
43
64
|
## 20261108
|
|
44
65
|
|
|
@@ -51,7 +72,7 @@
|
|
|
51
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.
|
|
52
73
|
6. ~ ROADMAP.md: Each release is stated as what it does and why; what is undecided is moved to PROPOSED.md.
|
|
53
74
|
7. + PROPOSED.md: What is undecided, under a heading for each theme or change.
|
|
54
|
-
8. ~
|
|
75
|
+
8. ~ DSV::VERSION: /0.12.13/0.12.14/
|
|
55
76
|
|
|
56
77
|
## 20261103
|
|
57
78
|
|
|
@@ -61,7 +82,7 @@
|
|
|
61
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.
|
|
62
83
|
3. ~ test/dsv_test.rb: + quote: given as a String on read; + parse_line with quote:.
|
|
63
84
|
4. ~ TODO: - 1.
|
|
64
|
-
5. ~
|
|
85
|
+
5. ~ DSV::VERSION: /0.12.12/0.12.13/
|
|
65
86
|
|
|
66
87
|
0.12.12: as_array: with a column selection gives the columns selected.
|
|
67
88
|
|
|
@@ -70,7 +91,7 @@
|
|
|
70
91
|
3. ~ test/dsv_test.rb: + selections under as_array:, by name, by position, by a repeated name, and from rows already read.
|
|
71
92
|
4. ~ README.md: + as_array:, and a selection under it.
|
|
72
93
|
5. ~ TODO: - 1.
|
|
73
|
-
6. ~
|
|
94
|
+
6. ~ DSV::VERSION: /0.12.11/0.12.12/
|
|
74
95
|
|
|
75
96
|
0.12.11: the class methods close the files they open, and return an Enumerator without a block.
|
|
76
97
|
|
|
@@ -80,14 +101,14 @@
|
|
|
80
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.
|
|
81
102
|
5. ~ README.md: Enumerating from the class, closing and the Enumerator.
|
|
82
103
|
6. ~ TODO: - 1.
|
|
83
|
-
7. ~
|
|
104
|
+
7. ~ DSV::VERSION: /0.12.10/0.12.11/
|
|
84
105
|
|
|
85
106
|
0.12.10: ~ TODO, now grouped by release; + ROADMAP.md.
|
|
86
107
|
|
|
87
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.
|
|
88
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.
|
|
89
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.
|
|
90
|
-
4. ~
|
|
111
|
+
4. ~ DSV::VERSION: /0.12.9/0.12.10/
|
|
91
112
|
|
|
92
113
|
|
|
93
114
|
## 20261028
|
|
@@ -100,7 +121,7 @@
|
|
|
100
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.
|
|
101
122
|
5. ~ README.md: Quoting, the rule for input which isn't RFC 4180.
|
|
102
123
|
6. ~ TODO: 12, as UnterminatedQuote finds its line; + 15, the rule as an option.
|
|
103
|
-
7. ~
|
|
124
|
+
7. ~ DSV::VERSION: /0.12.8/0.12.9/
|
|
104
125
|
|
|
105
126
|
0.12.8: a stray quote no longer takes in the next line.
|
|
106
127
|
|
|
@@ -109,7 +130,7 @@
|
|
|
109
130
|
3. ~ split_double_quoted(): takes a row as wholly quoted only where it ends in a closing quote.
|
|
110
131
|
4. + parse_fields(), from parse_row(), so that the read loop splits each row once.
|
|
111
132
|
5. ~ test/dsv_test.rb: - the skip on a stray quote; + a quoted field across lines after a stray quote.
|
|
112
|
-
6. ~
|
|
133
|
+
6. ~ DSV::VERSION: /0.12.7/0.12.8/
|
|
113
134
|
|
|
114
135
|
|
|
115
136
|
## 20261027
|
|
@@ -118,12 +139,12 @@
|
|
|
118
139
|
|
|
119
140
|
1. ~ reassemble_quoted_fields(): a piece closes a field where it ends in an odd run of quotes; + trailing_quotes().
|
|
120
141
|
2. ~ test/dsv_test.rb: - the skip on a doubled quote before a separator.
|
|
121
|
-
3. ~
|
|
142
|
+
3. ~ DSV::VERSION: /0.12.6/0.12.7/
|
|
122
143
|
|
|
123
144
|
0.12.6: + quoting examples.
|
|
124
145
|
|
|
125
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.
|
|
126
|
-
2. ~
|
|
147
|
+
2. ~ DSV::VERSION: /0.12.5/0.12.6/
|
|
127
148
|
|
|
128
149
|
|
|
129
150
|
## 20261016
|
|
@@ -137,7 +158,7 @@
|
|
|
137
158
|
5. ~ test/dsv_test.rb: - the skip on a Regexp separator with a quoted field; + examples of the above.
|
|
138
159
|
6. ~ README.md: writing with a Regexp raises; how :double reads a row it does not promise.
|
|
139
160
|
7. ~ TODO: 5, - the Regexp separator as a reason for the scanner parser.
|
|
140
|
-
8. ~
|
|
161
|
+
8. ~ DSV::VERSION: /0.12.4/0.12.5/
|
|
141
162
|
|
|
142
163
|
|
|
143
164
|
## 20260913, 1005
|
|
@@ -148,7 +169,7 @@
|
|
|
148
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.
|
|
149
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.
|
|
150
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.
|
|
151
|
-
5. ~
|
|
172
|
+
5. ~ DSV::VERSION: /0.12.3/0.12.4/
|
|
152
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.
|
|
153
174
|
|
|
154
175
|
|
|
@@ -158,7 +179,7 @@
|
|
|
158
179
|
|
|
159
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.
|
|
160
181
|
2. ~ TODO: the rename and the README done.
|
|
161
|
-
3. ~
|
|
182
|
+
3. ~ DSV::VERSION: /0.12.2/0.12.3/
|
|
162
183
|
|
|
163
184
|
0.12.2: + DSV::VERSION.
|
|
164
185
|
|
|
@@ -171,13 +192,13 @@
|
|
|
171
192
|
|
|
172
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.
|
|
173
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.
|
|
174
|
-
3. ~ lib/dsv.rb: /0.12.0/0.12.1
|
|
195
|
+
3. ~ lib/dsv.rb: /0.12.0/0.12.1/
|
|
175
196
|
|
|
176
197
|
|
|
177
198
|
0.12.0: /SimpleCSV/DSV/
|
|
178
199
|
|
|
179
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.
|
|
180
|
-
2. ~ lib/dsv.rb: /0.11.18/0.12.0
|
|
201
|
+
2. ~ lib/dsv.rb: /0.11.18/0.12.0/
|
|
181
202
|
|
|
182
203
|
0.11.18: + TODO, and the header's Todo, Ideas and Bugs sections retired into it.
|
|
183
204
|
|
|
@@ -188,62 +209,62 @@
|
|
|
188
209
|
|
|
189
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.
|
|
190
211
|
2. ~ test/SimpleCSV_test.rb: + the class-level header_row with and without a header. Run to 111, 3 skipped.
|
|
191
|
-
3. ~ lib/SimpleCSV.rb: /0.11.16/0.11.17
|
|
212
|
+
3. ~ lib/SimpleCSV.rb: /0.11.16/0.11.17/
|
|
192
213
|
|
|
193
214
|
0.11.16: ~ SimpleCSV#read and #each, selected_columns: given at construction is the selection when a call gives none.
|
|
194
215
|
|
|
195
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.
|
|
196
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.
|
|
197
|
-
3. ~ lib/SimpleCSV.rb: /0.11.15/0.11.16
|
|
218
|
+
3. ~ lib/SimpleCSV.rb: /0.11.15/0.11.16/
|
|
198
219
|
|
|
199
220
|
0.11.15: ~ SimpleCSV#read, a second read gives the rows once.
|
|
200
221
|
|
|
201
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.
|
|
202
223
|
2. ~ test/SimpleCSV_test.rb: - the skip on the read-twice example. Run to 109, 3 skipped.
|
|
203
|
-
3. ~ lib/SimpleCSV.rb: /0.11.14/0.11.15
|
|
224
|
+
3. ~ lib/SimpleCSV.rb: /0.11.14/0.11.15/
|
|
204
225
|
|
|
205
226
|
0.11.14: ~ SimpleCSV.open, the instance is a local, not a class-level variable.
|
|
206
227
|
|
|
207
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).
|
|
208
229
|
2. ~ test/SimpleCSV_test.rb: - the skip on the open example. Run to 109, 4 skipped.
|
|
209
|
-
3. ~ lib/SimpleCSV.rb: /0.11.13/0.11.14
|
|
230
|
+
3. ~ lib/SimpleCSV.rb: /0.11.13/0.11.14/
|
|
210
231
|
|
|
211
232
|
0.11.13: ~ SimpleCSV::File#initialize, the path is expanded on construction.
|
|
212
233
|
|
|
213
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.
|
|
214
235
|
2. ~ test/SimpleCSV_test.rb: - the skip on the expansion example. Run to 109, 5 skipped.
|
|
215
|
-
3. ~ lib/SimpleCSV.rb: /0.11.12/0.11.13
|
|
236
|
+
3. ~ lib/SimpleCSV.rb: /0.11.12/0.11.13/
|
|
216
237
|
|
|
217
238
|
0.11.12: ~ SimpleCSV#each, an Enumerator without a block.
|
|
218
239
|
|
|
219
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.
|
|
220
241
|
2. ~ test/SimpleCSV_test.rb: - the skip on the enumerator example. Run to 109, 6 skipped.
|
|
221
|
-
3. ~ lib/SimpleCSV.rb: /0.11.11/0.11.12
|
|
242
|
+
3. ~ lib/SimpleCSV.rb: /0.11.11/0.11.12/
|
|
222
243
|
|
|
223
244
|
0.11.11: ~ SimpleCSV#to_a, rows keyed by position come back as arrays in position order.
|
|
224
245
|
|
|
225
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.
|
|
226
247
|
2. ~ test/SimpleCSV_test.rb: - the skip on the to_a example. Run to 109, 7 skipped.
|
|
227
|
-
3. ~ lib/SimpleCSV.rb: /0.11.10/0.11.11
|
|
248
|
+
3. ~ lib/SimpleCSV.rb: /0.11.10/0.11.11/
|
|
228
249
|
|
|
229
250
|
0.11.10: ~ SimpleCSV.detect, nil when no row matches.
|
|
230
251
|
|
|
231
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.
|
|
232
253
|
2. ~ test/SimpleCSV_test.rb: - the skip on the detect example. Run to 109, 8 skipped.
|
|
233
|
-
3. ~ lib/SimpleCSV.rb: /0.11.9/0.11.10
|
|
254
|
+
3. ~ lib/SimpleCSV.rb: /0.11.9/0.11.10/
|
|
234
255
|
|
|
235
256
|
0.11.9: ~ SimpleCSV.read, .parse and .each, a column selection given before the options is honoured.
|
|
236
257
|
|
|
237
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.
|
|
238
259
|
2. ~ test/SimpleCSV_test.rb: - the skip on the selection example, + the selection through .each and .parse. Run to 109, 9 skipped.
|
|
239
|
-
3. ~ lib/SimpleCSV.rb: /0.11.8/0.11.9
|
|
260
|
+
3. ~ lib/SimpleCSV.rb: /0.11.8/0.11.9/
|
|
240
261
|
|
|
241
262
|
0.11.8: ~ SimpleCSV#initialize, the mode is normalised to Ruby's spelling whatever spelling it was given.
|
|
242
263
|
|
|
243
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.
|
|
244
265
|
2. ~ SimpleCSV::File#mode: the same table by the same method; #mode reports Ruby's spelling.
|
|
245
266
|
3. ~ test/SimpleCSV_test.rb: - the skip on the mode example, + the reported mode for three spellings. Run to 109, 10 skipped.
|
|
246
|
-
4. ~ lib/SimpleCSV.rb: /0.11.7/0.11.8
|
|
267
|
+
4. ~ lib/SimpleCSV.rb: /0.11.7/0.11.8/
|
|
247
268
|
|
|
248
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.
|
|
249
270
|
|
|
@@ -251,7 +272,7 @@
|
|
|
251
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.
|
|
252
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.
|
|
253
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.
|
|
254
|
-
5. ~ lib/SimpleCSV.rb: /0.11.6/0.11.7
|
|
275
|
+
5. ~ lib/SimpleCSV.rb: /0.11.6/0.11.7/
|
|
255
276
|
|
|
256
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.
|
|
257
278
|
|
|
@@ -260,7 +281,7 @@
|
|
|
260
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.
|
|
261
282
|
4. The library no longer calls Array#to_csv; the require stays until Phase 4's scoping, with Hash#to_csv's.
|
|
262
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.
|
|
263
|
-
6. ~ lib/SimpleCSV.rb: /0.11.5/0.11.6
|
|
284
|
+
6. ~ lib/SimpleCSV.rb: /0.11.5/0.11.6/
|
|
264
285
|
|
|
265
286
|
0.11.5: ~ SimpleCSV#columns, a repeated header name keeps every position, and a row holds its values as an Array.
|
|
266
287
|
|
|
@@ -270,7 +291,7 @@
|
|
|
270
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.
|
|
271
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.
|
|
272
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.
|
|
273
|
-
7. ~ lib/SimpleCSV.rb: /0.11.4/0.11.5
|
|
294
|
+
7. ~ lib/SimpleCSV.rb: /0.11.4/0.11.5/
|
|
274
295
|
|
|
275
296
|
0.11.4: ~ String#split_csv, the smallest changes that make it read RFC 4180 quoting correctly.
|
|
276
297
|
|
|
@@ -287,7 +308,7 @@
|
|
|
287
308
|
|
|
288
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.
|
|
289
310
|
2. ~ test/SimpleCSV_test.rb: - the two skips, one per class; each example now counts one construction. Run to 103, 22 skipped.
|
|
290
|
-
3. ~ lib/SimpleCSV.rb: /0.11.2/0.11.3
|
|
311
|
+
3. ~ lib/SimpleCSV.rb: /0.11.2/0.11.3/
|
|
291
312
|
|
|
292
313
|
0.11.2: ~ SimpleCSV#write_header, the header row is the column names, selected or all, written as a row.
|
|
293
314
|
|
|
@@ -295,14 +316,14 @@
|
|
|
295
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).
|
|
296
317
|
3. The library no longer calls Hash#to_csv anywhere; the require stays until Phase 4's scoping.
|
|
297
318
|
4. ~ test/SimpleCSV_test.rb: - the skip on the selected header example, and + the same under quote: :none. Run to 103, 24 skipped.
|
|
298
|
-
5. ~ lib/SimpleCSV.rb: /0.11.1/0.11.2
|
|
319
|
+
5. ~ lib/SimpleCSV.rb: /0.11.1/0.11.2/
|
|
299
320
|
|
|
300
321
|
0.11.1: ~ SimpleCSV#write, the file is emptied at the first write under r+, not at the end of every read.
|
|
301
322
|
|
|
302
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.
|
|
303
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.
|
|
304
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.
|
|
305
|
-
4. ~ lib/SimpleCSV.rb: /0.11.0/0.11.1
|
|
326
|
+
4. ~ lib/SimpleCSV.rb: /0.11.0/0.11.1/
|
|
306
327
|
|
|
307
328
|
0.11.0: + test/SimpleCSV_test.rb, and the header's Changes section retired to CHANGELOG.
|
|
308
329
|
|
|
@@ -438,7 +459,7 @@
|
|
|
438
459
|
1. ~ SimpleCSV#initialize, so as the default quoting is :none, not :double.
|
|
439
460
|
2. + SimpleCSV#read_header, which I'd mistakenly taken out in the recent cull... for use with SimpleCSV#read.
|
|
440
461
|
3. ~ SimpleCSV#read, calls read_header, so the first line isn't pulled in as data.
|
|
441
|
-
4. ~ SimpleCSV#initialize, /use_array/as_array
|
|
462
|
+
4. ~ SimpleCSV#initialize, /use_array/as_array/
|
|
442
463
|
5. ~ SimpleCSV#initialize, compressed a couple of the if statements, since they weren't complicated enough to be over 7 lines each.
|
|
443
464
|
6. ~ SimpleCSV#parse_row, logic was inverted for when @columns.blank? after a change in the logic for at 0.9.0!
|
|
444
465
|
7. ~ SimpleCSV#attributes, /columns/@columns.blank?/, and switched the logic order(!), since this is a little more robust and probably slightly faster too.
|
|
@@ -506,7 +527,7 @@
|
|
|
506
527
|
|
|
507
528
|
0.9.0
|
|
508
529
|
|
|
509
|
-
1. /CSVFile/SimpleCSV
|
|
530
|
+
1. /CSVFile/SimpleCSV/
|
|
510
531
|
2. Reset the Todo list and rolled in the Goals to that.
|
|
511
532
|
3. Moved the loader stuff (Array, Hash, String) in here.
|
|
512
533
|
4. More changes to interfaces to reflect the change in 0.8.0 to interface arguments.
|
|
@@ -543,7 +564,7 @@
|
|
|
543
564
|
2. ~ #read...
|
|
544
565
|
3. + #set_columns.
|
|
545
566
|
4. + #readrow.
|
|
546
|
-
5. /write_csv/write
|
|
567
|
+
5. /write_csv/write/
|
|
547
568
|
6. alias_method :write_csv, :write
|
|
548
569
|
7. + #each_csv.
|
|
549
570
|
8. + #csv_each.
|
|
@@ -560,7 +581,7 @@
|
|
|
560
581
|
4. + .first_row
|
|
561
582
|
5. + .attributes
|
|
562
583
|
6. + .columns
|
|
563
|
-
7. /#read_csv/#read
|
|
584
|
+
7. /#read_csv/#read/
|
|
564
585
|
8. ~ #do_read to use parse_row instead of parse_line.
|
|
565
586
|
9. + alias_method :parse_row, :read_row
|
|
566
587
|
10. ~ #columns=, fixed when taking a hash.
|
|
@@ -571,8 +592,8 @@
|
|
|
571
592
|
1. Removed all the debug stuff.
|
|
572
593
|
2. require'ing of String placed in Array.rb.
|
|
573
594
|
3. include Enumerable.
|
|
574
|
-
4. /line/row
|
|
575
|
-
5. /separator/row_separator
|
|
595
|
+
4. /line/row/
|
|
596
|
+
5. /separator/row_separator/
|
|
576
597
|
6. ~ #read_csv.
|
|
577
598
|
7. + #do_read.
|
|
578
599
|
8. + #read_header.
|
|
@@ -626,7 +647,7 @@
|
|
|
626
647
|
|
|
627
648
|
0.5.10
|
|
628
649
|
|
|
629
|
-
1. /read/read_csv
|
|
650
|
+
1. /read/read_csv/
|
|
630
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.
|
|
631
652
|
3. Added some aliases for read and write: read_csv and write_csv.
|
|
632
653
|
4. Removed self.class from expand_path in self.open.
|
|
@@ -963,10 +984,10 @@
|
|
|
963
984
|
|
|
964
985
|
0.2.0
|
|
965
986
|
|
|
966
|
-
1. /from_csv/parse
|
|
987
|
+
1. /from_csv/parse/
|
|
967
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.
|
|
968
989
|
3. Added < File to the CSVFile class definition.
|
|
969
|
-
4. /@file_handle/self
|
|
990
|
+
4. /@file_handle/self/
|
|
970
991
|
5. I decided to only use the term rows to designate parsed data and so is essentially restricted to @rows and related.
|
|
971
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.
|
|
972
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)
|
|
@@ -258,8 +368,9 @@ class DSV
|
|
|
258
368
|
end
|
|
259
369
|
end
|
|
260
370
|
|
|
261
|
-
# A row's values in position order
|
|
371
|
+
# A row's values in position order. Under a name at several positions the row holds an Array, whose values go back to those positions in turn; a single value under such a name goes at each. Where no name repeats, the values are taken as they are, sparing a check of every field.
|
|
262
372
|
def values_in_order(row)
|
|
373
|
+
return row.values_at(*attributes) if repeated_names.empty?
|
|
263
374
|
occurrence = ::Hash.new(0)
|
|
264
375
|
attributes.collect do |name|
|
|
265
376
|
value = row[name]
|
|
@@ -281,13 +392,13 @@ class DSV
|
|
|
281
392
|
when :none, :unquoted
|
|
282
393
|
split_on(row, @column_separator)
|
|
283
394
|
when :double, :double_quoted, :double_quotes
|
|
284
|
-
|
|
395
|
+
split_wholly_quoted(row)
|
|
285
396
|
else
|
|
286
397
|
split_quoted(row)
|
|
287
398
|
end
|
|
288
399
|
end
|
|
289
400
|
|
|
290
|
-
# 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.
|
|
291
402
|
def split_on(row, separator)
|
|
292
403
|
case separator
|
|
293
404
|
when ::Regexp; pieces_and_separators(row, separator).first
|
|
@@ -308,7 +419,7 @@ class DSV
|
|
|
308
419
|
[pieces << row[position..], separators]
|
|
309
420
|
end
|
|
310
421
|
|
|
311
|
-
#
|
|
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.
|
|
312
423
|
def split_quoted(row)
|
|
313
424
|
if @column_separator.is_a?(::Regexp)
|
|
314
425
|
reassemble_quoted_fields(*pieces_and_separators(row, @column_separator))
|
|
@@ -318,7 +429,7 @@ class DSV
|
|
|
318
429
|
end
|
|
319
430
|
|
|
320
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.
|
|
321
|
-
def
|
|
432
|
+
def split_wholly_quoted(row)
|
|
322
433
|
raise UnquotedField.new(:double) unless row.start_with?('"')
|
|
323
434
|
fields = split_on(row.delete_prefix('"'), quoted_separator)
|
|
324
435
|
return reassemble_wholly_quoted_fields(row) unless fields.last && trailing_quotes(fields.last, -1).odd?
|
|
@@ -424,13 +535,6 @@ class DSV
|
|
|
424
535
|
@source.read(start).to_s.scan(@row_separator).length + 1
|
|
425
536
|
end
|
|
426
537
|
|
|
427
|
-
def parse_row(raw_row, *selected_columns)
|
|
428
|
-
parse_fields(split_row(raw_row) || raise(UnterminatedQuote.new(1)), *selected_columns)
|
|
429
|
-
rescue UnexpectedRow => error
|
|
430
|
-
error.line = 1
|
|
431
|
-
raise
|
|
432
|
-
end
|
|
433
|
-
|
|
434
538
|
def parse_fields(fields, *selected_columns)
|
|
435
539
|
selected_columns.flatten!
|
|
436
540
|
return fields.values_at(*positions(selected_columns)) if @as_array && !selected_columns.empty?
|
|
@@ -449,7 +553,11 @@ class DSV
|
|
|
449
553
|
when Integer
|
|
450
554
|
fields.each{|column_value| parsed_row[i] = column_value unless !selected_columns.include?(i += 1)}
|
|
451
555
|
else
|
|
452
|
-
|
|
556
|
+
if repeated_names.empty?
|
|
557
|
+
fields.each{|column_value| parsed_row[attributes[i]] = column_value unless !selected_columns.include?(attributes[i += 1])}
|
|
558
|
+
else
|
|
559
|
+
fields.each{|column_value| store_field(parsed_row, attributes[i], column_value) unless !selected_columns.include?(attributes[i += 1])}
|
|
560
|
+
end
|
|
453
561
|
end
|
|
454
562
|
end
|
|
455
563
|
if @as_array
|
|
@@ -463,19 +571,10 @@ class DSV
|
|
|
463
571
|
end
|
|
464
572
|
end
|
|
465
573
|
|
|
466
|
-
# The positions of the columns selected, in column order, a repeated name giving each of its own.
|
|
467
|
-
def positions(selected_columns)
|
|
468
|
-
|
|
469
|
-
end
|
|
470
|
-
|
|
471
|
-
# 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.
|
|
472
|
-
def write(*selected_columns)
|
|
473
|
-
prepare_to_rewrite if @mode == 'r+'
|
|
474
|
-
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)
|
|
475
|
-
write_header(*selected_columns) if header_row? && attributes
|
|
476
|
-
each{|row| write_row(row, *selected_columns)}
|
|
574
|
+
# The positions of the columns selected, in column order, a repeated name giving each of its own.
|
|
575
|
+
def positions(selected_columns)
|
|
576
|
+
selected_columns.flat_map{|column| column.is_a?(::Integer) ? column : Array((columns || {})[column])}.sort
|
|
477
577
|
end
|
|
478
|
-
alias_method :write_csv, :write
|
|
479
578
|
|
|
480
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.
|
|
481
580
|
def prepare_to_rewrite
|
|
@@ -484,29 +583,6 @@ end
|
|
|
484
583
|
@source.truncate(0)
|
|
485
584
|
end
|
|
486
585
|
|
|
487
|
-
def write_header(*selected_columns)
|
|
488
|
-
selected_columns.flatten!
|
|
489
|
-
names = selected_columns.empty? ? attributes : selected_columns
|
|
490
|
-
write_values(names)
|
|
491
|
-
end
|
|
492
|
-
alias_method :write_csv_header, :write_header
|
|
493
|
-
|
|
494
|
-
# 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.
|
|
495
|
-
def write_row(row, *selected_columns)
|
|
496
|
-
selected_columns.flatten!
|
|
497
|
-
values = (
|
|
498
|
-
if !selected_columns.empty?
|
|
499
|
-
selected_columns.collect{|column| row[column]}
|
|
500
|
-
elsif attributes
|
|
501
|
-
values_in_order(row)
|
|
502
|
-
else
|
|
503
|
-
row.values
|
|
504
|
-
end
|
|
505
|
-
)
|
|
506
|
-
write_values(values)
|
|
507
|
-
end
|
|
508
|
-
alias_method :write_csv_row, :write_row
|
|
509
|
-
|
|
510
586
|
def write_values(values)
|
|
511
587
|
@render_row ||= row_renderer
|
|
512
588
|
@source.write(@render_row.call(values))
|
|
@@ -531,80 +607,6 @@ end
|
|
|
531
607
|
end
|
|
532
608
|
end
|
|
533
609
|
|
|
534
|
-
def each(*selected_columns)
|
|
535
|
-
return to_enum(:each, *selected_columns) unless block_given?
|
|
536
|
-
selected_columns = selection(selected_columns)
|
|
537
|
-
selected_columns.flatten!
|
|
538
|
-
if @rows[0]
|
|
539
|
-
if selected_columns.empty?
|
|
540
|
-
@rows.each{|row| yield row}
|
|
541
|
-
elsif @as_array
|
|
542
|
-
@rows.each{|row| yield row.values_at(*positions(selected_columns))}
|
|
543
|
-
else
|
|
544
|
-
@rows.each do |row|
|
|
545
|
-
yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
|
|
546
|
-
end
|
|
547
|
-
end
|
|
548
|
-
else
|
|
549
|
-
if selected_columns.empty? || @as_array
|
|
550
|
-
read_csv(selected_columns).each{|row| yield row}
|
|
551
|
-
else
|
|
552
|
-
read_csv(selected_columns).each do |row|
|
|
553
|
-
yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
|
|
554
|
-
end
|
|
555
|
-
end
|
|
556
|
-
end
|
|
557
|
-
end
|
|
558
|
-
alias_method :each_row, :each
|
|
559
|
-
|
|
560
|
-
def attributes
|
|
561
|
-
@attributes ||= (
|
|
562
|
-
if blank?(columns)
|
|
563
|
-
nil
|
|
564
|
-
else
|
|
565
|
-
a = []
|
|
566
|
-
columns.each do |k,v|
|
|
567
|
-
case v
|
|
568
|
-
when Array
|
|
569
|
-
v.each{|e| a << [k, e]}
|
|
570
|
-
else
|
|
571
|
-
a << [k, v]
|
|
572
|
-
end
|
|
573
|
-
end
|
|
574
|
-
a.sort{|a,b| a[1] <=> b[1]}.collect{|a| a[0]}
|
|
575
|
-
end
|
|
576
|
-
)
|
|
577
|
-
end
|
|
578
|
-
|
|
579
|
-
def attributes=(attributes)
|
|
580
|
-
@attributes = attributes
|
|
581
|
-
end
|
|
582
|
-
|
|
583
|
-
def header_row?
|
|
584
|
-
@header_row
|
|
585
|
-
end
|
|
586
|
-
|
|
587
|
-
def first_row
|
|
588
|
-
@source.rewind
|
|
589
|
-
return_value = @source.gets(@row_separator)
|
|
590
|
-
@source.rewind
|
|
591
|
-
return_value
|
|
592
|
-
end
|
|
593
|
-
alias_method :first_row?, :first_row
|
|
594
|
-
|
|
595
|
-
def to_a
|
|
596
|
-
read_csv unless @rows[0]
|
|
597
|
-
if @as_array
|
|
598
|
-
@rows
|
|
599
|
-
elsif blank?(@columns)
|
|
600
|
-
@rows.collect(&:values)
|
|
601
|
-
else
|
|
602
|
-
@rows.collect{|row| values_in_order(row)}
|
|
603
|
-
end
|
|
604
|
-
end
|
|
605
|
-
|
|
606
|
-
private
|
|
607
|
-
|
|
608
610
|
def quoted_row_renderer(quote, separator)
|
|
609
611
|
doubled_quote = quote * 2
|
|
610
612
|
->(values){values.collect{|value| quote + value.to_s.gsub(quote, doubled_quote) + quote}.join(separator) + @row_separator}
|
|
@@ -613,5 +615,4 @@ end
|
|
|
613
615
|
def unquoted_row_renderer(separator)
|
|
614
616
|
->(values){values.join(separator) + @row_separator}
|
|
615
617
|
end
|
|
616
|
-
|
|
617
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}
|