censive 0.27 → 0.29

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/censive.rb +12 -47
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc8277614e4b254907b5d127eddae62757c4522785a9564ace4207e0bda1c1f6
4
- data.tar.gz: 47049fc05225b53909c827b359d2201d9e0b915a21cd604625cb1cd3d2f1b1d5
3
+ metadata.gz: 7ab03fca630d441eb0d600137017738fe47949b1b333ddb92d82bd690b9015ef
4
+ data.tar.gz: e864e8e08e27abd92852a6cf28f1a54e09454c421023e004a6222f428a874b81
5
5
  SHA512:
6
- metadata.gz: d904a4b70401c772e14dac76ebd8a62f88e5d6a3efd49916733eea049d613c29d3f23703a458893fd86391c668a713baadb242bee83f3909d7e18a609bd1081a
7
- data.tar.gz: 11ea3270797dd9a9f4f994599d9324c0b23955fb8c86a01cf56378a07d94818cf922bf00f556f2100da27660412b29279b7222834dcc3ff59037fda0db920443
6
+ metadata.gz: c412bd6870fb5e577436d055cea945905ebc3ab2f74df43ca678eb5bc0c26c9e1d7a4ecb43f10f8896d5735799f64fce2d3f33ee347e454b6840b4ce054d09dc
7
+ data.tar.gz: 968a53df8658a96b6f2c74d9e3dd868320028630d7b92c3e7068e3ef52894181cf76283e5516c10cb0df3f711e99896513cb5455db490bff5ee4dca5ac7f33f4
data/lib/censive.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # censive - A quick and lightweight CSV handling library for Ruby
5
5
  #
6
6
  # Author: Steve Shreeve (steve.shreeve@gmail.com)
7
- # Date: Mar 20, 2023
7
+ # Date: Mar 22, 2023
8
8
  #
9
9
  # https://crystal-lang.org/api/1.7.2/CSV.html (Crystal's CSV library)
10
10
  # https://github.com/ruby/strscan/blob/master/ext/strscan/strscan.c
@@ -28,7 +28,7 @@
28
28
  require "strscan"
29
29
 
30
30
  class Censive < StringScanner
31
- VERSION="0.27"
31
+ VERSION="0.29"
32
32
 
33
33
  attr :encoding, :out, :rows
34
34
 
@@ -96,8 +96,8 @@ class Censive < StringScanner
96
96
  xsep = Regexp.escape(@sep) # may need to be escaped
97
97
  @eoc = /(?=#{"\\" + xsep}|#{@cr}|#{@lf}|\z)/o # end of cell
98
98
  @eol = /#{@cr}#{@lf}?|#{@lf}/o # end of line
99
- @escapes = /(#{@quote})|#{"\\"+xsep}|#{@cr}|#{@lf}/o
100
- @quotable = /#{"\\"+xsep}|#{@cr}|#{@lf}/o
99
+ @escapes = /(#{@quote})|#{xsep}|#{@cr}|#{@lf}/o
100
+ @quotable = /#{xsep}|#{@cr}|#{@lf}/o
101
101
  @quotes = /#{@quote}/o
102
102
  @seps = /#{xsep}+/o
103
103
  @quoted = @excel ? /(?:=)?#{@quote}/o : @quote
@@ -185,10 +185,14 @@ class Censive < StringScanner
185
185
  @rows.each {|row| yield row }
186
186
  end
187
187
 
188
- def to_csv(**opts)
189
- dest = opts.empty? ? self : self.class.writer(**opts)
190
- each {|row| dest << row }
191
- dest.out
188
+ def to_csv(*args, **opts, &code)
189
+ if args.empty? && opts.empty?
190
+ block_given? ? each(&code) : each {|row| @out << row }
191
+ elsif block_given?
192
+ Censive.writer(*args, **opts, &code)
193
+ else
194
+ Censive.writer(*args, **opts) {|csv| each {|row| csv << row }}
195
+ end
192
196
  end
193
197
 
194
198
  # ==[ Helpers ]==
@@ -253,42 +257,3 @@ class Censive < StringScanner
253
257
  abort "\n#{File.basename($0)}: #{msg} at character #{pos} near '#{string[pos-4,7]}'"
254
258
  end
255
259
  end
256
-
257
- if __FILE__ == $0
258
- str = DATA.gets("\n\n").chomp
259
- # str = File.read(ARGV.first || "lc-2023.csv")
260
- # str = File.open("KEN_ALL.CSV", "r:cp932").read
261
-
262
- # require "stringio"
263
- # csv = Censive.new(str, excel: true, relax: true)
264
- # out = "" # StringIO.new
265
- # csv.export(out: out) # (excel: true) # sep: "|")
266
- # puts out # .string
267
-
268
- # csv = Censive.new(str, excel: true, relax: true, out: "")
269
- # out = csv.export
270
- # puts out.out
271
-
272
- puts Censive.parse(str, excel: true, relax: true).to_csv
273
- end
274
-
275
- __END__
276
- "AAA "BBB",CCC,"DDD"
277
-
278
- "CHUI, LOK HANG "BENNY",224325325610,="001453","Hemoglobin A1c",=""
279
- "Don",="007",10,"Ed"
280
- Name,Age,,,Shoe,,,
281
- "Alice",27,5
282
- Bob,33,10 1/2
283
- Charlie or "Chuck",=B2 + B3,9
284
- Subtotal,=sum(B2:B5),="01234"
285
- A,B,C,D
286
- A,B,"C",D
287
- A,B,C",D
288
- A,B,"C",D
289
- 08/27/2022,="73859443",="04043260",,"Crossover @ Mathilda","CHO, JOELLE "JOJO"",08/19/2022
290
- 123,
291
- 123,"CHO, JOELLE "JOJO"",456
292
- 123,"CHO, JOELLE ""JOJO""",456
293
- =,=x,x=,="x",="","","=",123,0123,="123",="0123"
294
- ,=x,x=,x,,,,,,=,,123,="0123",123,,="0123"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: censive
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.27'
4
+ version: '0.29'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-20 00:00:00.000000000 Z
11
+ date: 2023-03-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A quick and lightweight CSV handling library for Ruby
14
14
  email: steve.shreeve@gmail.com
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  - !ruby/object:Gem::Version
48
48
  version: '0'
49
49
  requirements: []
50
- rubygems_version: 3.4.8
50
+ rubygems_version: 3.4.9
51
51
  signing_key:
52
52
  specification_version: 4
53
53
  summary: A quick and lightweight CSV handling library for Ruby