smarter_csv 1.7.3 → 1.7.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d4046758f38c21262fdec6bc7e13e3a7811c7aee3944d92e0cc36a2a1cfb032a
4
- data.tar.gz: 9d111e2f36171ca488034f3af73fc71c7c9f6fde73986d277aeaf1560a066fa2
3
+ metadata.gz: 3c20e6e8f4281f99aa22e67c778e1e950ef7040a48f454c7d9751f2ccf44c093
4
+ data.tar.gz: 14ec931e58fce24c675bd76ad39f6046c52a0a4b07cc89814fa55c6a981ebe27
5
5
  SHA512:
6
- metadata.gz: c46c5c45dd3fafe66735b2b17b0679c5aaff27b3670140d97bc19e1c825ad91310fa2cf55a12a5c7b0c31ef82fe9cc12a2c4bda0a78b218d80ad5816c01c0d9f
7
- data.tar.gz: ba03acd95955f8afeb8e96f16c7cfa2e1605dbaf6fddb7008930294aab83196aed21f57605efb3553799381c1c4811528eee2db221efa50dc82f58bcf9135842
6
+ metadata.gz: cf3d642f523bf49d0867bc1768a6df247f3392390090c2b0fbfda5ac75f5f8f829eaac2ec14105936e4d317c7a1d1b865d74de1e60ce6405c6fd1f868bd703eb
7
+ data.tar.gz: 70522e31ca2ced36beef2a38509d1df01bad12af5bb56cb1404cfcbceffdfb4cbbbf8a8a8a535e3ea060d2d4c5b4c1049c8486d163d7de6b466dd83838aa2cf0
data/CHANGELOG.md CHANGED
@@ -1,6 +1,9 @@
1
1
 
2
2
  # SmarterCSV 1.x Change Log
3
3
 
4
+ ## 1.7.4 (2022-01-13)
5
+ * improved guessing of the column separator, thanks to Alessandro Fazzi
6
+
4
7
  ## 1.7.3 (2022-12-05)
5
8
  * new option :silence_missing_keys; if set to true, it ignores missing keys in `key_mapping`
6
9
 
data/CONTRIBUTORS.md CHANGED
@@ -49,3 +49,4 @@ A Big Thank you to everyone who filed issues, sent comments, and who contributed
49
49
  * [Nicolas Rodriguez](https://github.com/n-rodriguez)
50
50
  * [Hirotaka Mizutani ](https://github.com/hirotaka)
51
51
  * [Rahul Chaudhary](https://github.com/rahulch95)
52
+ * [Alessandro Fazzi](https://github.com/pioneerskies)
data/Rakefile CHANGED
@@ -3,6 +3,17 @@
3
3
  require "bundler/gem_tasks"
4
4
  require 'rspec/core/rake_task'
5
5
 
6
+
7
+ # temp fix for NoMethodError: undefined method `last_comment'
8
+ # remove when fixed in Rake 11.x and higher
9
+ module TempFixForRakeLastComment
10
+ def last_comment
11
+ last_description
12
+ end
13
+ end
14
+ Rake::Application.send :include, TempFixForRakeLastComment
15
+ ### end of tempfix
16
+
6
17
  RSpec::Core::RakeTask.new(:spec)
7
18
 
8
19
  require "rubocop/rake_task"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SmarterCSV
4
- VERSION = "1.7.3"
4
+ VERSION = "1.7.4"
5
5
  end
data/lib/smarter_csv.rb CHANGED
@@ -374,24 +374,21 @@ module SmarterCSV
374
374
  return false
375
375
  end
376
376
 
377
- # raise exception if none is found
377
+ # If file has headers, then guesses column separator from headers.
378
+ # Otherwise guesses column separator from contents.
379
+ # Raises exception if none is found.
378
380
  def guess_column_separator(filehandle, options)
379
- del = [',', "\t", ';', ':', '|']
380
- n = Hash.new(0)
381
+ possible_delimiters = [',', "\t", ';', ':', '|']
381
382
 
382
- 5.times do
383
- line = filehandle.readline(options[:row_sep])
384
- del.each do |d|
385
- n[d] += line.scan(d).count
386
- end
387
- rescue EOFError # short files
388
- break
389
- end
383
+ candidates = if options.fetch(:headers_in_file)
384
+ candidated_column_separators_from_headers(filehandle, options, possible_delimiters)
385
+ else
386
+ candidated_column_separators_from_contents(filehandle, options, possible_delimiters)
387
+ end
390
388
 
391
- filehandle.rewind
392
- raise SmarterCSV::NoColSepDetected if n.values.max == 0
389
+ raise SmarterCSV::NoColSepDetected if candidates.values.max == 0
393
390
 
394
- col_sep = n.key(n.values.max)
391
+ candidates.key(candidates.values.max)
395
392
  end
396
393
 
397
394
  # limitation: this currently reads the whole file in before making a decision
@@ -525,5 +522,37 @@ module SmarterCSV
525
522
  end
526
523
  result
527
524
  end
525
+
526
+ private
527
+
528
+ def candidated_column_separators_from_headers(filehandle, options, delimiters)
529
+ candidates = Hash.new(0)
530
+ line = filehandle.readline(options[:row_sep])
531
+
532
+ delimiters.each do |d|
533
+ candidates[d] += line.scan(d).count
534
+ end
535
+
536
+ filehandle.rewind
537
+
538
+ candidates
539
+ end
540
+
541
+ def candidated_column_separators_from_contents(filehandle, options, delimiters)
542
+ candidates = Hash.new(0)
543
+
544
+ 5.times do
545
+ line = filehandle.readline(options[:row_sep])
546
+ delimiters.each do |d|
547
+ candidates[d] += line.scan(d).count
548
+ end
549
+ rescue EOFError # short files
550
+ break
551
+ end
552
+
553
+ filehandle.rewind
554
+
555
+ candidates
556
+ end
528
557
  end
529
558
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smarter_csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.3
4
+ version: 1.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tilo Sloboda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-09 00:00:00.000000000 Z
11
+ date: 2023-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print