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 +4 -4
- data/CHANGELOG.md +3 -0
- data/CONTRIBUTORS.md +1 -0
- data/Rakefile +11 -0
- data/lib/smarter_csv/version.rb +1 -1
- data/lib/smarter_csv.rb +43 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c20e6e8f4281f99aa22e67c778e1e950ef7040a48f454c7d9751f2ccf44c093
|
4
|
+
data.tar.gz: 14ec931e58fce24c675bd76ad39f6046c52a0a4b07cc89814fa55c6a981ebe27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf3d642f523bf49d0867bc1768a6df247f3392390090c2b0fbfda5ac75f5f8f829eaac2ec14105936e4d317c7a1d1b865d74de1e60ce6405c6fd1f868bd703eb
|
7
|
+
data.tar.gz: 70522e31ca2ced36beef2a38509d1df01bad12af5bb56cb1404cfcbceffdfb4cbbbf8a8a8a535e3ea060d2d4c5b4c1049c8486d163d7de6b466dd83838aa2cf0
|
data/CHANGELOG.md
CHANGED
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"
|
data/lib/smarter_csv/version.rb
CHANGED
data/lib/smarter_csv.rb
CHANGED
@@ -374,24 +374,21 @@ module SmarterCSV
|
|
374
374
|
return false
|
375
375
|
end
|
376
376
|
|
377
|
-
#
|
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
|
-
|
380
|
-
n = Hash.new(0)
|
381
|
+
possible_delimiters = [',', "\t", ';', ':', '|']
|
381
382
|
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
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
|
-
|
392
|
-
raise SmarterCSV::NoColSepDetected if n.values.max == 0
|
389
|
+
raise SmarterCSV::NoColSepDetected if candidates.values.max == 0
|
393
390
|
|
394
|
-
|
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.
|
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:
|
11
|
+
date: 2023-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: awesome_print
|