ruby-enum 0.9.0 → 1.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.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +14 -0
  3. data/Dangerfile +2 -0
  4. data/Gemfile +8 -5
  5. data/Gemfile.lock +147 -82
  6. data/README.md +61 -2
  7. data/Rakefile +7 -0
  8. data/benchmarks/case.rb +45 -0
  9. data/coverage/assets/0.13.2/DataTables-1.10.20/images/sort_asc.png +0 -0
  10. data/coverage/assets/0.13.2/DataTables-1.10.20/images/sort_asc_disabled.png +0 -0
  11. data/coverage/assets/0.13.2/DataTables-1.10.20/images/sort_both.png +0 -0
  12. data/coverage/assets/0.13.2/DataTables-1.10.20/images/sort_desc.png +0 -0
  13. data/coverage/assets/0.13.2/DataTables-1.10.20/images/sort_desc_disabled.png +0 -0
  14. data/coverage/assets/0.13.2/application.css +1 -0
  15. data/coverage/assets/0.13.2/application.js +7 -0
  16. data/coverage/assets/0.13.2/colorbox/border.png +0 -0
  17. data/coverage/assets/0.13.2/colorbox/controls.png +0 -0
  18. data/coverage/assets/0.13.2/colorbox/loading.gif +0 -0
  19. data/coverage/assets/0.13.2/colorbox/loading_background.png +0 -0
  20. data/coverage/assets/0.13.2/favicon_green.png +0 -0
  21. data/coverage/assets/0.13.2/favicon_red.png +0 -0
  22. data/coverage/assets/0.13.2/favicon_yellow.png +0 -0
  23. data/coverage/assets/0.13.2/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  24. data/coverage/assets/0.13.2/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  25. data/coverage/assets/0.13.2/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  26. data/coverage/assets/0.13.2/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  27. data/coverage/assets/0.13.2/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  28. data/coverage/assets/0.13.2/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  29. data/coverage/assets/0.13.2/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  30. data/coverage/assets/0.13.2/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  31. data/coverage/assets/0.13.2/images/ui-icons_222222_256x240.png +0 -0
  32. data/coverage/assets/0.13.2/images/ui-icons_2e83ff_256x240.png +0 -0
  33. data/coverage/assets/0.13.2/images/ui-icons_454545_256x240.png +0 -0
  34. data/coverage/assets/0.13.2/images/ui-icons_888888_256x240.png +0 -0
  35. data/coverage/assets/0.13.2/images/ui-icons_cd0a0a_256x240.png +0 -0
  36. data/coverage/assets/0.13.2/loading.gif +0 -0
  37. data/coverage/assets/0.13.2/magnify.png +0 -0
  38. data/coverage/index.html +5768 -973
  39. data/lib/ruby-enum/enum/case.rb +84 -0
  40. data/lib/ruby-enum/enum/i18n_mock.rb +19 -0
  41. data/lib/ruby-enum/enum.rb +9 -3
  42. data/lib/ruby-enum/errors/base.rb +4 -4
  43. data/lib/ruby-enum/version.rb +1 -1
  44. data/lib/ruby-enum.rb +15 -3
  45. data/pkg/ruby-enum-0.9.0.gem +0 -0
  46. data/ruby-enum.gemspec +2 -1
  47. data/spec/ruby-enum/enum/case_spec.rb +119 -0
  48. data/spec/ruby-enum/enum_spec.rb +201 -71
  49. data/spec/spec_helper.rb +2 -2
  50. data/spec_i18n/Gemfile +9 -0
  51. data/spec_i18n/Rakefile +12 -0
  52. data/spec_i18n/spec/i18n_spec.rb +50 -0
  53. data/spec_i18n/spec/spec_helper.rb +8 -0
  54. metadata +45 -20
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26cee9efd34000cd0b004832d0eaae6ba5b90a3668c039b7490c3b7c0605e965
4
- data.tar.gz: 4d5b7744d335dc558f3953d0b9ad27d38d44b7a40219cbd29ad3f360e7d54100
3
+ metadata.gz: 0ef6a193362634cd178978189b5e5a09fafe68ad2428c1d646abf33ef6f66d4a
4
+ data.tar.gz: 182bdfaff04b1b241bac9614722a3010d2613e2ff8b0cb7a603053df6796b664
5
5
  SHA512:
