dsv 0.14.0 → 0.14.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 52c5aba99e308ac5ec49ca71cd9ff802841b9cc33e67290b6947bbc5ec0c0956
4
- data.tar.gz: c12693bbcc29d81f5a1527b9e88a6f83194e84c75f42e6ed04f265a01d79ec21
3
+ metadata.gz: b69244ec05a6f3303e048cf5f4866b026c04497cce84c1281261223f46cae932
4
+ data.tar.gz: 1f2b028014b57beed86a853ec8a36c2b0a53b3db0b0e88fcabf77645b091f329
5
5
  SHA512:
6
- metadata.gz: f3f677737daf0fb1d2a12453142b2bda9e0caf21dd6baa1ef1ad286f8c9f4ac422a923663fa5ee6272e5bc4dd49d99c8f7813572fd56e0a0d6d80d97915fcd77
7
- data.tar.gz: ac3b055dde0446e5a1997170d875289a0767ecae0e4e8af52f63e7cb1626be22e79b2af52659fa169f69691f7fddf9b0eb54e86103134ca01344cc0558233534
6
+ metadata.gz: 9353eb0403013111353b18ec8b39c7f6c035e20b204f7c99287377f1429196b94cfa6c9fcad0dac4e8d8a1782c58ad3de65666936b95ed05a4951e6bf43a47ce
7
+ data.tar.gz: 1e1ee800d9651f883ef517cf9ac074a80e8a8731c98fec4cb05a06f65e27974b4af1ce426cf828d00926d369c88fd30f45f54c6e0e257d38ada1ff1abfabc72f
data/CHANGELOG CHANGED
@@ -2,7 +2,22 @@
2
2
 
3
3
  ## 20261126
4
4
 
5
- 0.14.0: The internals are private, and split_double_quoted() is renamed split_wholly_quoted().
5
+ 0.14.2: ~ DSV.new: A path or text no longer has its header read twice.
6
+
7
+ 1. ~ initialize(): A String source is opened by DSV::File.source or DSV::String.source, rather than taken from a DSV::File or DSV::String built and thrown away, whose own initialize had already read the header.
8
+ 2. + DSV::File.source, + DSV::String.source: Each opens its argument as an IO, and DSV::File#source and DSV::String#source call them.
9
+ 3. ~ test/dsv_test.rb: DSV.new on a path or on text is tested to construct no DSV::File or DSV::String.
10
+ 4. ~ DSV::VERSION: /0.14.1/0.14.2/
11
+
12
+ 0.14.1: ~ DSV#write: Appending (a, a+) no longer repeats the header.
13
+
14
+ 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.
15
+ 2. ~ test/dsv_test.rb: Appending under a and a+ is tested, to a file with a header and to an empty one.
16
+ 3. ~ README.md: Appending is described.
17
+ 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.
18
+ 5. ~ DSV::VERSION: /0.14.0/0.14.1/
19
+
20
+ 0.14.0:The internals are private, and split_double_quoted() is renamed split_wholly_quoted().
6
21
 
7
22
  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
23
  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/File.rb CHANGED
@@ -5,8 +5,14 @@ require_relative '../dsv'
5
5
 
6
6
  class DSV
7
7
  class File < DSV
8
+ class << self
9
+ def source(filename, options = {})
10
+ ::File.new(::File.expand_path(filename), DSV.normalised_mode(options[:mode]), options[:permissions])
11
+ end
12
+ end # class << self
13
+
8
14
  def source
9
- @source ||= ::File.new(filename, mode, permissions)
15
+ @source ||= DSV::File.source(filename, options)
10
16
  end
11
17
 
12
18
  def mode
data/lib/DSV/String.rb CHANGED
@@ -5,8 +5,15 @@ require_relative '../dsv'
5
5
 
6
6
  class DSV
7
7
  class String < DSV
