ruby-enum 0.9.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26cee9efd34000cd0b004832d0eaae6ba5b90a3668c039b7490c3b7c0605e965
4
- data.tar.gz: 4d5b7744d335dc558f3953d0b9ad27d38d44b7a40219cbd29ad3f360e7d54100
3
+ metadata.gz: aca947b7b06abbc0504549ab9fa43e99be4d3e0dc408a92b8e10efea700d9bf7
4
+ data.tar.gz: 7e57ab06229ac0552b984630493f04dd98a3579b7ca8151c819dc792417d505f
5
5
  SHA512:
6
- metadata.gz: 982371ddb8393adf84259e32f20df8e69de71c2084e5dcd49d52e30a3c17941a5321dcac5ecb6f2aa194486963037e61034609d05ec1c3e1681599fe59fdd936
7
- data.tar.gz: a69948eefae40413871fc3f4c4d49b218428c86a13eeeaba256d903337fb7e1d972fb4689bd6bd8ab9c4c58ac44e414d49a4ee3ee2bdddaf9e23deed47fbcbc4
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
@@ -11,7 +11,9 @@ group :development, :test do
11
11
  gem 'danger-changelog', '0.6.1'
12
12
  gem 'danger-toc', '0.2.0'
13
13
  gem 'rspec', '~> 3.0'
14
- gem 'rubocop', '0.80.1'
14
+ gem 'rubocop', '~> 1.0'
15
+ gem 'rubocop-rake'
16
+ gem 'rubocop-rspec'
15
17
  end
16
18
 
17
19
  group :test do
data/Gemfile.lock CHANGED
@@ -1,43 +1,49 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-enum (0.9.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 (6.1.0)
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
- zeitwerk (~> 2.3)
16
- addressable (2.7.0)
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
- claide (1.0.3)
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.1.7)
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 (8.2.1)
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, < 2.0)
39
+ faraday (>= 0.9.0, < 3.0)
34
40
  faraday-http-cache (~> 2.0)
35
- git (~> 1.7)
41
+ git (~> 1.13)
36
42
  kramdown (~> 2.3)
37
43
  kramdown-parser-gfm (~> 1.0)
38
44
  no_proxy_fix
39
- octokit (~> 4.7)
40
- terminal-table (~> 1)
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.3)
50
- docile (1.3.5)
51
- faraday (1.3.0)
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-http-cache (2.2.0)
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 (1.0.0)
58
- git (1.8.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.8.7)
69
+ i18n (1.14.1)
61
70
  concurrent-ruby (~> 1.0)
62
- jaro_winkler (1.5.4)
63
- kramdown (2.3.0)
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
- minitest (5.14.3)
68
- multipart-post (2.1.1)
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 (4.20.0)
72
- faraday (>= 0.9)
73
- sawyer (~> 0.8.0, >= 0.5.3)
81
+ octokit (8.0.0)
82
+ faraday (>= 1, < 3)
83
+ sawyer (~> 0.9)
74
84
  open4 (1.3.4)
75
- parallel (1.20.1)
76
- parser (3.0.0.0)
85
+ parallel (1.24.0)
86
+ parser (3.2.2.4)
77
87
  ast (~> 2.4.1)
78
- public_suffix (4.0.6)
79
- rainbow (3.0.0)
80
- rake (13.0.1)
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
- rexml (3.2.4)
83
- rspec (3.9.0)
84
- rspec-core (~> 3.9.0)
85
- rspec-expectations (~> 3.9.0)
86
- rspec-mocks (~> 3.9.0)
87
- rspec-core (3.9.1)
88
- rspec-support (~> 3.9.1)
89
- rspec-expectations (3.9.1)
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.9.0)
92
- rspec-mocks (3.9.1)
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.9.0)
95
- rspec-support (3.9.2)
96
- rubocop (0.80.1)
97
- jaro_winkler (~> 1.5.1)
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.7.0.1)
113
+ parser (>= 3.2.2.4)
100
114
  rainbow (>= 2.2.2, < 4.0)
101
- rexml
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 (>= 1.4.0, < 1.7)
104
- ruby-progressbar (1.11.0)
105
- ruby2_keywords (0.0.2)
106
- sawyer (0.8.2)
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 (> 0.8, < 2.0)
109
- simplecov (0.21.2)
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.2)
115
- terminal-table (1.8.0)
116
- unicode-display_width (~> 1.1, >= 1.1.1)
117
- tzinfo (2.0.4)
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 (1.6.1)
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 (= 0.80.1)
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.4
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
@@ -16,3 +16,10 @@ require 'rubocop/rake_task'
16
16
  RuboCop::RakeTask.new(:rubocop)
17
17
 
18
18
  task default: %i[rubocop spec]
19
+
20
+ namespace :benchmark do
21
+ desc 'Run benchmark for the Ruby::Enum::Case'
22
+ task :case do
23
+ require_relative 'benchmarks/case'
24
+ end
25
+ end
@@ -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"