ruby-enum 0.9.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +94 -64
- data/README.md +59 -0
- data/Rakefile +7 -0
- data/benchmarks/case.rb +45 -0
- data/coverage/index.html +5445 -501
- data/lib/ruby-enum/enum/case.rb +84 -0
- data/lib/ruby-enum/enum/i18n_mock.rb +19 -0
- data/lib/ruby-enum/enum.rb +6 -3
- data/lib/ruby-enum/errors/base.rb +3 -3
- data/lib/ruby-enum/version.rb +1 -1
- data/lib/ruby-enum.rb +15 -3
- data/ruby-enum.gemspec +2 -1
- data/spec/ruby-enum/enum/case_spec.rb +118 -0
- data/spec/ruby-enum/enum_spec.rb +106 -34
- data/spec_i18n/Gemfile +9 -0
- data/spec_i18n/Gemfile.lock +35 -0
- data/spec_i18n/Rakefile +12 -0
- data/spec_i18n/spec/i18n_spec.rb +48 -0
- data/spec_i18n/spec/spec_helper.rb +8 -0
- metadata +16 -21
- data/pkg/ruby-enum-0.8.0.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aca947b7b06abbc0504549ab9fa43e99be4d3e0dc408a92b8e10efea700d9bf7
|
4
|
+
data.tar.gz: 7e57ab06229ac0552b984630493f04dd98a3579b7ca8151c819dc792417d505f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f626bd0c2c072f17734527253a8ae95e96dc0631779f6d56b398c540a9d63c3b41eb000647d276d81f859ae92f11e5a0a74e9966cf96f1796cad5ef172c42239
|
7
|
+
data.tar.gz: 2d58616dc04acf94a9a7e65029fe6422224717bb849cd07513782206ecc543b4d478a7d539b5fa1d434ab3b841430772406cd112438152541242d32a3081e38e
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
### 1.0.0 (2023/01/10)
|
2
|
+
|
3
|
+
* [#41](https://github.com/dblock/ruby-enum/pull/41): Make i18n dependency optional - [@peterfication](https://github.com/peterfication).
|
4
|
+
* [#43](https://github.com/dblock/ruby-enum/pull/43): Add exhaustive case matcher - [@peterfication](https://github.com/peterfication).
|
5
|
+
* [#40](https://github.com/dblock/ruby-enum/pull/39): Enable new Rubocop cops and address/allowlist lints - [@petergoldstein](https://github.com/petergoldstein).
|
6
|
+
* [#39](https://github.com/dblock/ruby-enum/pull/39): Require Ruby >= 2.7 - [@petergoldstein](https://github.com/petergoldstein).
|
7
|
+
* [#38](https://github.com/dblock/ruby-enum/pull/38): Ensure Ruby >= 2.3 - [@ojab](https://github.com/ojab).
|
8
|
+
|
1
9
|
### 0.9.0 (2021/01/21)
|
2
10
|
|
3
11
|
* [#34](https://github.com/dblock/ruby-enum/pull/34): Added support for Ruby 3.0 - [@dblock](https://github.com/dblock).
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,43 +1,49 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-enum (0.
|
5
|
-
i18n
|
4
|
+
ruby-enum (1.0.0)
|
6
5
|
|
7
6
|
GEM
|
8
7
|
remote: http://rubygems.org/
|
9
8
|
specs:
|
10
|
-
activesupport (
|
9
|
+
activesupport (7.1.2)
|
10
|
+
base64
|
11
|
+
bigdecimal
|
11
12
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
connection_pool (>= 2.2.5)
|
14
|
+
drb
|
12
15
|
i18n (>= 1.6, < 2)
|
13
16
|
minitest (>= 5.1)
|
17
|
+
mutex_m
|
14
18
|
tzinfo (~> 2.0)
|
15
|
-
|
16
|
-
|
17
|
-
public_suffix (>= 2.0.2, < 5.0)
|
19
|
+
addressable (2.8.6)
|
20
|
+
public_suffix (>= 2.0.2, < 6.0)
|
18
21
|
ast (2.4.2)
|
19
|
-
|
22
|
+
base64 (0.2.0)
|
23
|
+
bigdecimal (3.1.5)
|
24
|
+
claide (1.1.0)
|
20
25
|
claide-plugins (0.9.2)
|
21
26
|
cork
|
22
27
|
nap
|
23
28
|
open4 (~> 1.3)
|
24
29
|
colored2 (3.1.2)
|
25
|
-
concurrent-ruby (1.
|
30
|
+
concurrent-ruby (1.2.2)
|
31
|
+
connection_pool (2.4.1)
|
26
32
|
cork (0.3.0)
|
27
33
|
colored2 (~> 3.1)
|
28
|
-
danger (
|
34
|
+
danger (9.4.2)
|
29
35
|
claide (~> 1.0)
|
30
36
|
claide-plugins (>= 0.9.2)
|
31
37
|
colored2 (~> 3.1)
|
32
38
|
cork (~> 0.1)
|
33
|
-
faraday (>= 0.9.0, <
|
39
|
+
faraday (>= 0.9.0, < 3.0)
|
34
40
|
faraday-http-cache (~> 2.0)
|
35
|
-
git (~> 1.
|
41
|
+
git (~> 1.13)
|
36
42
|
kramdown (~> 2.3)
|
37
43
|
kramdown-parser-gfm (~> 1.0)
|
38
44
|
no_proxy_fix
|
39
|
-
octokit (
|
40
|
-
terminal-table (
|
45
|
+
octokit (>= 4.0)
|
46
|
+
terminal-table (>= 1, < 4)
|
41
47
|
danger-changelog (0.6.1)
|
42
48
|
danger-plugin-api (~> 1.0)
|
43
49
|
danger-plugin-api (1.0.0)
|
@@ -46,80 +52,102 @@ GEM
|
|
46
52
|
activesupport
|
47
53
|
danger-plugin-api (~> 1.0)
|
48
54
|
kramdown
|
49
|
-
diff-lcs (1.
|
50
|
-
docile (1.
|
51
|
-
|
52
|
-
faraday-net_http (~> 1.0)
|
53
|
-
multipart-post (>= 1.2, < 3)
|
55
|
+
diff-lcs (1.5.0)
|
56
|
+
docile (1.4.0)
|
57
|
+
drb (2.2.0)
|
54
58
|
ruby2_keywords
|
55
|
-
faraday
|
59
|
+
faraday (2.8.1)
|
60
|
+
base64
|
61
|
+
faraday-net_http (>= 2.0, < 3.1)
|
62
|
+
ruby2_keywords (>= 0.0.4)
|
63
|
+
faraday-http-cache (2.5.0)
|
56
64
|
faraday (>= 0.8)
|
57
|
-
faraday-net_http (
|
58
|
-
git (1.
|
65
|
+
faraday-net_http (3.0.2)
|
66
|
+
git (1.19.0)
|
67
|
+
addressable (~> 2.8)
|
59
68
|
rchardet (~> 1.8)
|
60
|
-
i18n (1.
|
69
|
+
i18n (1.14.1)
|
61
70
|
concurrent-ruby (~> 1.0)
|
62
|
-
|
63
|
-
kramdown (2.
|
71
|
+
json (2.7.1)
|
72
|
+
kramdown (2.4.0)
|
64
73
|
rexml
|
65
74
|
kramdown-parser-gfm (1.1.0)
|
66
75
|
kramdown (~> 2.0)
|
67
|
-
|
68
|
-
|
76
|
+
language_server-protocol (3.17.0.3)
|
77
|
+
minitest (5.20.0)
|
78
|
+
mutex_m (0.2.0)
|
69
79
|
nap (1.1.0)
|
70
80
|
no_proxy_fix (0.1.2)
|
71
|
-
octokit (
|
72
|
-
faraday (>=
|
73
|
-
sawyer (~> 0.
|
81
|
+
octokit (8.0.0)
|
82
|
+
faraday (>= 1, < 3)
|
83
|
+
sawyer (~> 0.9)
|
74
84
|
open4 (1.3.4)
|
75
|
-
parallel (1.
|
76
|
-
parser (3.
|
85
|
+
parallel (1.24.0)
|
86
|
+
parser (3.2.2.4)
|
77
87
|
ast (~> 2.4.1)
|
78
|
-
|
79
|
-
|
80
|
-
|
88
|
+
racc
|
89
|
+
public_suffix (5.0.4)
|
90
|
+
racc (1.7.3)
|
91
|
+
rainbow (3.1.1)
|
92
|
+
rake (13.1.0)
|
81
93
|
rchardet (1.8.0)
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
rspec-
|
86
|
-
rspec-
|
87
|
-
|
88
|
-
|
89
|
-
|
94
|
+
regexp_parser (2.8.3)
|
95
|
+
rexml (3.2.6)
|
96
|
+
rspec (3.12.0)
|
97
|
+
rspec-core (~> 3.12.0)
|
98
|
+
rspec-expectations (~> 3.12.0)
|
99
|
+
rspec-mocks (~> 3.12.0)
|
100
|
+
rspec-core (3.12.2)
|
101
|
+
rspec-support (~> 3.12.0)
|
102
|
+
rspec-expectations (3.12.3)
|
90
103
|
diff-lcs (>= 1.2.0, < 2.0)
|
91
|
-
rspec-support (~> 3.
|
92
|
-
rspec-mocks (3.
|
104
|
+
rspec-support (~> 3.12.0)
|
105
|
+
rspec-mocks (3.12.6)
|
93
106
|
diff-lcs (>= 1.2.0, < 2.0)
|
94
|
-
rspec-support (~> 3.
|
95
|
-
rspec-support (3.
|
96
|
-
rubocop (
|
97
|
-
|
107
|
+
rspec-support (~> 3.12.0)
|
108
|
+
rspec-support (3.12.1)
|
109
|
+
rubocop (1.59.0)
|
110
|
+
json (~> 2.3)
|
111
|
+
language_server-protocol (>= 3.17.0)
|
98
112
|
parallel (~> 1.10)
|
99
|
-
parser (>= 2.
|
113
|
+
parser (>= 3.2.2.4)
|
100
114
|
rainbow (>= 2.2.2, < 4.0)
|
101
|
-
|
115
|
+
regexp_parser (>= 1.8, < 3.0)
|
116
|
+
rexml (>= 3.2.5, < 4.0)
|
117
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
102
118
|
ruby-progressbar (~> 1.7)
|
103
|
-
unicode-display_width (>=
|
104
|
-
|
105
|
-
|
106
|
-
|
119
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
120
|
+
rubocop-ast (1.30.0)
|
121
|
+
parser (>= 3.2.1.0)
|
122
|
+
rubocop-capybara (2.20.0)
|
123
|
+
rubocop (~> 1.41)
|
124
|
+
rubocop-factory_bot (2.25.0)
|
125
|
+
rubocop (~> 1.33)
|
126
|
+
rubocop-rake (0.6.0)
|
127
|
+
rubocop (~> 1.0)
|
128
|
+
rubocop-rspec (2.26.0)
|
129
|
+
rubocop (~> 1.40)
|
130
|
+
rubocop-capybara (~> 2.17)
|
131
|
+
rubocop-factory_bot (~> 2.22)
|
132
|
+
ruby-progressbar (1.13.0)
|
133
|
+
ruby2_keywords (0.0.5)
|
134
|
+
sawyer (0.9.2)
|
107
135
|
addressable (>= 2.3.5)
|
108
|
-
faraday (
|
109
|
-
simplecov (0.
|
136
|
+
faraday (>= 0.17.3, < 3)
|
137
|
+
simplecov (0.22.0)
|
110
138
|
docile (~> 1.1)
|
111
139
|
simplecov-html (~> 0.11)
|
112
140
|
simplecov_json_formatter (~> 0.1)
|
113
141
|
simplecov-html (0.12.3)
|
114
|
-
simplecov_json_formatter (0.1.
|
115
|
-
terminal-table (
|
116
|
-
unicode-display_width (
|
117
|
-
tzinfo (2.0.
|
142
|
+
simplecov_json_formatter (0.1.4)
|
143
|
+
terminal-table (3.0.2)
|
144
|
+
unicode-display_width (>= 1.1.1, < 3)
|
145
|
+
tzinfo (2.0.6)
|
118
146
|
concurrent-ruby (~> 1.0)
|
119
|
-
unicode-display_width (
|
120
|
-
zeitwerk (2.4.2)
|
147
|
+
unicode-display_width (2.5.0)
|
121
148
|
|
122
149
|
PLATFORMS
|
150
|
+
arm64-darwin-21
|
123
151
|
ruby
|
124
152
|
|
125
153
|
DEPENDENCIES
|
@@ -128,9 +156,11 @@ DEPENDENCIES
|
|
128
156
|
danger-toc (= 0.2.0)
|
129
157
|
rake
|
130
158
|
rspec (~> 3.0)
|
131
|
-
rubocop (
|
159
|
+
rubocop (~> 1.0)
|
160
|
+
rubocop-rake
|
161
|
+
rubocop-rspec
|
132
162
|
ruby-enum!
|
133
163
|
simplecov
|
134
164
|
|
135
165
|
BUNDLED WITH
|
136
|
-
2.1
|
166
|
+
2.5.1
|
data/README.md
CHANGED
@@ -24,6 +24,9 @@ Enum-like behavior for Ruby, heavily inspired by [this](http://www.rubyfleebie.c
|
|
24
24
|
- [Mapping values to keys](#mapping-values-to-keys)
|
25
25
|
- [Duplicate enumerator keys or duplicate values](#duplicate-enumerator-keys-or-duplicate-values)
|
26
26
|
- [Inheritance](#inheritance)
|
27
|
+
- [Exhaustive case matcher](#exhaustive-case-matcher)
|
28
|
+
- [I18n support](#i18n-support)
|
29
|
+
- [Benchmarks](#benchmarks)
|
27
30
|
- [Contributing](#contributing)
|
28
31
|
- [Copyright and License](#copyright-and-license)
|
29
32
|
- [Related Projects](#related-projects)
|
@@ -259,6 +262,62 @@ OrderState.values # ['CREATED', 'PAID']
|
|
259
262
|
ShippedOrderState.values # ['CREATED', 'PAID', 'PREPARED', SHIPPED']
|
260
263
|
```
|
261
264
|
|
265
|
+
### Exhaustive case matcher
|
266
|
+
|
267
|
+
If you want to make sure that you cover all cases in a case stament, you can use the exhaustive case matcher: `Ruby::Enum::Case`. It will raise an error if a case/enum value is not handled, or if a value is specified that's not part of the enum. This is inspired by the [Rust Pattern Syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html). If multiple cases match, all matches are being executed. The return value is the value from the matched case, or an array of return values if multiple cases matched.
|
268
|
+
|
269
|
+
> NOTE: This will add checks at runtime which might lead to worse performance. See [benchmarks](#benchmarks).
|
270
|
+
|
271
|
+
> NOTE: `:else` is a reserved keyword if you want to use `Ruby::Enum::Case`.
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
class Color < OrderState
|
275
|
+
include Ruby::Enum
|
276
|
+
include Ruby::Enum::Case
|
277
|
+
|
278
|
+
define :RED, :red
|
279
|
+
define :GREEN, :green
|
280
|
+
define :BLUE, :blue
|
281
|
+
define :YELLOW, :yellow
|
282
|
+
end
|
283
|
+
```
|
284
|
+
|
285
|
+
```ruby
|
286
|
+
color = Color::RED
|
287
|
+
Color.Case(color, {
|
288
|
+
[Color::GREEN, Color::BLUE] => -> { "order is green or blue" },
|
289
|
+
Color::YELLOW => -> { "order is yellow" },
|
290
|
+
Color::RED => -> { "order is red" },
|
291
|
+
})
|
292
|
+
```
|
293
|
+
|
294
|
+
It also supports default/else:
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
color = Color::RED
|
298
|
+
Color.Case(color, {
|
299
|
+
[Color::GREEN, Color::BLUE] => -> { "order is green or blue" },
|
300
|
+
else: -> { "order is yellow or red" },
|
301
|
+
})
|
302
|
+
```
|
303
|
+
|
304
|
+
### I18n support
|
305
|
+
|
306
|
+
This gem has an optional dependency to `i18n`. If it's available, the error messages will have a nice description and can be translated. If it's not available, the errors will only contain the message keys.
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
# Add this to your Gemfile if you want to have a nice error description instead of just a message key.
|
310
|
+
gem "i18n"
|
311
|
+
```
|
312
|
+
|
313
|
+
## Benchmarks
|
314
|
+
|
315
|
+
Benchmark scripts are defined in the [`benchmarks`](benchmarks) folder and can be run with Rake:
|
316
|
+
|
317
|
+
```console
|
318
|
+
rake benchmarks:case
|
319
|
+
```
|
320
|
+
|
262
321
|
## Contributing
|
263
322
|
|
264
323
|
You're encouraged to contribute to ruby-enum. See [CONTRIBUTING](CONTRIBUTING.md) for details.
|
data/Rakefile
CHANGED
data/benchmarks/case.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
|
5
|
+
require 'benchmark'
|
6
|
+
require 'ruby-enum'
|
7
|
+
|
8
|
+
##
|
9
|
+
# Test enum
|
10
|
+
class Color
|
11
|
+
include Ruby::Enum
|
12
|
+
include Ruby::Enum::Case
|
13
|
+
|
14
|
+
define :RED, :red
|
15
|
+
define :GREEN, :green
|
16
|
+
define :BLUE, :blue
|
17
|
+
end
|
18
|
+
|
19
|
+
puts 'Running 1.000.000 normal case statements'
|
20
|
+
case_statement_time = Benchmark.realtime do
|
21
|
+
1_000_000.times do
|
22
|
+
case Color::RED
|
23
|
+
when Color::RED, Color::GREEN
|
24
|
+
'red or green'
|
25
|
+
when Color::BLUE
|
26
|
+
'blue'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
puts 'Running 1.000.000 ruby-enum case statements'
|
32
|
+
ruby_enum_time = Benchmark.realtime do
|
33
|
+
1_000_000.times do
|
34
|
+
Color.case(Color::RED,
|
35
|
+
{
|
36
|
+
[Color::RED, Color::GREEN] => -> { 'red or green' },
|
37
|
+
Color::BLUE => -> { 'blue' }
|
38
|
+
})
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
puts "ruby-enum case: #{ruby_enum_time.round(4)}"
|
43
|
+
puts "case statement: #{case_statement_time.round(4)}"
|
44
|
+
|
45
|
+
puts "ruby-enum case is #{(ruby_enum_time / case_statement_time).round(2)} times slower"
|