8
+ class << self
9
+ # The options are taken, and unused, so that DSV#initialize calls this as it does DSV::File.source.
10
+ def source(string, options = {})
11
+ StringIO.new(string)
12
+ end
13
+ end # class << self
14
+
8
15
  def source
9
- @source ||= StringIO.new(@string)
16
+ @source ||= DSV::String.source(@string)
10
17
  end
11
18
 
12
19
  private
data/lib/DSV/VERSION.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # DSV::VERSION
3
3
 
4
4
  class DSV
5
- VERSION = '0.14.0'
5
+ VERSION = '0.14.2'
6
6
  end
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
@@ -331,14 +331,14 @@ class DSV
331
331
  private
332
332
 
333
333
  def initialize(source, *args)
334
+ options = DSV.extract_options(args)
334
335
  @source = (
335
336
  if source.is_a?(::String)
336
- DSV.source_type(source).new(source, *args).source
337
+ DSV.source_type(source).source(source, options)
337
338
  else
338
339
  source
339
340
  end
340
341
  )
341
- options = DSV.extract_options(args)
342
342
  @header_row = options[:header_row] || options[:headers] || options[:header] || false
343
343
  @mode = DSV.normalised_mode(options[:mode])
344
344
  @quote = options[:quote] || nil
@@ -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
@@ -365,6 +365,11 @@ describe DSV do
365
365
  _(DSV.new(StringIO.new(DATA), headers: true).read).must_equal KEYED
366
366
  end
367
367
 
368
+ it "opens a path or text itself, constructing no DSV::File or DSV::String" do
369
+ with_file{|path| _(constructions(DSV::File){DSV.new(path, headers: true).close}).must_equal 0}
370
+ _(constructions(DSV::String){DSV.new(DATA, headers: true)}).must_equal 0
371
+ end
372
+
368
373
  it "reads only the selected_columns: given at construction when a call selects none, a call's own selection winning" do
369
374
  csv = DSV.new(DATA, headers: true, selected_columns: ['a'])
370
375
  _(csv.read).must_equal [{'a' => '1'}, {'a' => '4'}]
@@ -840,6 +845,25 @@ describe DSV do
840
845
  end
841
846
  end
842
847
 
848
+ it "appends under a or a+ to a file with a header without writing it again, and writes it to an empty file" do
849
+ with_file do |path|
850
+ DSV.open(path, mode: 'a+', headers: true, quote: :none){|csv| csv.rows = [{'a' => '7', 'b' => '8', 'c' => '9'}]; csv.write}
851
+ _(File.read(path)).must_equal "a,b,c\n1,2,3\n4,5,6\n7,8,9\n"
852
+ end
853
+ with_file do |path|
854
+ DSV.open(path, mode: 'a', headers: true, columns: [:a, :b, :c], quote: :none){|csv| csv.rows = [{'a' => 7, 'b' => 8, 'c' => 9}]; csv.write}
855
+ _(File.read(path)).must_equal "a,b,c\n1,2,3\n4,5,6\n7,8,9\n"
856
+ end
857
+ with_file('') do |path|
858
+ DSV.open(path, mode: 'a', headers: true, columns: [:a, :b], quote: :none){|csv| csv.rows = [{'a' => 1, 'b' => 2}]; csv.write}
859
+ _(File.read(path)).must_equal "a,b\n1,2\n"
860
+ end
861
+ with_file('') do |path|
862
+ DSV.open(path, mode: 'a+', headers: true, quote: :none){|csv| csv.rows = [{'a' => 1, 'b' => 2}]; csv.write}
863
+ _(File.read(path)).must_equal "a,b\n1,2\n"
864
+ end
865
+ end
866
+
843
867
  it "rewrites a file in place under r+ when the rows are read, changed and written" do
844
868
  with_file do |path|
845
869
  DSV.open(path, mode: 'r+', headers: true, quote: :none){|csv| csv.rows = csv.read.map{|row| row.merge('a' => 'X')}; csv.write}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.14.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran