bmg 0.18.14 → 0.18.15

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
  SHA1:
3
- metadata.gz: 8541ab1e17726eb751772cc29a3d198f8bc021d7
4
- data.tar.gz: a4e07beb7b994d3ac6dda214e3a75822a1a3eb07
3
+ metadata.gz: 14260453d9c3f5d24b5ef6975072e452af309485
4
+ data.tar.gz: f6de19178d2b4404c9fbe248d42f9c596cee410e
5
5
  SHA512:
6
- metadata.gz: '063179c6a79505b6baaaf800f56a35dc7084b95ac4fa2c73acc92e809bb1d164d99bb3ce6120863d3b87886f8e21b3bc3579a1043e01dc2938ff6c278a4c08c3'
7
- data.tar.gz: 3fe57b85503565d547522f4a41ad608ca3839add2305e5108b212f269a4d76adb4d681da58cca01bea564c1ce24b70922215923f3df332d5a7d51dadc09a2ee8
6
+ metadata.gz: e53da5df4abb7095036dc275948a70e4af9f1945e2fd446244e765d7fead1258a9e564d9076baa3efc4ee29566810a5176c76d2c3c953de906a7ff36a3a86dff
7
+ data.tar.gz: a6f0cb03795e5b954563f774bc65c7879a78049928d06da935017a8dfcdb2bdd563c4ab08922cb95f774d68714ab108bb480d0722249ec5e6cc67fed5ed0064a
@@ -10,20 +10,18 @@ module Bmg
10
10
  }
11
11
 
12
12
  def initialize(type, path_or_io, options = {})
13
- @type = type
13
+ require 'csv'
14
+
14
15
  @path_or_io = path_or_io
15
- @options = DEFAULT_OPTIONS.merge(options)
16
- if @options[:smart] && !@path_or_io.is_a?(IO)
17
- @options[:col_sep] ||= infer_col_sep
18
- @options[:quote_char] ||= infer_quote_char
19
- end
16
+ @options = handle_options(options)
17
+ @type = handle_type(type)
20
18
  end
21
19
 
22
20
  def each
23
21
  return to_enum unless block_given?
24
- require 'csv'
25
- with_io do |io|
26
- ::CSV.new(io, **csv_options).each do |row|
22
+
23
+ with_csv do |csv|
24
+ csv.each do |row|
27
25
  yield tuple(row)
28
26
  end
29
27
  end
@@ -44,33 +42,70 @@ module Bmg
44
42
  row.to_hash.each_with_object({}){|(k,v),h| h[k.to_sym] = v }
45
43
  end
46
44
 
45
+ def handle_type(type)
46
+ return type if type.knows_attrlist?
47
+
48
+ type.with_attrlist(infer_attrlist)
49
+ end
50
+
51
+ def infer_attrlist
52
+ with_csv do |csv|
53
+ csv.each do |row|
54
+ return tuple(row).keys
55
+ end
56
+ end
57
+ end
58
+
59
+ def handle_options(options)
60
+ options = DEFAULT_OPTIONS.merge(options)
61
+ if options[:smart] && !@path_or_io.is_a?(IO)
62
+ options[:col_sep] ||= infer_col_sep
63
+ options[:quote_char] ||= infer_quote_char(options[:col_sep])
64
+ end
65
+ options
66
+ end
67
+
47
68
  def infer_col_sep
48
69
  sniff(text_portion, [",","\t",";"], ",")
49
70
  end
50
71
 
51
- def infer_quote_char
52
- sniff(text_portion, ["'","\""], "\"")
72
+ def infer_quote_char(col_sep)
73
+ sniff(text_portion, ['"',"'"], '"'){|quote|
74
+ /#{quote}#{col_sep}#{quote}|^#{quote}|#{quote}$/
75
+ }
53
76
  end
54
77
 
55
78
  def text_portion
56
- @text_portion ||= with_io{|io| io.readlines(10).join("\n") }
79
+ @text_portion ||= with_io{|io| io.readlines(50).join("\n") }
57
80
  end
58
81
 
59
82
  def with_io(&bl)
60
83
  case @path_or_io
61
84
  when IO, StringIO
85
+ @path_or_io.rewind if @path_or_io.respond_to?(:rewind)
62
86
  bl.call(@path_or_io)
63
87
  else
64
88
  File.open(@path_or_io, "r", &bl)
65
89
  end
66
90
  end
67
91
 
92
+ def with_csv(&bl)
93
+ with_io do |io|
94
+ yield ::CSV.new(io, **csv_options)
95
+ end
96
+ end
97
+
68
98
  # Finds the best candidate among `candidates` for a separator
69
99
  # found in `str`. If none is found, returns `default`.
70
- def sniff(str, candidates, default)
100
+ def sniff(str, candidates, default, &bl)
71
101
  snif = {}
72
102
  candidates.each {|delim|
73
- snif[delim] = str.count(delim)
103
+ counter = bl ? bl.call(delim) : delim
104
+ snif[delim] = if counter.is_a?(Regexp)
105
+ str.scan(counter).length
106
+ else
107
+ str.count(counter)
108
+ end
74
109
  }
75
110
  snif = snif.sort {|a,b| b[1] <=> a[1] }
76
111
  snif.size > 0 ? snif[0][0] : default
data/lib/bmg/version.rb CHANGED
@@ -2,7 +2,7 @@ module Bmg
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 18
5
- TINY = 14
5
+ TINY = 15
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bmg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.14
4
+ version: 0.18.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau