dsv 0.14.0 → 0.14.1
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 +9 -1
- data/README.md +9 -0
- data/lib/DSV/VERSION.rb +1 -1
- data/lib/dsv.rb +7 -1
- data/test/dsv_test.rb +19 -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: 4160107dbe47d742cbeea0e220bca705f459c5d9d7e54e385b76a29aef2e3385
|
|
4
|
+
data.tar.gz: de2d7469e42cf0e1c5c9f5adacb40b7722d97c1dde1d10804dc2a6eb55176467
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6d2f8c730725c1c1b8c057c024b0263c6858c03b5f38cc21b780cdd067fbfd36759c03b078ef132cd6cae1c74022e72aecdbabd8f93c5144e3e853d637ae6ae2
|
|
7
|
+
data.tar.gz: 617e1ce3bada65c9b89fd0a78e9e35ce1f19263f5c9e3f1136b5c808e8b9ed6dfd66bab14536d0d6e37b6a98dcd3f6c5a0930949e0f2e548e22c4e5f2115a706
|
data/CHANGELOG
CHANGED
|
@@ -2,7 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
## 20261126
|
|
4
4
|
|
|
5
|
-
0.14.
|
|
5
|
+
0.14.1: ~ DSV#write: Appending (a, a+) no longer repeats the header.
|
|
6
|
+
|
|
7
|
+
1. ~ write(), + writes_header?(): Under a or a+, the header is written only to an empty file. Every append had written it again, partway down a file which already had one.
|
|
8
|
+
2. ~ test/dsv_test.rb: Appending under a and a+ is tested, to a file with a header and to an empty one.
|
|
9
|
+
3. ~ README.md: Appending is described.
|
|
10
|
+
4. ~ PROPOSED.md: Writing asks two more questions: whether a reads the file's header separately, so that rows in another column order raise, and whether a row separator is written first where the file doesn't end in one.
|
|
11
|
+
5. ~ DSV::VERSION: /0.14.0/0.14.1/
|
|
12
|
+
|
|
13
|
+
0.14.0:The internals are private, and split_double_quoted() is renamed split_wholly_quoted().
|
|
6
14
|
|
|
7
15
|
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
16
|
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.
|
data/README.md
CHANGED
|
@@ -127,6 +127,15 @@ DSV.open('people.csv', mode: 'r+', headers: true) do |dsv|
|
|
|
127
127
|
end
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
+
Under `a` or `a+`, rows are added at the end, and a header is written only to an empty file. `a+` reads the header, so rows keyed by name go in the file's column order; `a` can't read it, so they go in the order `columns:` gives:
|
|
131
|
+
|
|
132
|
+
```ruby
|
|
133
|
+
DSV.open('people.csv', mode: 'a+', headers: true) do |dsv|
|
|
134
|
+
dsv.rows = [{'name' => 'Grace', 'born' => '1906'}]
|
|
135
|
+
dsv.write
|
|
136
|
+
end
|
|
137
|
+
```
|
|
138
|
+
|
|
130
139
|
### Repeated header names
|
|
131
140
|
|
|
132
141
|
Where the header repeats a name, `columns` maps the name to every position and the row holds the values under that name as an Array, in position order:
|
data/lib/DSV/VERSION.rb
CHANGED
data/lib/dsv.rb
CHANGED
|
@@ -228,7 +228,7 @@ class DSV
|
|
|
228
228
|
def write(*selected_columns)
|
|
229
229
|
prepare_to_rewrite if @mode == 'r+'
|
|
230
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
|
|
231
|
+
write_header(*selected_columns) if header_row? && attributes && writes_header?
|
|
232
232
|
each{|row| write_row(row, *selected_columns)}
|
|
233
233
|
end
|
|
234
234
|
alias_method :write_csv, :write
|
|
@@ -583,6 +583,12 @@ class DSV
|
|
|
583
583
|
@source.truncate(0)
|
|
584
584
|
end
|
|
585
585
|
|
|
586
|
+
# Under a or a+, a header is written only to an empty file, since a file with anything in it already has one. A source with no size, such as a pipe, always gets one.
|
|
587
|
+
def writes_header?
|
|
588
|
+
return true unless ['a', 'a+'].include?(@mode) && @source.respond_to?(:size)
|
|
589
|
+
@source.size.zero?
|
|
590
|
+
end
|
|
591
|
+
|
|
586
592
|
def write_values(values)
|
|
587
593
|
@render_row ||= row_renderer
|
|
588
594
|
@source.write(@render_row.call(values))
|
data/test/dsv_test.rb
CHANGED
|
@@ -840,6 +840,25 @@ describe DSV do
|
|
|
840
840
|
end
|
|
841
841
|
end
|
|
842
842
|
|
|
843
|
+
it "appends under a or a+ to a file with a header without writing it again, and writes it to an empty file" do
|
|
844
|
+
with_file do |path|
|
|
845
|
+
DSV.open(path, mode: 'a+', headers: true, quote: :none){|csv| csv.rows = [{'a' => '7', 'b' => '8', 'c' => '9'}]; csv.write}
|
|
846
|
+
_(File.read(path)).must_equal "a,b,c\n1,2,3\n4,5,6\n7,8,9\n"
|
|
847
|
+
end
|
|
848
|
+
with_file do |path|
|
|
849
|
+
DSV.open(path, mode: 'a', headers: true, columns: [:a, :b, :c], quote: :none){|csv| csv.rows = [{'a' => 7, 'b' => 8, 'c' => 9}]; csv.write}
|
|
850
|
+
_(File.read(path)).must_equal "a,b,c\n1,2,3\n4,5,6\n7,8,9\n"
|
|
851
|
+
end
|
|
852
|
+
with_file('') do |path|
|
|
853
|
+
DSV.open(path, mode: 'a', headers: true, columns: [:a, :b], quote: :none){|csv| csv.rows = [{'a' => 1, 'b' => 2}]; csv.write}
|
|
854
|
+
_(File.read(path)).must_equal "a,b\n1,2\n"
|
|
855
|
+
end
|
|
856
|
+
with_file('') do |path|
|
|
857
|
+
DSV.open(path, mode: 'a+', headers: true, quote: :none){|csv| csv.rows = [{'a' => 1, 'b' => 2}]; csv.write}
|
|
858
|
+
_(File.read(path)).must_equal "a,b\n1,2\n"
|
|
859
|
+
end
|
|
860
|
+
end
|
|
861
|
+
|
|
843
862
|
it "rewrites a file in place under r+ when the rows are read, changed and written" do
|
|
844
863
|
with_file do |path|
|
|
845
864
|
DSV.open(path, mode: 'r+', headers: true, quote: :none){|csv| csv.rows = csv.read.map{|row| row.merge('a' => 'X')}; csv.write}
|