6
- metadata.gz: 982371ddb8393adf84259e32f20df8e69de71c2084e5dcd49d52e30a3c17941a5321dcac5ecb6f2aa194486963037e61034609d05ec1c3e1681599fe59fdd936
7
- data.tar.gz: a69948eefae40413871fc3f4c4d49b218428c86a13eeeaba256d903337fb7e1d972fb4689bd6bd8ab9c4c58ac44e414d49a4ee3ee2bdddaf9e23deed47fbcbc4
6
+ metadata.gz: 40d0b2b8a93150ec1f21ef209e020dcb6a75142eaba2fd989b9f0ac6853953ec5a6ec0b9a356f8d1411e2ffb6ab955acb453cb576ba841cad3ec7eea6ffebac5
7
+ data.tar.gz: 65fe31ba27ba5f9c76d95090d00d8024fb0ba1f7abb8bd5e5bff87ea7559f941ec67a35a655f5cfe39b1e68e89de043d0702bd572ed18465483c1c4bc36fe6dc
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ### 1.1.0 (2026/6/20)
2
+
3
+ * [#56](https://github.com/dblock/ruby-enum/pull/56): Fix `DuplicateKeyError` when the enum class is reloaded - [@flvrone](https://github.com/flvrone).
4
+ * [#54](https://github.com/dblock/ruby-enum/pull/54): Add support for Ruby 4.0 - [@dblock](https://github.com/dblock).
5
+ * [#53](https://github.com/dblock/ruby-enum/pull/53): Replace code climate with coveralls - [@dblock](https://github.com/dblock).
6
+
7
+ ### 1.0.0 (2023/01/10)
8
+
9
+ * [#41](https://github.com/dblock/ruby-enum/pull/41): Make i18n dependency optional - [@peterfication](https://github.com/peterfication).
10
+ * [#43](https://github.com/dblock/ruby-enum/pull/43): Add exhaustive case matcher - [@peterfication](https://github.com/peterfication).
11
+ * [#40](https://github.com/dblock/ruby-enum/pull/39): Enable new Rubocop cops and address/allowlist lints - [@petergoldstein](https://github.com/petergoldstein).
12
+ * [#39](https://github.com/dblock/ruby-enum/pull/39): Require Ruby >= 2.7 - [@petergoldstein](https://github.com/petergoldstein).
13
+ * [#38](https://github.com/dblock/ruby-enum/pull/38): Ensure Ruby >= 2.3 - [@ojab](https://github.com/ojab).
14
+
1
15
  ### 0.9.0 (2021/01/21)
2
16
 
3
17
  * [#34](https://github.com/dblock/ruby-enum/pull/34): Added support for Ruby 3.0 - [@dblock](https://github.com/dblock).
data/Dangerfile CHANGED
@@ -1,4 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ danger.import_dangerfile(gem: 'danger-pr-comment')
4
+
3
5
  changelog.check!
4
6
  toc.check!
data/Gemfile CHANGED
@@ -8,12 +8,15 @@ gem 'rake'
8
8
 
9
9
  group :development, :test do
10
10
  gem 'danger'
11
- gem 'danger-changelog', '0.6.1'
12
- gem 'danger-toc', '0.2.0'
13
- gem 'rspec', '~> 3.0'
14
- gem 'rubocop', '0.80.1'
11
+ gem 'danger-changelog'
12
+ gem 'danger-pr-comment'
13
+ gem 'danger-toc'
14
+ gem 'rspec'
15
+ gem 'rubocop', '1.82.1'
16
+ gem 'rubocop-rake'
17
+ gem 'rubocop-rspec'
15
18
  end
16
19
 
17
20
  group :test do
18
- gem 'simplecov', require: false
21
+ gem 'coveralls_reborn', require: false
19
22
  end
data/Gemfile.lock CHANGED
@@ -1,136 +1,201 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-enum (0.9.0)
5
- i18n
4
+ ruby-enum (1.1.0)
6
5
 
7
6
  GEM
8
7
  remote: http://rubygems.org/
9
8
  specs:
10
- activesupport (6.1.0)
11
- concurrent-ruby (~> 1.0, >= 1.0.2)
9
+ activesupport (8.1.1)
10
+ base64
11
+ bigdecimal
12
+ concurrent-ruby (~> 1.0, >= 1.3.1)
13
+ connection_pool (>= 2.2.5)
14
+ drb
12
15
  i18n (>= 1.6, < 2)
16
+ json
17
+ logger (>= 1.4.2)
13
18
  minitest (>= 5.1)
14
- tzinfo (~> 2.0)
15
- zeitwerk (~> 2.3)
16
- addressable (2.7.0)
17
- public_suffix (>= 2.0.2, < 5.0)
18
- ast (2.4.2)
19
- claide (1.0.3)
19
+ securerandom (>= 0.3)
20
+ tzinfo (~> 2.0, >= 2.0.5)
21
+ uri (>= 0.13.1)
22
+ addressable (2.8.8)
23
+ public_suffix (>= 2.0.2, < 8.0)
24
+ ast (2.4.3)
25
+ base64 (0.3.0)
26
+ bigdecimal (4.0.1)
27
+ claide (1.1.0)
20
28
  claide-plugins (0.9.2)
21
29
  cork
22
30
  nap
23
31
  open4 (~> 1.3)
24
- colored2 (3.1.2)
25
- concurrent-ruby (1.1.7)
26
- cork (0.3.0)
27
- colored2 (~> 3.1)
28
- danger (8.2.1)
32
+ colored (1.2)
33
+ colored2 (4.0.3)
34
+ concurrent-ruby (1.3.6)
35
+ connection_pool (3.0.2)
36
+ cork (0.2.0)
37
+ colored (~> 1.2)
38
+ coveralls_reborn (0.29.0)
39
+ simplecov (~> 0.22.0)
40
+ term-ansicolor (~> 1.7)
41
+ thor (~> 1.2)
42
+ tins (~> 1.32)
43
+ danger (9.5.3)
44
+ base64 (~> 0.2)
29
45
  claide (~> 1.0)
30
46
  claide-plugins (>= 0.9.2)
31
- colored2 (~> 3.1)
47
+ colored2 (>= 3.1, < 5)
32
48
  cork (~> 0.1)
33
- faraday (>= 0.9.0, < 2.0)
49
+ faraday (>= 0.9.0, < 3.0)
34
50
  faraday-http-cache (~> 2.0)
35
- git (~> 1.7)
36
- kramdown (~> 2.3)
51
+ git (>= 1.13, < 3.0)
52
+ kramdown (>= 2.5.1, < 3.0)
37
53
  kramdown-parser-gfm (~> 1.0)
38
- no_proxy_fix
39
- octokit (~> 4.7)
40
- terminal-table (~> 1)
41
- danger-changelog (0.6.1)
54
+ octokit (>= 4.0)
55
+ pstore (~> 0.1)
56
+ terminal-table (>= 1, < 5)
57
+ danger-changelog (0.8.0)
42
58
  danger-plugin-api (~> 1.0)
43
59
  danger-plugin-api (1.0.0)
44
60
  danger (> 2.0)
61
+ danger-pr-comment (0.1.0)
62
+ danger (~> 9)
45
63
  danger-toc (0.2.0)
46
64
  activesupport
47
65
  danger-plugin-api (~> 1.0)
48
66
  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)
54
- ruby2_keywords
55
- faraday-http-cache (2.2.0)
67
+ diff-lcs (1.6.2)
68
+ docile (1.4.1)
69
+ drb (2.2.3)
70
+ faraday (2.14.0)
71
+ faraday-net_http (>= 2.0, < 3.5)
72
+ json
73
+ logger
74
+ faraday-http-cache (2.5.1)
56
75
  faraday (>= 0.8)
57
- faraday-net_http (1.0.0)
58
- git (1.8.1)
76
+ faraday-net_http (3.4.2)
77
+ net-http (~> 0.5)
78
+ git (2.3.3)
79
+ activesupport (>= 5.0)
80
+ addressable (~> 2.8)
81
+ process_executer (~> 1.1)
59
82
  rchardet (~> 1.8)
60
- i18n (1.8.7)
83
+ i18n (1.14.8)
61
84
  concurrent-ruby (~> 1.0)
62
- jaro_winkler (1.5.4)
63
- kramdown (2.3.0)
64
- rexml
85
+ io-console (0.8.2)
86
+ json (2.18.0)
87
+ kramdown (2.5.1)
88
+ rexml (>= 3.3.9)
65
89
  kramdown-parser-gfm (1.1.0)
66
90
  kramdown (~> 2.0)
67
- minitest (5.14.3)
68
- multipart-post (2.1.1)
91
+ language_server-protocol (3.17.0.5)
92
+ lint_roller (1.1.0)
93
+ logger (1.7.0)
94
+ minitest (6.0.1)
95
+ prism (~> 1.5)
96
+ mize (0.6.1)
69
97
  nap (1.1.0)
70
- no_proxy_fix (0.1.2)
71
- octokit (4.20.0)
72
- faraday (>= 0.9)
73
- sawyer (~> 0.8.0, >= 0.5.3)
98
+ net-http (0.9.1)
99
+ uri (>= 0.11.1)
100
+ octokit (10.0.0)
101
+ faraday (>= 1, < 3)
102
+ sawyer (~> 0.9)
74
103
  open4 (1.3.4)
75
- parallel (1.20.1)
76
- parser (3.0.0.0)
104
+ parallel (1.27.0)
105
+ parser (3.3.10.0)
77
106
  ast (~> 2.4.1)
78
- public_suffix (4.0.6)
79
- rainbow (3.0.0)
80
- rake (13.0.1)
81
- 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)
107
+ racc
108
+ prism (1.7.0)
109
+ process_executer (1.3.0)
110
+ pstore (0.2.0)
111
+ public_suffix (7.0.0)
112
+ racc (1.8.1)
113
+ rainbow (3.1.1)
114
+ rake (13.3.1)
115
+ rchardet (1.10.0)
116
+ readline (0.0.4)
117
+ reline
118
+ regexp_parser (2.11.3)
119
+ reline (0.6.3)
120
+ io-console (~> 0.5)
121
+ rexml (3.4.4)
122
+ rspec (3.13.2)
123
+ rspec-core (~> 3.13.0)
124
+ rspec-expectations (~> 3.13.0)
125
+ rspec-mocks (~> 3.13.0)
126
+ rspec-core (3.13.6)
127
+ rspec-support (~> 3.13.0)
128
+ rspec-expectations (3.13.5)
90
129
  diff-lcs (>= 1.2.0, < 2.0)
91
- rspec-support (~> 3.9.0)
92
- rspec-mocks (3.9.1)
130
+ rspec-support (~> 3.13.0)
131
+ rspec-mocks (3.13.7)
93
132
  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)
133
+ rspec-support (~> 3.13.0)
134
+ rspec-support (3.13.6)
135
+ rubocop (1.82.1)
136
+ json (~> 2.3)
137
+ language_server-protocol (~> 3.17.0.2)
138
+ lint_roller (~> 1.1.0)
98
139
  parallel (~> 1.10)
99
- parser (>= 2.7.0.1)
140
+ parser (>= 3.3.0.2)
100
141
  rainbow (>= 2.2.2, < 4.0)
101
- rexml
142
+ regexp_parser (>= 2.9.3, < 3.0)
143
+ rubocop-ast (>= 1.48.0, < 2.0)
102
144
  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)
145
+ unicode-display_width (>= 2.4.0, < 4.0)
146
+ rubocop-ast (1.49.0)
147
+ parser (>= 3.3.7.2)
148
+ prism (~> 1.7)
149
+ rubocop-rake (0.7.1)
150
+ lint_roller (~> 1.1)
151
+ rubocop (>= 1.72.1)
152
+ rubocop-rspec (3.8.0)
153
+ lint_roller (~> 1.1)
154
+ rubocop (~> 1.81)
155
+ ruby-progressbar (1.13.0)
156
+ sawyer (0.9.3)
107
157
  addressable (>= 2.3.5)
108
- faraday (> 0.8, < 2.0)
109
- simplecov (0.21.2)
158
+ faraday (>= 0.17.3, < 3)
159
+ securerandom (0.4.1)
160
+ simplecov (0.22.0)
110
161
  docile (~> 1.1)
111
162
  simplecov-html (~> 0.11)
112
163
  simplecov_json_formatter (~> 0.1)
113
- 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)
164
+ simplecov-html (0.13.2)
165
+ simplecov_json_formatter (0.1.4)
166
+ sync (0.5.0)
167
+ term-ansicolor (1.11.3)
168
+ tins (~> 1)
169
+ terminal-table (4.0.0)
170
+ unicode-display_width (>= 1.1.1, < 4)
171
+ thor (1.4.0)
172
+ tins (1.51.0)
173
+ bigdecimal
174
+ mize (~> 0.6)
175
+ readline
176
+ sync
177
+ tzinfo (2.0.6)
118
178
  concurrent-ruby (~> 1.0)
119
- unicode-display_width (1.6.1)
120
- zeitwerk (2.4.2)
179
+ unicode-display_width (3.2.0)
180
+ unicode-emoji (~> 4.1)
181
+ unicode-emoji (4.2.0)
182
+ uri (1.1.1)
121
183
 
122
184
  PLATFORMS
123
185
  ruby
124
186
 
125
187
  DEPENDENCIES
188
+ coveralls_reborn
126
189
  danger
127
- danger-changelog (= 0.6.1)
128
- danger-toc (= 0.2.0)
190
+ danger-changelog
191
+ danger-pr-comment
192
+ danger-toc
129
193
  rake
130
- rspec (~> 3.0)
131
- rubocop (= 0.80.1)
194
+ rspec
195
+ rubocop (= 1.82.1)
196
+ rubocop-rake
197
+ rubocop-rspec
132
198
  ruby-enum!
133
- simplecov
134
199
 
135
200
  BUNDLED WITH
136
201
  2.1.4
data/README.md CHANGED
@@ -2,8 +2,8 @@ Ruby::Enum
2
2
  ==========
3
3
 
4
4
  [![Gem Version](http://img.shields.io/gem/v/ruby-enum.svg)](http://badge.fury.io/rb/ruby-enum)
5
- [![Build Status](https://github.com/dblock/ruby-enum/workflows/test/badge.svg?branch=master)](https://github.com/dblock/ruby-enum/actions)
6
- [![Code Climate](https://codeclimate.com/github/dblock/ruby-enum.svg)](https://codeclimate.com/github/dblock/ruby-enum)
5
+ [![Test](https://github.com/dblock/ruby-enum/actions/workflows/test.yml/badge.svg)](https://github.com/dblock/ruby-enum/actions/workflows/test.yml)
6
+ [![Coverage Status](https://coveralls.io/repos/github/dblock/ruby-enum/badge.svg?branch=master)](https://coveralls.io/github/dblock/ruby-enum?branch=master)
7
7
 
8
8
  Enum-like behavior for Ruby, heavily inspired by [this](http://www.rubyfleebie.com/enumerations-and-ruby), and improved upon [another blog post](http://code.dblock.org/how-to-define-enums-in-ruby).
9
9
 
@@ -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"