excel-esv 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/ci.yml +32 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +5 -0
- data/CHANGELOG.md +15 -3
- data/Gemfile +10 -2
- data/README.md +27 -1
- data/Rakefile +1 -1
- data/excel-esv.gemspec +8 -11
- data/lib/esv/version.rb +1 -1
- data/lib/esv.rb +65 -14
- data/spec/esv_spec.rb +189 -95
- data/spec/spec_helper.rb +2 -26
- metadata +11 -42
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: bb907e9f6b481001f01561df298d6052f3efb37f4ae3f57191b2bb807699203e
|
|
4
|
+
data.tar.gz: ea18830fa510acd5fe6d204714fbfc75114c0e167d08bd3fa969b61406893bf2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 745103dd2cbf9eda28e2d2947f4785b6a3f95372be9c3044265029cb4ac2e01c16d943923cb6e3f677387b2dfc6b5cbaba6bfddab001c0e775c97a92e967870b
|
|
7
|
+
data.tar.gz: e22bb76f26d291021847496fd679f6f982c4ecd14f0e9978674f08607f90d848b9539d5931d0a1ae038af8c22a8254814e69198af1802f755960e2fc0394ece4
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: Ruby CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ master ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ master ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
ruby-versions:
|
|
11
|
+
uses: ruby/actions/.github/workflows/ruby_versions.yml@master
|
|
12
|
+
with:
|
|
13
|
+
min_version: 3.2
|
|
14
|
+
engine: cruby
|
|
15
|
+
|
|
16
|
+
test:
|
|
17
|
+
needs: ruby-versions
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
|
|
20
|
+
strategy:
|
|
21
|
+
matrix:
|
|
22
|
+
ruby-version: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
|
|
23
|
+
|
|
24
|
+
steps:
|
|
25
|
+
- uses: actions/checkout@v5
|
|
26
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
|
27
|
+
uses: ruby/setup-ruby@v1
|
|
28
|
+
with:
|
|
29
|
+
ruby-version: ${{ matrix.ruby-version }}
|
|
30
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
|
31
|
+
- name: Run tests
|
|
32
|
+
run: bundle exec rake
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 3.1.0 (Nov 12, 2025)
|
|
4
|
+
|
|
5
|
+
* Support specifying worksheet index when there are multiple: `ESV.parse(data, worksheet_index: 0)`.
|
|
6
|
+
|
|
7
|
+
## 3.0.1 (Nov 19, 2021)
|
|
8
|
+
|
|
9
|
+
* Add `ESV.parse(data, header_converters:)` [#9]
|
|
10
|
+
|
|
11
|
+
[#9]: https://github.com/barsoom/excel-esv/pull/9
|
|
12
|
+
|
|
13
|
+
## 3.0.0 (Nov 20, 2018)
|
|
2
14
|
|
|
3
15
|
* Returns the last value of any formula cells instead of returning a `Spreadsheet::Formula`.
|
|
4
16
|
* Returns the URL of any link cells instead of returning a `Spreadsheet::Link`.
|
|
5
17
|
|
|
6
|
-
|
|
18
|
+
## 2.0.0 (Nov 19, 2018)
|
|
7
19
|
|
|
8
20
|
* `parse` now returns an actual nested `Array`, not array-like `Spreadsheet::Row` records.
|
|
9
21
|
|
|
10
|
-
|
|
22
|
+
## 1.0.0 (Dec 8, 2017)
|
|
11
23
|
|
|
12
24
|
* `send_excel` now supports a `filename:` argument, e.g. `send_excel(data, filename: "salaries.xls")`.
|
data/Gemfile
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
source
|
|
1
|
+
source "https://rubygems.org"
|
|
2
2
|
|
|
3
|
-
# Specify your gem's dependencies in esv.gemspec
|
|
3
|
+
# Specify your gem's dependencies in excel-esv.gemspec
|
|
4
4
|
gemspec
|
|
5
|
+
|
|
6
|
+
group :development do
|
|
7
|
+
gem "barsoom_utils"
|
|
8
|
+
gem "bundler"
|
|
9
|
+
gem "rake"
|
|
10
|
+
gem "rspec"
|
|
11
|
+
gem "rubocop"
|
|
12
|
+
end
|
data/README.md
CHANGED
|
@@ -45,7 +45,31 @@ output = ESV.parse(data)
|
|
|
45
45
|
# => [ [ "Name", "Dogs", … ], … ]
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
This
|
|
48
|
+
This will raise for a file with multiple worksheets unless you explicitly specify the one you want (the first worksheet is index 0):
|
|
49
|
+
|
|
50
|
+
``` ruby
|
|
51
|
+
ESV.parse(data, worksheet_index: 0)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`.parse` supports the `header_converters:` keyword argument, which takes the same arguments as `CSV.parse` does:
|
|
55
|
+
|
|
56
|
+
- a Symbol name for a registered header converter
|
|
57
|
+
- a Proc which takes the value and returns the converted value
|
|
58
|
+
- an Array of Symbol names for registered header converters
|
|
59
|
+
|
|
60
|
+
``` ruby
|
|
61
|
+
require "esv"
|
|
62
|
+
|
|
63
|
+
data = File.read("/tmp/test.xls")
|
|
64
|
+
output = ESV.parse(data, header_converters: :symbol)
|
|
65
|
+
# => [ [ :name, :dogs, … ], … ]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Registering a new converter:
|
|
69
|
+
|
|
70
|
+
``` ruby
|
|
71
|
+
ESV::HEADER_CONVERTERS[:upcase] = ->(value) { value.upcase }
|
|
72
|
+
```
|
|
49
73
|
|
|
50
74
|
### Parse file
|
|
51
75
|
|
|
@@ -58,6 +82,8 @@ output = ESV.parse_file("/tmp/test.xls")
|
|
|
58
82
|
|
|
59
83
|
This assumes a file with a single worksheet and will raise otherwise.
|
|
60
84
|
|
|
85
|
+
Also supports `header_converters:`.
|
|
86
|
+
|
|
61
87
|
### Generate in Ruby on Rails
|
|
62
88
|
|
|
63
89
|
In `config/initializers/mime_types.rb`:
|
data/Rakefile
CHANGED
data/excel-esv.gemspec
CHANGED
|
@@ -1,24 +1,21 @@
|
|
|
1
1
|
# coding: utf-8
|
|
2
|
-
lib = File.expand_path(
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
-
require
|
|
4
|
+
require "esv/version"
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "excel-esv"
|
|
8
8
|
spec.version = ESV::VERSION
|
|
9
|
-
spec.authors = ["Henrik Nyh"]
|
|
10
|
-
spec.email = ["henrik@nyh.se"]
|
|
9
|
+
spec.authors = [ "Henrik Nyh" ]
|
|
10
|
+
spec.email = [ "henrik@nyh.se" ]
|
|
11
11
|
spec.summary = %q{Excel parsing and generation with the ease of CSV.}
|
|
12
12
|
spec.homepage = "https://github.com/barsoom/excel-esv"
|
|
13
13
|
spec.license = "MIT"
|
|
14
|
+
spec.metadata = { "rubygems_mfa_required" => "true" }
|
|
14
15
|
|
|
15
16
|
spec.files = `git ls-files -z`.split("\x0")
|
|
16
|
-
spec.
|
|
17
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
18
|
-
spec.require_paths = ["lib"]
|
|
17
|
+
spec.require_paths = [ "lib" ]
|
|
19
18
|
|
|
20
|
-
spec.
|
|
21
|
-
spec.
|
|
22
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
|
23
|
-
spec.add_development_dependency "rspec"
|
|
19
|
+
spec.add_runtime_dependency "spreadsheet"
|
|
20
|
+
spec.add_runtime_dependency "logger"
|
|
24
21
|
end
|
data/lib/esv/version.rb
CHANGED
data/lib/esv.rb
CHANGED
|
@@ -16,27 +16,78 @@ module ESV
|
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
# You can register your own header converters in this Hash.
|
|
20
|
+
#
|
|
21
|
+
# @see https://rubyapi.org/3.2/o/csv#class-CSV-label-Custom+Header+Converters
|
|
22
|
+
HEADER_CONVERTERS = {
|
|
23
|
+
downcase: ->(value) {
|
|
24
|
+
value.respond_to?(:downcase) ? value.downcase : value
|
|
25
|
+
},
|
|
26
|
+
# Details:
|
|
27
|
+
#
|
|
28
|
+
# Strips leading and trailing whitespace.
|
|
29
|
+
# Downcases the header.
|
|
30
|
+
# Replaces embedded spaces with underscores.
|
|
31
|
+
# Removes non-word characters.
|
|
32
|
+
# Makes the string into a Symbol.
|
|
33
|
+
symbol: ->(value) {
|
|
34
|
+
value.to_s.strip.downcase.tr(" ", "_").gsub(/\W+/, "").to_sym
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
# @param data [Object] Spreadsheet data to be parsed.
|
|
39
|
+
# @param header_converters [nil, Symbol, Proc, Array<Symbol>] If given, can take these forms:
|
|
40
|
+
# - a Symbol name for a registered header converter
|
|
41
|
+
# - a Proc which takes the value and returns the converted value
|
|
42
|
+
# - an Array of Symbol names for registered header converters
|
|
43
|
+
# @param worksheet_index [nil, Integer] If given, specifies which worksheet index to parse (first is 0). If nil, expects exactly one worksheet.
|
|
44
|
+
# @return [Array<Array>] a list of rows
|
|
45
|
+
def self.parse(data, header_converters: nil, worksheet_index: nil)
|
|
20
46
|
fake_file = StringIO.new(data)
|
|
21
47
|
book = Spreadsheet.open(fake_file)
|
|
22
48
|
|
|
23
|
-
# We
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
49
|
+
# We prefer raising to silently ignoring worksheets.
|
|
50
|
+
if !worksheet_index && book.worksheets.length > 1
|
|
51
|
+
raise "Expected 1 worksheet, found #{book.worksheets.length}."
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
worksheet_index ||= 0
|
|
55
|
+
|
|
56
|
+
is_first_row = true
|
|
57
|
+
book.worksheet(worksheet_index).to_a.map(&:to_a).map { |row|
|
|
58
|
+
row.each_with_index.map { |cell, index|
|
|
59
|
+
value =
|
|
60
|
+
case cell
|
|
61
|
+
when Spreadsheet::Formula then cell.value
|
|
62
|
+
when Spreadsheet::Link then cell.href
|
|
63
|
+
else cell
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
if header_converters && is_first_row
|
|
67
|
+
case header_converters
|
|
68
|
+
when Proc then
|
|
69
|
+
value = header_converters.call(value)
|
|
70
|
+
when Symbol then
|
|
71
|
+
value = HEADER_CONVERTERS[header_converters].call(value)
|
|
72
|
+
when Enumerable then
|
|
73
|
+
# Apply the converters in order.
|
|
74
|
+
header_converters.each { |name|
|
|
75
|
+
value = HEADER_CONVERTERS.fetch(name).call(value)
|
|
76
|
+
}
|
|
77
|
+
else
|
|
78
|
+
raise "Unsupported kind of header_converters #{header_converters.inspect}"
|
|
79
|
+
end
|
|
80
|
+
value
|
|
81
|
+
else
|
|
82
|
+
value
|
|
34
83
|
end
|
|
84
|
+
}.tap {
|
|
85
|
+
is_first_row = false
|
|
35
86
|
}
|
|
36
87
|
}
|
|
37
88
|
end
|
|
38
89
|
|
|
39
|
-
def self.parse_file(path)
|
|
40
|
-
parse
|
|
90
|
+
def self.parse_file(path, header_converters: nil)
|
|
91
|
+
parse(File.read(path), header_converters: header_converters)
|
|
41
92
|
end
|
|
42
93
|
end
|
data/spec/esv_spec.rb
CHANGED
|
@@ -1,130 +1,224 @@
|
|
|
1
1
|
require "esv"
|
|
2
2
|
|
|
3
|
-
describe ESV
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
RSpec.describe ESV do
|
|
4
|
+
describe ".generate and .parse" do
|
|
5
|
+
it "works" do
|
|
6
|
+
data = ESV.generate do |esv|
|
|
7
|
+
esv << [ "Dogs", "Cats" ]
|
|
8
|
+
esv << [ 1, 2 ]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
output = ESV.parse(data)
|
|
12
|
+
|
|
13
|
+
expect(output).to eq [
|
|
14
|
+
[ "Dogs", "Cats" ],
|
|
15
|
+
[ 1, 2 ],
|
|
16
|
+
]
|
|
8
17
|
end
|
|
9
|
-
|
|
10
|
-
output = ESV.parse(data)
|
|
11
|
-
|
|
12
|
-
expect(output).to eq [
|
|
13
|
-
[ "Dogs", "Cats" ],
|
|
14
|
-
[ 1, 2 ],
|
|
15
|
-
]
|
|
16
18
|
end
|
|
17
|
-
end
|
|
18
19
|
|
|
19
|
-
describe
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
end
|
|
20
|
+
describe "::HEADER_CONVERTERS" do
|
|
21
|
+
context "with :downcase" do
|
|
22
|
+
it "downcases string value" do
|
|
23
|
+
expect(described_class::HEADER_CONVERTERS[:downcase].call("CAT")).to eq("cat")
|
|
24
|
+
end
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
]
|
|
26
|
+
it "returns a non-string value" do
|
|
27
|
+
expect(described_class::HEADER_CONVERTERS[:downcase].call(1)).to eq(1)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
context "with :symbol" do
|
|
32
|
+
it "stringifies before symbolizing given value" do
|
|
33
|
+
expect(described_class::HEADER_CONVERTERS[:symbol].call(1)).to eq(:"1")
|
|
34
|
+
end
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
[ "one", "two" ],
|
|
39
|
-
]
|
|
36
|
+
it "symbolizes given value" do
|
|
37
|
+
expect(described_class::HEADER_CONVERTERS[:symbol].call("cat")).to eq(:cat)
|
|
38
|
+
end
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
context "symbolizes and" do
|
|
41
|
+
it "downcases given value" do
|
|
42
|
+
expect(described_class::HEADER_CONVERTERS[:symbol].call("CAT")).to eq(:cat)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "strips any spaces around value" do
|
|
46
|
+
expect(described_class::HEADER_CONVERTERS[:symbol].call(" CAT ")).to eq(:cat)
|
|
47
|
+
end
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
[ "one", "two" ],
|
|
48
|
-
]
|
|
49
|
+
it "replaces embedded spaces with underscores" do
|
|
50
|
+
expect(described_class::HEADER_CONVERTERS[:symbol].call("CAT CAT")).to eq(:cat_cat)
|
|
51
|
+
end
|
|
49
52
|
|
|
50
|
-
|
|
53
|
+
it "removes non-word characters" do
|
|
54
|
+
expect(described_class::HEADER_CONVERTERS[:symbol].call("CAT?")).to eq(:cat)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
51
58
|
end
|
|
52
59
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
describe ".parse" do
|
|
61
|
+
describe "worksheet handling" do
|
|
62
|
+
context "with more than one worksheet" do
|
|
63
|
+
let(:excel_file_with_two_worksheets) {
|
|
64
|
+
generate_excel_file do |sheet_0, book|
|
|
65
|
+
sheet_0.row(0).replace([ "I am worksheet 0" ])
|
|
66
|
+
|
|
67
|
+
sheet_1 = book.create_worksheet
|
|
68
|
+
sheet_1.row(0).replace([ "I am worksheet 1" ])
|
|
69
|
+
end
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
it "raises when not given a worksheet index" do
|
|
73
|
+
expect {
|
|
74
|
+
ESV.parse(excel_file_with_two_worksheets)
|
|
75
|
+
}.to raise_error(/Expected 1 worksheet, found 2/)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "allows specifying a worksheet index" do
|
|
79
|
+
output = ESV.parse(excel_file_with_two_worksheets, worksheet_index: 0)
|
|
80
|
+
expect(output).to eq [[ "I am worksheet 0" ]]
|
|
81
|
+
|
|
82
|
+
output = ESV.parse(excel_file_with_two_worksheets, worksheet_index: 1)
|
|
83
|
+
expect(output).to eq [[ "I am worksheet 1" ]]
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it "ignores formatting, always returning a plain array of data" do
|
|
89
|
+
excel_file_with_formatting = generate_excel_file do |sheet|
|
|
90
|
+
sheet.row(0).replace([ 1, 2 ])
|
|
91
|
+
sheet.row(0).default_format = Spreadsheet::Format.new(color: :blue)
|
|
92
|
+
end
|
|
58
93
|
|
|
59
|
-
|
|
60
|
-
end
|
|
94
|
+
output = ESV.parse(excel_file_with_formatting)
|
|
61
95
|
|
|
62
|
-
|
|
96
|
+
expect(output).to eq [
|
|
97
|
+
[ 1, 2 ],
|
|
98
|
+
]
|
|
63
99
|
|
|
64
|
-
|
|
65
|
-
excel_file do |sheet, book|
|
|
66
|
-
book.create_worksheet
|
|
100
|
+
expect(output[0].class).to eq Array
|
|
67
101
|
end
|
|
68
|
-
end
|
|
69
102
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
103
|
+
it "returns the last value of a formula cell" do
|
|
104
|
+
excel_file_with_formula = generate_excel_file do |sheet|
|
|
105
|
+
formula = Spreadsheet::Formula.new
|
|
106
|
+
formula.value = "two"
|
|
107
|
+
sheet.row(0).replace([ "one", formula ])
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
output = ESV.parse(excel_file_with_formula)
|
|
111
|
+
|
|
112
|
+
expect(output).to eq [
|
|
113
|
+
[ "one", "two" ],
|
|
114
|
+
]
|
|
115
|
+
expect(output[0].class).to eq Array
|
|
74
116
|
end
|
|
75
|
-
end
|
|
76
117
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
118
|
+
|
|
119
|
+
it "returns the URL of a link cell" do
|
|
120
|
+
excel_file_with_link = generate_excel_file do |sheet|
|
|
121
|
+
link = Spreadsheet::Link.new("https://example.com", "desc", "foo")
|
|
122
|
+
sheet.row(0).replace([ "one", link ])
|
|
123
|
+
end
|
|
124
|
+
output = ESV.parse(excel_file_with_link)
|
|
125
|
+
expect(output).to eq [
|
|
126
|
+
[ "one", "https://example.com#foo" ],
|
|
127
|
+
]
|
|
128
|
+
|
|
129
|
+
expect(output[0][1].class).to eq String
|
|
82
130
|
end
|
|
83
|
-
end
|
|
84
131
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
132
|
+
context "when given a header_converters: option" do
|
|
133
|
+
let(:data) {
|
|
134
|
+
ESV.generate do |esv|
|
|
135
|
+
esv << [ "Dogs", "Cats cats" ]
|
|
136
|
+
esv << [ 1, 2 ]
|
|
137
|
+
end
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
it "lets you specify header_converters by symbolic name" do
|
|
141
|
+
output = ESV.parse(data, header_converters: :downcase)
|
|
142
|
+
|
|
143
|
+
expect(output).to eq [
|
|
144
|
+
[ "dogs", "cats cats" ],
|
|
145
|
+
[ 1, 2 ],
|
|
146
|
+
]
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
it "lets you specify header_converters as a proc" do
|
|
150
|
+
output = ESV.parse(data, header_converters: ->(value) { value.upcase })
|
|
151
|
+
|
|
152
|
+
expect(output).to eq [
|
|
153
|
+
[ "DOGS", "CATS CATS" ],
|
|
154
|
+
[ 1, 2 ],
|
|
155
|
+
]
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
it "lets you register new symbolic names for header_converters and use them" do
|
|
159
|
+
ESV::HEADER_CONVERTERS[:upcase] = ->(value) { value.upcase }
|
|
160
|
+
|
|
161
|
+
output = ESV.parse(data, header_converters: :upcase)
|
|
162
|
+
|
|
163
|
+
expect(output).to eq [
|
|
164
|
+
[ "DOGS", "CATS CATS" ],
|
|
165
|
+
[ 1, 2 ],
|
|
166
|
+
]
|
|
167
|
+
|
|
168
|
+
ESV::HEADER_CONVERTERS.delete(:upcase)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it "lets you specify header_converters by symbolic names as a list and have them apply in order" do
|
|
172
|
+
ESV::HEADER_CONVERTERS[:reverse] = ->(value) { value.reverse }
|
|
173
|
+
output = ESV.parse(data, header_converters: [ :downcase, :reverse, :symbol ])
|
|
174
|
+
|
|
175
|
+
expect(output).to eq [
|
|
176
|
+
[ :sgod, :stac_stac ],
|
|
177
|
+
[ 1, 2 ],
|
|
178
|
+
]
|
|
179
|
+
ESV::HEADER_CONVERTERS.delete(:reverse)
|
|
180
|
+
end
|
|
89
181
|
end
|
|
90
|
-
end
|
|
91
182
|
|
|
92
|
-
|
|
93
|
-
book = Spreadsheet::Workbook.new
|
|
94
|
-
sheet = book.create_worksheet
|
|
183
|
+
private
|
|
95
184
|
|
|
96
|
-
block
|
|
185
|
+
def generate_excel_file(&block)
|
|
186
|
+
book = Spreadsheet::Workbook.new
|
|
187
|
+
sheet = book.create_worksheet
|
|
97
188
|
|
|
98
|
-
|
|
99
|
-
fake_file = StringIO.new(data)
|
|
100
|
-
book.write(fake_file)
|
|
101
|
-
data
|
|
102
|
-
end
|
|
103
|
-
end
|
|
189
|
+
block.call(sheet, book)
|
|
104
190
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
191
|
+
data = ""
|
|
192
|
+
fake_file = StringIO.new(data)
|
|
193
|
+
book.write(fake_file)
|
|
194
|
+
data
|
|
195
|
+
end
|
|
108
196
|
end
|
|
109
197
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
ESV.generate_file(path) do |esv|
|
|
114
|
-
esv << [ "Dogs", "Cats" ]
|
|
115
|
-
esv << [ 1, 2 ]
|
|
198
|
+
describe ".generate_file and .parse_file" do
|
|
199
|
+
before do
|
|
200
|
+
@file = Tempfile.new("esv")
|
|
116
201
|
end
|
|
117
202
|
|
|
118
|
-
|
|
203
|
+
it "works" do
|
|
204
|
+
path = @file.path
|
|
119
205
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
206
|
+
ESV.generate_file(path) do |esv|
|
|
207
|
+
esv << [ "Dogs", "Cats" ]
|
|
208
|
+
esv << [ 1, 2 ]
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
output = ESV.parse_file(path)
|
|
125
212
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
213
|
+
expect(output).to eq [
|
|
214
|
+
[ "Dogs", "Cats" ],
|
|
215
|
+
[ 1, 2 ],
|
|
216
|
+
]
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
after do
|
|
220
|
+
@file.close
|
|
221
|
+
@file.unlink
|
|
222
|
+
end
|
|
129
223
|
end
|
|
130
224
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,23 +1,4 @@
|
|
|
1
|
-
# This file was generated by the `rspec --init` command. Conventionally, all
|
|
2
|
-
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
|
3
|
-
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
|
4
|
-
# file to always be loaded, without a need to explicitly require it in any files.
|
|
5
|
-
#
|
|
6
|
-
# Given that it is always loaded, you are encouraged to keep this file as
|
|
7
|
-
# light-weight as possible. Requiring heavyweight dependencies from this file
|
|
8
|
-
# will add to the boot time of your test suite on EVERY test run, even for an
|
|
9
|
-
# individual file that may not need all of that loaded. Instead, make a
|
|
10
|
-
# separate helper file that requires this one and then use it only in the specs
|
|
11
|
-
# that actually need it.
|
|
12
|
-
#
|
|
13
|
-
# The `.rspec` file also contains a few flags that are not defaults but that
|
|
14
|
-
# users commonly want.
|
|
15
|
-
#
|
|
16
|
-
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
|
17
1
|
RSpec.configure do |config|
|
|
18
|
-
# The settings below are suggested to provide a good initial experience
|
|
19
|
-
# with RSpec, but feel free to customize to your heart's content.
|
|
20
|
-
=begin
|
|
21
2
|
# These two settings work together to allow you to limit a spec run
|
|
22
3
|
# to individual examples or groups you care about by tagging them with
|
|
23
4
|
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
|
@@ -32,7 +13,7 @@ RSpec.configure do |config|
|
|
|
32
13
|
# Use the documentation formatter for detailed output,
|
|
33
14
|
# unless a formatter has already been configured
|
|
34
15
|
# (e.g. via a command-line flag).
|
|
35
|
-
config.default_formatter =
|
|
16
|
+
config.default_formatter = "doc"
|
|
36
17
|
end
|
|
37
18
|
|
|
38
19
|
# Print the 10 slowest examples and example groups at the
|
|
@@ -45,6 +26,7 @@ RSpec.configure do |config|
|
|
|
45
26
|
# the seed, which is printed after each run.
|
|
46
27
|
# --seed 1234
|
|
47
28
|
config.order = :random
|
|
29
|
+
config.disable_monkey_patching!
|
|
48
30
|
|
|
49
31
|
# Seed global randomization in this process using the `--seed` CLI option.
|
|
50
32
|
# Setting this allows you to use `--seed` to deterministically reproduce
|
|
@@ -62,17 +44,11 @@ RSpec.configure do |config|
|
|
|
62
44
|
expectations.syntax = :expect
|
|
63
45
|
end
|
|
64
46
|
|
|
65
|
-
# rspec-mocks config goes here. You can use an alternate test double
|
|
66
|
-
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
|
67
47
|
config.mock_with :rspec do |mocks|
|
|
68
|
-
# Enable only the newer, non-monkey-patching expect syntax.
|
|
69
|
-
# For more details, see:
|
|
70
|
-
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
|
71
48
|
mocks.syntax = :expect
|
|
72
49
|
|
|
73
50
|
# Prevents you from mocking or stubbing a method that does not exist on
|
|
74
51
|
# a real object. This is generally recommended.
|
|
75
52
|
mocks.verify_partial_doubles = true
|
|
76
53
|
end
|
|
77
|
-
=end
|
|
78
54
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: excel-esv
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Henrik Nyh
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: spreadsheet
|
|
@@ -25,56 +24,30 @@ dependencies:
|
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
25
|
version: '0'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
|
-
name:
|
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
|
30
|
-
requirements:
|
|
31
|
-
- - "~>"
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: '1.7'
|
|
34
|
-
type: :development
|
|
35
|
-
prerelease: false
|
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
-
requirements:
|
|
38
|
-
- - "~>"
|
|
39
|
-
- !ruby/object:Gem::Version
|
|
40
|
-
version: '1.7'
|
|
41
|
-
- !ruby/object:Gem::Dependency
|
|
42
|
-
name: rake
|
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
|
44
|
-
requirements:
|
|
45
|
-
- - "~>"
|
|
46
|
-
- !ruby/object:Gem::Version
|
|
47
|
-
version: '10.0'
|
|
48
|
-
type: :development
|
|
49
|
-
prerelease: false
|
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
-
requirements:
|
|
52
|
-
- - "~>"
|
|
53
|
-
- !ruby/object:Gem::Version
|
|
54
|
-
version: '10.0'
|
|
55
|
-
- !ruby/object:Gem::Dependency
|
|
56
|
-
name: rspec
|
|
27
|
+
name: logger
|
|
57
28
|
requirement: !ruby/object:Gem::Requirement
|
|
58
29
|
requirements:
|
|
59
30
|
- - ">="
|
|
60
31
|
- !ruby/object:Gem::Version
|
|
61
32
|
version: '0'
|
|
62
|
-
type: :
|
|
33
|
+
type: :runtime
|
|
63
34
|
prerelease: false
|
|
64
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
36
|
requirements:
|
|
66
37
|
- - ">="
|
|
67
38
|
- !ruby/object:Gem::Version
|
|
68
39
|
version: '0'
|
|
69
|
-
description:
|
|
70
40
|
email:
|
|
71
41
|
- henrik@nyh.se
|
|
72
42
|
executables: []
|
|
73
43
|
extensions: []
|
|
74
44
|
extra_rdoc_files: []
|
|
75
45
|
files:
|
|
46
|
+
- ".github/dependabot.yml"
|
|
47
|
+
- ".github/workflows/ci.yml"
|
|
76
48
|
- ".gitignore"
|
|
77
49
|
- ".rspec"
|
|
50
|
+
- ".rubocop.yml"
|
|
78
51
|
- CHANGELOG.md
|
|
79
52
|
- Gemfile
|
|
80
53
|
- LICENSE.txt
|
|
@@ -91,8 +64,8 @@ files:
|
|
|
91
64
|
homepage: https://github.com/barsoom/excel-esv
|
|
92
65
|
licenses:
|
|
93
66
|
- MIT
|
|
94
|
-
metadata:
|
|
95
|
-
|
|
67
|
+
metadata:
|
|
68
|
+
rubygems_mfa_required: 'true'
|
|
96
69
|
rdoc_options: []
|
|
97
70
|
require_paths:
|
|
98
71
|
- lib
|
|
@@ -107,11 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
107
80
|
- !ruby/object:Gem::Version
|
|
108
81
|
version: '0'
|
|
109
82
|
requirements: []
|
|
110
|
-
|
|
111
|
-
rubygems_version: 2.6.11
|
|
112
|
-
signing_key:
|
|
83
|
+
rubygems_version: 3.6.9
|
|
113
84
|
specification_version: 4
|
|
114
85
|
summary: Excel parsing and generation with the ease of CSV.
|
|
115
|
-
test_files:
|
|
116
|
-
- spec/esv_spec.rb
|
|
117
|
-
- spec/spec_helper.rb
|
|
86
|
+
test_files: []
|