xsv 1.0.0 → 1.0.1

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: ef096c0bca09f5a58d67897e9884388a89883a058dabf43f1e223eec250a93d5
4
- data.tar.gz: 7d1b47f63b21096975aa8f31f5bfb9473c79badc898a7c4735e4a7260fbf6fc3
3
+ metadata.gz: 85395cd1f83157097328c94eb82db6ecbf4c3e18ae1241c56762b21d5de03927
4
+ data.tar.gz: 3141ce4b9bd703aed15cab002f6905636d3597a92492e7d7689077628b7618b9
5
5
  SHA512:
6
- metadata.gz: e851ff5345dbfa88594a5dd57749f78b8d6309bbede337d66d45ce66dc2c0eb29fcf2e123e5bc97bc456962fa1d21e5527f25214c792cdd486243cdba1e8ce5c
7
- data.tar.gz: 4ae56754f6328a420a9ed6c8a635936950e2b1c1f61a4dcd584dd9517036abc8942b2936b4803a09ad1e6bf4ea78f94776fe62554ca97d54a9c61a1698211340
6
+ metadata.gz: 91870ea588f4fbe857c7c8a75c0282a8a22dd91c552bb166cda59d6ecffb32694383dd764136f8412dcaffa12d177df15cd73d408a84d7553ed9d83074c5af46
7
+ data.tar.gz: e9c81c6c5601b290d13d8eea15a57c018c7c6cee064bd478026b82d279b28d29e7f30d0fe9703e46d16c9653697f71454714592fc9f05ac105836598b7cd4176
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Xsv Changelog
2
2
 
3
+ ## 1.0.1 2021-03-18
4
+
5
+ - Allow passing a block to Workbook.open
6
+ - `parse_headers!` returns self to allow chaining (thanks @senhalil)
7
+
3
8
  ## 1.0.0 2021-01-26
4
9
 
5
10
  - Xsv no longer depends on native extensions, thanks to a pure-Ruby XML parser
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Travis CI](https://img.shields.io/travis/martijn/xsv/master)](https://travis-ci.org/martijn/xsv)
4
4
  [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://rubydoc.info/github/martijn/xsv)
5
+ [![Gem Version](https://badge.fury.io/rb/xsv.svg)](https://badge.fury.io/rb/xsv)
5
6
 
6
7
  Xsv is a fast, lightweight, pure Ruby parser for Office Open XML spreadsheet files
7
8
  (commonly known as Excel or .xlsx files). It strives to be minimal in the
@@ -82,7 +83,8 @@ sheet[1] # => {"header1" => "value1", "header2" => "value2"}
82
83
  Be aware that hash mode will lead to unpredictable results if the worksheet
83
84
  has multiple columns with the same header.
84
85
 
85
- `Xsv::Workbook.open` accepts a filename, or an IO or String containing a workbook.
86
+ `Xsv::Workbook.open` accepts a filename, or an IO or String containing a workbook. Optionally, you can pass a block
87
+ which will be called with the workbook as parameter, like `File#open`.
86
88
 
87
89
  `Xsv::Sheet` implements `Enumerable` so you can call methods like `#first`,
88
90
  `#filter`/`#select`, and `#map` on it.
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Xsv
4
4
  class SaxParser
5
- ATTR_REGEX = /((\S+)="(.*?)")/m.freeze
5
+ ATTR_REGEX = /((\S+)="(.*?)")/m
6
6
 
7
7
  def parse(io)
8
8
  state = :look_start
@@ -34,11 +34,11 @@ module Xsv
34
34
 
35
35
  if respond_to?(:characters) && !chars.empty?
36
36
  if chars.index('&')
37
- chars.gsub!('&', %q{&})
38
- chars.gsub!(''', %q{'})
39
- chars.gsub!('>', %q{>})
40
- chars.gsub!('&lt;', %q{<})
41
- chars.gsub!('&quot;', %q{"})
37
+ chars.gsub!('&amp;', '&')
38
+ chars.gsub!('&apos;', "'")
39
+ chars.gsub!('&gt;', '>')
40
+ chars.gsub!('&lt;', '<')
41
+ chars.gsub!('&quot;', '"')
42
42
  end
43
43
  characters(chars)
44
44
  end
@@ -66,13 +66,11 @@ module Xsv
66
66
 
67
67
  if tag_name.start_with?('/')
68
68
  end_element(tag_name[1..-1]) if respond_to?(:end_element)
69
+ elsif args.nil?
70
+ start_element(tag_name, nil)
69
71
  else
70
- if args.nil?
71
- start_element(tag_name, nil)
72
- else
73
- start_element(tag_name, args.scan(ATTR_REGEX).each_with_object({}) { |m, h| h[m[1].to_sym] = m[2] })
74
- end_element(tag_name) if args.end_with?('/') && respond_to?(:end_element)
75
- end
72
+ start_element(tag_name, args.scan(ATTR_REGEX).each_with_object({}) { |m, h| h[m[1].to_sym] = m[2] })
73
+ end_element(tag_name) if args.end_with?('/') && respond_to?(:end_element)
76
74
  end
77
75
 
78
76
  state = :look_start
data/lib/xsv/sheet.rb CHANGED
@@ -80,12 +80,12 @@ module Xsv
80
80
 
81
81
  # Load headers in the top row of the worksheet. After parsing of headers
82
82
  # all methods return hashes instead of arrays
83
- # @return [true]
83
+ # @return [self]
84
84
  def parse_headers!
85
85
  @headers = parse_headers
86
86
  @mode = :hash
87
87
 
88
- true
88
+ self
89
89
  end
90
90
 
91
91
  # Return the headers of the sheet as an array
@@ -100,9 +100,10 @@ module Xsv
100
100
  private
101
101
 
102
102
  def parse_headers
103
- if @mode == :array
103
+ case @mode
104
+ when :array
104
105
  first
105
- elsif @mode == :hash
106
+ when :hash
106
107
  @mode = :array
107
108
  headers.tap { @mode = :hash }
108
109
  end || []
@@ -13,7 +13,7 @@ module Xsv
13
13
 
14
14
  handler = new(workbook.trim_empty_rows) do |row, col|
15
15
  rows = row
16
- cols = col == 0 ? 0 : col + 1
16
+ cols = col.zero? ? 0 : col + 1
17
17
 
18
18
  return rows, cols
19
19
  end
@@ -34,9 +34,10 @@ module Xsv
34
34
  end
35
35
 
36
36
  def end_element(name)
37
- if name == 'styleSheet'
37
+ case name
38
+ when 'styleSheet'
38
39
  @block.call(@xfs, @numFmts)
39
- elsif name == 'cellXfs'
40
+ when 'cellXfs'
40
41
  @state = nil
41
42
  end
42
43
  end
data/lib/xsv/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Xsv
4
- VERSION = '1.0.0'
4
+ VERSION = '1.0.1'
5
5
  end
data/lib/xsv/workbook.rb CHANGED
@@ -22,6 +22,16 @@ module Xsv
22
22
  else # must be a filename
23
23
  new(Zip::File.open(data), **kws)
24
24
  end
25
+
26
+ if block_given?
27
+ begin
28
+ yield(@workbook)
29
+ ensure
30
+ @workbook.close
31
+ end
32
+ else
33
+ @workbook
34
+ end
25
35
  end
26
36
 
27
37
  # Open a workbook from an instance of {Zip::File}. Generally it's recommended
@@ -52,6 +62,7 @@ module Xsv
52
62
  # @return [true]
53
63
  def close
54
64
  @zip.close
65
+ @zip = nil
55
66
  @sheets = nil
56
67
  @xfs = nil
57
68
  @numFmts = nil
@@ -78,7 +89,7 @@ module Xsv
78
89
  stream = handle.get_input_stream
79
90
  SharedStringsParser.parse(stream)
80
91
  ensure
81
- stream.close if stream
92
+ stream&.close
82
93
  end
83
94
 
84
95
  def fetch_styles
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xsv
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martijn Storck
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-26 00:00:00.000000000 Z
11
+ date: 2021-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip