censive 0.16 → 0.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/censive.gemspec +1 -3
  3. data/lib/censive.rb +33 -26
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7abd1490426ab80b6b1cc3c7b28a590eb22c5dd565405662dd82bd1c427b013b
4
- data.tar.gz: adb90312a493b92ccb36e7354153a30f530f33d2b205fd6ebbea49ca1e80efe5
3
+ metadata.gz: d3e3d32cc23e58416e6d9c2f962be016beff4f1555af801e969032162fcb9f95
4
+ data.tar.gz: 405c48fba04cbf797354d0732a6ccc9238a5d198469a6d927fc37de86da5eaa7
5
5
  SHA512:
6
- metadata.gz: f603db91e71cce8d72322d9a248f81f625238aa2a7d5107a6849d18ecdb00cf94865f2ad716afde1df79e4d0dd126765968822e3e9d53f3bea09886f2ae08d89
7
- data.tar.gz: b5fc1fa9b2a309677091334d50200ada240ea2490d23d427cc32774a828a672ce1be1c22e156d21a7ef0ebdfc604f7b003386f46d4bc7fb2b6fdb50dc6d2a857
6
+ metadata.gz: 5afd52cdd519854a78977d778c05f0f9bad02bad177523e79c4f3fc96e4291178a2186f021d8a40d1be8ec16d1bc659a3a18a49721994d4c7326a90006362398
7
+ data.tar.gz: 378b0f4a3e289a0a1244d0a69ade975d15e6fae99b000d00d507e8ebd1ae0d29605b43bbb08e25b18f0306d3e54eb327f75f94e390de0fe3cb02da2fafe7185b
data/censive.gemspec CHANGED
@@ -1,8 +1,6 @@
1
- # encoding: utf-8
2
-
3
1
  Gem::Specification.new do |s|
4
2
  s.name = "censive"
5
- s.version = "0.16"
3
+ s.version = "0.18"
6
4
  s.author = "Steve Shreeve"
7
5
  s.email = "steve.shreeve@gmail.com"
8
6
  s.summary =
data/lib/censive.rb CHANGED
@@ -22,6 +22,9 @@
22
22
  require "strscan"
23
23
 
24
24
  class Censive < StringScanner
25
+ def self.parse(...)
26
+ new(...).parse
27
+ end
25
28
 
26
29
  def self.writer(obj=nil, **opts, &code)
27
30
  case obj
@@ -31,33 +34,32 @@ class Censive < StringScanner
31
34
  end
32
35
  end
33
36
 
34
- def initialize(str=nil,
35
- drop: false , # drop trailing empty fields?
36
- excel: false , # literals ="01" formulas =A1 + B2 http://bit.ly/3Y7jIvc
37
- mode: :compact, # export mode: compact or full
38
- out: $stdout , # output stream, needs to respond to <<
39
- quote: '"' , # quote character
40
- relax: false , # relax quote parsing so ,"Fo"o, => ,"Fo""o",
41
- rowsep: "\n" , # row separator for export
42
- sep: "," , # column separator character
43
- strip: false , # strip fields when reading
37
+ def initialize(str="",
38
+ drop: false , # drop trailing empty fields?
39
+ encoding: "utf-8" , # character encoding
40
+ excel: false , # literals ="01" formulas =A1 + B2 http://bit.ly/3Y7jIvc
41
+ mode: :compact, # export mode: compact or full
42
+ out: $stdout , # output stream, needs to respond to <<
43
+ quote: '"' , # quote character
44
+ relax: false , # relax quote parsing so ,"Fo"o, => ,"Fo""o",
45
+ rowsep: "\n" , # row separator for export
46
+ sep: "," , # column separator character
47
+ strip: false , # strip fields when reading
44
48
  **opts # grab bag
45
49
  )
46
- super(str || "")
47
- reset
48
-
49
50
  # options
50
- @drop = drop
51
- @excel = excel
52
- @mode = mode
53
- @out = out
54
- @quote = quote
55
- @relax = relax
56
- @rowsep = rowsep
57
- @sep = sep
58
- @strip = strip
59
-
60
- # determined
51
+ @drop = drop
52
+ @encoding = encoding
53
+ @excel = excel
54
+ @mode = mode
55
+ @out = out
56
+ @quote = quote
57
+ @relax = relax
58
+ @rowsep = rowsep
59
+ @sep = sep
60
+ @strip = strip
61
+
62
+ # definitions
61
63
  @cr = "\r"
62
64
  @lf = "\n"
63
65
  @es = ""
@@ -65,6 +67,13 @@ class Censive < StringScanner
65
67
  @esc = (@quote * 2)
66
68
  @eol = /#{@cr}#{@lf}?|#{@lf}|\z/o # end of line
67
69
  @eoc = /(?=#{"\\" + @sep}|#{@cr}|#{@lf}|\z)/o # end of cell
70
+
71
+ # data source
72
+ if str.size < 100 && File.readable?(str)
73
+ str = File.open(str, "r:#{encoding}").read
74
+ end
75
+ super(str)
76
+ reset
68
77
  end
69
78
 
70
79
  def reset(str=nil)
@@ -199,8 +208,6 @@ if __FILE__ == $0
199
208
  end
200
209
 
201
210
  __END__
202
- 123,"CHO, JOELLE "JOJO"",456
203
-
204
211
  Name,Age,Shoe
205
212
  Alice,27,5
206
213
  Bob,33,10 1/2
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: censive
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.16'
4
+ version: '0.18'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve