ruby-enum 0.8.0 → 0.9.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 +4 -4
- data/CHANGELOG.md +18 -10
- data/Dangerfile +4 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +89 -9
- data/LICENSE.md +1 -1
- data/README.md +143 -76
- data/RELEASING.md +4 -11
- data/UPGRADING.md +44 -0
- data/coverage/assets/0.12.3/DataTables-1.10.20/images/sort_asc.png +0 -0
- data/coverage/assets/0.12.3/DataTables-1.10.20/images/sort_asc_disabled.png +0 -0
- data/coverage/assets/0.12.3/DataTables-1.10.20/images/sort_both.png +0 -0
- data/coverage/assets/0.12.3/DataTables-1.10.20/images/sort_desc.png +0 -0
- data/coverage/assets/0.12.3/DataTables-1.10.20/images/sort_desc_disabled.png +0 -0
- data/coverage/assets/0.12.3/application.css +1 -0
- data/coverage/assets/0.12.3/application.js +7 -0
- data/coverage/assets/0.12.3/colorbox/border.png +0 -0
- data/coverage/assets/0.12.3/colorbox/controls.png +0 -0
- data/coverage/assets/0.12.3/colorbox/loading.gif +0 -0
- data/coverage/assets/0.12.3/colorbox/loading_background.png +0 -0
- data/coverage/assets/0.12.3/favicon_green.png +0 -0
- data/coverage/assets/0.12.3/favicon_red.png +0 -0
- data/coverage/assets/0.12.3/favicon_yellow.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/coverage/assets/0.12.3/images/ui-icons_222222_256x240.png +0 -0
- data/coverage/assets/0.12.3/images/ui-icons_2e83ff_256x240.png +0 -0
- data/coverage/assets/0.12.3/images/ui-icons_454545_256x240.png +0 -0
- data/coverage/assets/0.12.3/images/ui-icons_888888_256x240.png +0 -0
- data/coverage/assets/0.12.3/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/coverage/assets/0.12.3/loading.gif +0 -0
- data/coverage/assets/0.12.3/magnify.png +0 -0
- data/coverage/index.html +3841 -0
- data/lib/ruby-enum/enum.rb +8 -2
- data/lib/ruby-enum/errors/base.rb +1 -1
- data/lib/ruby-enum/version.rb +1 -1
- data/pkg/ruby-enum-0.8.0.gem +0 -0
- data/spec/ruby-enum/enum_spec.rb +37 -0
- data/spec/spec_helper.rb +4 -0
- metadata +39 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 26cee9efd34000cd0b004832d0eaae6ba5b90a3668c039b7490c3b7c0605e965
|
|
4
|
+
data.tar.gz: 4d5b7744d335dc558f3953d0b9ad27d38d44b7a40219cbd29ad3f360e7d54100
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 982371ddb8393adf84259e32f20df8e69de71c2084e5dcd49d52e30a3c17941a5321dcac5ecb6f2aa194486963037e61034609d05ec1c3e1681599fe59fdd936
|
|
7
|
+
data.tar.gz: a69948eefae40413871fc3f4c4d49b218428c86a13eeeaba256d903337fb7e1d972fb4689bd6bd8ab9c4c58ac44e414d49a4ee3ee2bdddaf9e23deed47fbcbc4
|
data/CHANGELOG.md
CHANGED
|
@@ -1,42 +1,50 @@
|
|
|
1
|
-
### 0.
|
|
1
|
+
### 0.9.0 (2021/01/21)
|
|
2
|
+
|
|
3
|
+
* [#34](https://github.com/dblock/ruby-enum/pull/34): Added support for Ruby 3.0 - [@dblock](https://github.com/dblock).
|
|
4
|
+
* [#29](https://github.com/dblock/ruby-enum/pull/29): Added superclass values when enumerating with `Ruby::Enum#values` - [@gi](https://github.com/gi).
|
|
5
|
+
* [#30](https://github.com/dblock/ruby-enum/pull/30): Default value to key - [@gi](https://github.com/gi).
|
|
6
|
+
* [#34](https://github.com/dblock/ruby-enum/pull/34): Replaced Travis-CI with Github Actions, added Danger PR linter - [@dblock](https://github.com/dblock).
|
|
7
|
+
* [#37](https://github.com/dblock/ruby-enum/pull/37): Added code coverage - [@dblock](https://github.com/dblock).
|
|
8
|
+
|
|
9
|
+
### 0.8.0 (2020/03/27)
|
|
2
10
|
|
|
3
11
|
* [#22](https://github.com/dblock/ruby-enum/pull/22): Added `Ruby::Enum#each_key` and `Ruby::Enum#each_value` - [@dblock](https://github.com/dblock).
|
|
4
12
|
* [#22](https://github.com/dblock/ruby-enum/pull/22): Dropped support for Ruby 2.2 - [@dblock](https://github.com/dblock).
|
|
5
13
|
* [#22](https://github.com/dblock/ruby-enum/pull/22): Upgraded RuboCop to 0.80.1 - [@dblock](https://github.com/dblock).
|
|
6
14
|
|
|
7
|
-
### 0.7.2 (2017/
|
|
15
|
+
### 0.7.2 (2017/03/15)
|
|
8
16
|
|
|
9
17
|
* [#18](https://github.com/dblock/ruby-enum/pull/18): Added support for non constant definition - [@laertispappas](https://github.com/laertispappas).
|
|
10
18
|
|
|
11
|
-
### 0.7.1 (2017/
|
|
19
|
+
### 0.7.1 (2017/02/23)
|
|
12
20
|
|
|
13
21
|
* [#16](https://github.com/dblock/ruby-enum/pull/16): Replaced `const_missing` with `const_set` - [@laertispappas](https://github.com/laertispappas).
|
|
14
22
|
|
|
15
|
-
### 0.7.0 (2017/
|
|
23
|
+
### 0.7.0 (2017/02/21)
|
|
16
24
|
|
|
17
25
|
* [#3](https://github.com/dblock/ruby-enum/pull/13): Added support for subclassing an Enum - [@laertispappas](https://github.com/laertispappas).
|
|
18
26
|
|
|
19
|
-
### 0.6.0 (2016/
|
|
27
|
+
### 0.6.0 (2016/05/12)
|
|
20
28
|
|
|
21
29
|
* [#12](https://github.com/dblock/ruby-enum/pull/12): A `Ruby::Enum::Errors::DuplicateKeyError` or a `Ruby::Enum::Errors::DuplciateKeyValyeError` will now be raised when duplicate keys / values are defined - [@laertispappas](https://github.com/laertispappas).
|
|
22
30
|
|
|
23
|
-
### 0.5.0 (2015/20
|
|
31
|
+
### 0.5.0 (2015/11/20)
|
|
24
32
|
|
|
25
33
|
* [#8](https://github.com/dblock/ruby-enum/pull/8): Added `Ruby::Enum#key`, `Ruby::Enum#value`, `Ruby::Enum#key?`, and `Ruby::Enum#value?` - [@dmolesUC3](https://github.com/dmolesUC3).
|
|
26
34
|
|
|
27
|
-
### 0.4.0 (2014/29
|
|
35
|
+
### 0.4.0 (2014/06/29)
|
|
28
36
|
|
|
29
37
|
* [#5](https://github.com/dblock/ruby-enum/pull/5): Mixed in `Enumerable` - [@kgann](https://github.com/kgann).
|
|
30
38
|
|
|
31
|
-
### 0.3.0 (2014/19
|
|
39
|
+
### 0.3.0 (2014/05/19)
|
|
32
40
|
|
|
33
41
|
* [#4](https://github.com/dblock/ruby-enum/pull/4): Added `Ruby::Enum#map` - [@ArnaudRinquin](https://github.com/ArnaudRinquin).
|
|
34
42
|
|
|
35
|
-
### 0.2.1 (2013/15
|
|
43
|
+
### 0.2.1 (2013/05/15)
|
|
36
44
|
|
|
37
45
|
* Added `Ruby::Enum#values`, `Ruby::Enum#keys` and `Ruby::Enum#to_h` - [@dblock](https://github.com/dblock).
|
|
38
46
|
* A `Ruby::Enum::Errors::UninitializedConstantError` will now be raised when referencing an undefined enum - [@dblock](https://github.com/dblock).
|
|
39
47
|
|
|
40
|
-
### 0.1.0 (2013/14
|
|
48
|
+
### 0.1.0 (2013/05/14)
|
|
41
49
|
|
|
42
50
|
* Initial public release, live-coded during [May 2013 NYC.rb](http://code.dblock.org/your-first-ruby-gem) - [@dblock](https://github.com/dblock).
|
data/Dangerfile
ADDED
data/Gemfile
CHANGED
|
@@ -7,6 +7,13 @@ gemspec
|
|
|
7
7
|
gem 'rake'
|
|
8
8
|
|
|
9
9
|
group :development, :test do
|
|
10
|
+
gem 'danger'
|
|
11
|
+
gem 'danger-changelog', '0.6.1'
|
|
12
|
+
gem 'danger-toc', '0.2.0'
|
|
10
13
|
gem 'rspec', '~> 3.0'
|
|
11
14
|
gem 'rubocop', '0.80.1'
|
|
12
15
|
end
|
|
16
|
+
|
|
17
|
+
group :test do
|
|
18
|
+
gem 'simplecov', require: false
|
|
19
|
+
end
|
data/Gemfile.lock
CHANGED
|
@@ -1,23 +1,84 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
ruby-enum (0.
|
|
4
|
+
ruby-enum (0.9.0)
|
|
5
5
|
i18n
|
|
6
6
|
|
|
7
7
|
GEM
|
|
8
8
|
remote: http://rubygems.org/
|
|
9
9
|
specs:
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
activesupport (6.1.0)
|
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
12
|
+
i18n (>= 1.6, < 2)
|
|
13
|
+
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)
|
|
20
|
+
claide-plugins (0.9.2)
|
|
21
|
+
cork
|
|
22
|
+
nap
|
|
23
|
+
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)
|
|
29
|
+
claide (~> 1.0)
|
|
30
|
+
claide-plugins (>= 0.9.2)
|
|
31
|
+
colored2 (~> 3.1)
|
|
32
|
+
cork (~> 0.1)
|
|
33
|
+
faraday (>= 0.9.0, < 2.0)
|
|
34
|
+
faraday-http-cache (~> 2.0)
|
|
35
|
+
git (~> 1.7)
|
|
36
|
+
kramdown (~> 2.3)
|
|
37
|
+
kramdown-parser-gfm (~> 1.0)
|
|
38
|
+
no_proxy_fix
|
|
39
|
+
octokit (~> 4.7)
|
|
40
|
+
terminal-table (~> 1)
|
|
41
|
+
danger-changelog (0.6.1)
|
|
42
|
+
danger-plugin-api (~> 1.0)
|
|
43
|
+
danger-plugin-api (1.0.0)
|
|
44
|
+
danger (> 2.0)
|
|
45
|
+
danger-toc (0.2.0)
|
|
46
|
+
activesupport
|
|
47
|
+
danger-plugin-api (~> 1.0)
|
|
48
|
+
kramdown
|
|
12
49
|
diff-lcs (1.3)
|
|
13
|
-
|
|
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)
|
|
56
|
+
faraday (>= 0.8)
|
|
57
|
+
faraday-net_http (1.0.0)
|
|
58
|
+
git (1.8.1)
|
|
59
|
+
rchardet (~> 1.8)
|
|
60
|
+
i18n (1.8.7)
|
|
14
61
|
concurrent-ruby (~> 1.0)
|
|
15
62
|
jaro_winkler (1.5.4)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
63
|
+
kramdown (2.3.0)
|
|
64
|
+
rexml
|
|
65
|
+
kramdown-parser-gfm (1.1.0)
|
|
66
|
+
kramdown (~> 2.0)
|
|
67
|
+
minitest (5.14.3)
|
|
68
|
+
multipart-post (2.1.1)
|
|
69
|
+
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)
|
|
74
|
+
open4 (1.3.4)
|
|
75
|
+
parallel (1.20.1)
|
|
76
|
+
parser (3.0.0.0)
|
|
77
|
+
ast (~> 2.4.1)
|
|
78
|
+
public_suffix (4.0.6)
|
|
19
79
|
rainbow (3.0.0)
|
|
20
80
|
rake (13.0.1)
|
|
81
|
+
rchardet (1.8.0)
|
|
21
82
|
rexml (3.2.4)
|
|
22
83
|
rspec (3.9.0)
|
|
23
84
|
rspec-core (~> 3.9.0)
|
|
@@ -40,17 +101,36 @@ GEM
|
|
|
40
101
|
rexml
|
|
41
102
|
ruby-progressbar (~> 1.7)
|
|
42
103
|
unicode-display_width (>= 1.4.0, < 1.7)
|
|
43
|
-
ruby-progressbar (1.
|
|
104
|
+
ruby-progressbar (1.11.0)
|
|
105
|
+
ruby2_keywords (0.0.2)
|
|
106
|
+
sawyer (0.8.2)
|
|
107
|
+
addressable (>= 2.3.5)
|
|
108
|
+
faraday (> 0.8, < 2.0)
|
|
109
|
+
simplecov (0.21.2)
|
|
110
|
+
docile (~> 1.1)
|
|
111
|
+
simplecov-html (~> 0.11)
|
|
112
|
+
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)
|
|
118
|
+
concurrent-ruby (~> 1.0)
|
|
44
119
|
unicode-display_width (1.6.1)
|
|
120
|
+
zeitwerk (2.4.2)
|
|
45
121
|
|
|
46
122
|
PLATFORMS
|
|
47
123
|
ruby
|
|
48
124
|
|
|
49
125
|
DEPENDENCIES
|
|
126
|
+
danger
|
|
127
|
+
danger-changelog (= 0.6.1)
|
|
128
|
+
danger-toc (= 0.2.0)
|
|
50
129
|
rake
|
|
51
130
|
rspec (~> 3.0)
|
|
52
131
|
rubocop (= 0.80.1)
|
|
53
132
|
ruby-enum!
|
|
133
|
+
simplecov
|
|
54
134
|
|
|
55
135
|
BUNDLED WITH
|
|
56
|
-
2.1.
|
|
136
|
+
2.1.4
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
|
@@ -2,117 +2,157 @@ Ruby::Enum
|
|
|
2
2
|
==========
|
|
3
3
|
|
|
4
4
|
[](http://badge.fury.io/rb/ruby-enum)
|
|
5
|
-
[](https://github.com/dblock/ruby-enum/actions)
|
|
6
6
|
[](https://codeclimate.com/github/dblock/ruby-enum)
|
|
7
7
|
|
|
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).
|
|
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
|
+
|
|
10
|
+
## Table of Contents
|
|
11
|
+
|
|
12
|
+
- [Usage](#usage)
|
|
13
|
+
- [Constants](#constants)
|
|
14
|
+
- [Class Methods](#class-methods)
|
|
15
|
+
- [Default Value](#default-value)
|
|
16
|
+
- [Enumerating](#enumerating)
|
|
17
|
+
- [Iterating](#iterating)
|
|
18
|
+
- [Mapping](#mapping)
|
|
19
|
+
- [Reducing](#reducing)
|
|
20
|
+
- [Sorting](#sorting)
|
|
21
|
+
- [Hashing](#hashing)
|
|
22
|
+
- [Retrieving keys and values](#retrieving-keys-and-values)
|
|
23
|
+
- [Mapping keys to values](#mapping-keys-to-values)
|
|
24
|
+
- [Mapping values to keys](#mapping-values-to-keys)
|
|
25
|
+
- [Duplicate enumerator keys or duplicate values](#duplicate-enumerator-keys-or-duplicate-values)
|
|
26
|
+
- [Inheritance](#inheritance)
|
|
27
|
+
- [Contributing](#contributing)
|
|
28
|
+
- [Copyright and License](#copyright-and-license)
|
|
29
|
+
- [Related Projects](#related-projects)
|
|
9
30
|
|
|
10
31
|
## Usage
|
|
11
32
|
|
|
12
|
-
Enums can be defined and accessed either as constants or class methods, which is a matter of preference.
|
|
33
|
+
Enums can be defined and accessed either as constants, or class methods, which is a matter of preference.
|
|
13
34
|
|
|
14
35
|
### Constants
|
|
15
36
|
|
|
16
|
-
Define enums and reference them as constants.
|
|
37
|
+
Define enums, and reference them as constants.
|
|
17
38
|
|
|
18
39
|
``` ruby
|
|
19
|
-
class
|
|
40
|
+
class OrderState
|
|
20
41
|
include Ruby::Enum
|
|
21
42
|
|
|
22
|
-
define :
|
|
23
|
-
define :
|
|
43
|
+
define :CREATED, 'created'
|
|
44
|
+
define :PAID, 'paid'
|
|
24
45
|
end
|
|
25
46
|
```
|
|
26
47
|
|
|
27
48
|
``` ruby
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
49
|
+
OrderState::CREATED # 'created'
|
|
50
|
+
OrderState::PAID # 'paid'
|
|
51
|
+
OrderState::UNKNOWN # raises Ruby::Enum::Errors::UninitializedConstantError
|
|
52
|
+
OrderState.keys # [ :CREATED, :PAID ]
|
|
53
|
+
OrderState.values # [ 'created', 'paid' ]
|
|
54
|
+
OrderState.to_h # { :CREATED => 'created', :PAID => 'paid' }
|
|
34
55
|
```
|
|
35
56
|
|
|
36
57
|
### Class Methods
|
|
37
58
|
|
|
38
|
-
Define enums reference them as class methods.
|
|
59
|
+
Define enums, and reference them as class methods.
|
|
39
60
|
|
|
40
61
|
``` ruby
|
|
41
62
|
class OrderState
|
|
42
63
|
include Ruby::Enum
|
|
43
64
|
|
|
44
|
-
define :created, '
|
|
45
|
-
define :paid, '
|
|
65
|
+
define :created, 'created'
|
|
66
|
+
define :paid, 'paid'
|
|
46
67
|
end
|
|
47
68
|
```
|
|
48
69
|
|
|
49
70
|
```ruby
|
|
50
|
-
OrderState.created #
|
|
51
|
-
OrderState.paid #
|
|
71
|
+
OrderState.created # 'created'
|
|
72
|
+
OrderState.paid # 'paid'
|
|
52
73
|
OrderState.undefined # NoMethodError is raised
|
|
53
74
|
OrderState.keys # [ :created, :paid ]
|
|
54
|
-
OrderState.values # [
|
|
55
|
-
OrderState.to_h # { :created => '
|
|
75
|
+
OrderState.values # ['created', 'paid']
|
|
76
|
+
OrderState.to_h # { :created => 'created', :paid => 'paid' }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Default Value
|
|
80
|
+
|
|
81
|
+
The value is optional. If unspecified, the value will default to the key.
|
|
82
|
+
|
|
83
|
+
``` ruby
|
|
84
|
+
class OrderState
|
|
85
|
+
include Ruby::Enum
|
|
86
|
+
|
|
87
|
+
define :UNSPECIFIED
|
|
88
|
+
define :unspecified
|
|
89
|
+
end
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
``` ruby
|
|
93
|
+
OrderState::UNSPECIFIED # :UNSPECIFIED
|
|
94
|
+
OrderState.unspecified # :unspecified
|
|
56
95
|
```
|
|
57
96
|
|
|
58
97
|
### Enumerating
|
|
59
98
|
|
|
60
|
-
|
|
99
|
+
Enums support all `Enumerable` methods.
|
|
61
100
|
|
|
62
101
|
#### Iterating
|
|
63
102
|
|
|
64
103
|
``` ruby
|
|
65
|
-
|
|
66
|
-
# key and enum.key
|
|
67
|
-
# enum.value is
|
|
104
|
+
OrderState.each do |key, enum|
|
|
105
|
+
# key and enum.key are :CREATED, :PAID
|
|
106
|
+
# enum.value is 'created', 'paid'
|
|
68
107
|
end
|
|
69
108
|
```
|
|
70
109
|
|
|
71
110
|
``` ruby
|
|
72
|
-
|
|
73
|
-
# :
|
|
111
|
+
OrderState.each_key do |key|
|
|
112
|
+
# :CREATED, :PAID
|
|
74
113
|
end
|
|
75
114
|
```
|
|
76
115
|
|
|
77
116
|
``` ruby
|
|
78
|
-
|
|
79
|
-
#
|
|
117
|
+
OrderState.each_value do |value|
|
|
118
|
+
# 'created', 'paid'
|
|
80
119
|
end
|
|
81
120
|
```
|
|
82
121
|
|
|
83
122
|
#### Mapping
|
|
84
123
|
|
|
85
124
|
``` ruby
|
|
86
|
-
|
|
87
|
-
# key and enum.key
|
|
88
|
-
# enum.value is
|
|
125
|
+
OrderState.map do |key, enum|
|
|
126
|
+
# key and enum.key are :CREATED, :PAID
|
|
127
|
+
# enum.value is 'created', 'paid'
|
|
89
128
|
[enum.value, key]
|
|
90
129
|
end
|
|
91
130
|
|
|
92
|
-
# => [ ['
|
|
131
|
+
# => [ ['created', :CREATED], ['paid', :PAID] ]
|
|
93
132
|
```
|
|
94
133
|
|
|
95
134
|
#### Reducing
|
|
96
135
|
|
|
97
136
|
``` ruby
|
|
98
|
-
|
|
99
|
-
# key and enum.key
|
|
100
|
-
# enum.value is
|
|
137
|
+
OrderState.reduce([]) do |arr, (key, enum)|
|
|
138
|
+
# key and enum.key are :CREATED, :PAID
|
|
139
|
+
# enum.value is 'created', 'paid'
|
|
101
140
|
arr << [enum.value, key]
|
|
102
141
|
end
|
|
103
142
|
|
|
104
|
-
# => [ ['
|
|
143
|
+
# => [ ['created', :CREATED], ['paid', :PAID] ]
|
|
105
144
|
```
|
|
106
145
|
|
|
107
146
|
#### Sorting
|
|
147
|
+
|
|
108
148
|
``` ruby
|
|
109
|
-
|
|
110
|
-
# key and enum.key
|
|
111
|
-
# enum.value is
|
|
112
|
-
enum.value
|
|
149
|
+
OrderState.sort_by do |key, enum|
|
|
150
|
+
# key and enum.key are :CREATED, :PAID
|
|
151
|
+
# enum.value is 'created', 'paid'
|
|
152
|
+
enum.value.length
|
|
113
153
|
end
|
|
114
154
|
|
|
115
|
-
# => [
|
|
155
|
+
# => [[:PAID, #<OrderState:0x0 @key=:PAID, @value="paid">], [:CREATED, #<OrderState:0x1 @key=:CREATED, @value="created">]]
|
|
116
156
|
```
|
|
117
157
|
|
|
118
158
|
### Hashing
|
|
@@ -122,83 +162,110 @@ Several hash-like methods are supported.
|
|
|
122
162
|
#### Retrieving keys and values
|
|
123
163
|
|
|
124
164
|
``` ruby
|
|
125
|
-
|
|
126
|
-
# => [:
|
|
165
|
+
OrderState.keys
|
|
166
|
+
# => [:CREATED, :PAID]
|
|
127
167
|
|
|
128
|
-
|
|
129
|
-
# => [
|
|
168
|
+
OrderState.values
|
|
169
|
+
# => ['created', 'paid']
|
|
130
170
|
```
|
|
131
171
|
|
|
132
172
|
#### Mapping keys to values
|
|
133
173
|
|
|
134
174
|
``` ruby
|
|
135
|
-
|
|
175
|
+
OrderState.key?(:CREATED)
|
|
136
176
|
# => true
|
|
137
177
|
|
|
138
|
-
|
|
139
|
-
# =>
|
|
178
|
+
OrderState.value(:CREATED)
|
|
179
|
+
# => 'created'
|
|
140
180
|
|
|
141
|
-
|
|
181
|
+
OrderState.key?(:FAILED)
|
|
142
182
|
# => false
|
|
143
183
|
|
|
144
|
-
|
|
184
|
+
OrderState.value(:FAILED)
|
|
145
185
|
# => nil
|
|
146
186
|
```
|
|
147
187
|
|
|
148
188
|
#### Mapping values to keys
|
|
149
189
|
|
|
150
190
|
``` ruby
|
|
151
|
-
|
|
191
|
+
OrderState.value?('paid')
|
|
152
192
|
# => true
|
|
153
193
|
|
|
154
|
-
|
|
155
|
-
# => :
|
|
194
|
+
OrderState.key('paid')
|
|
195
|
+
# => :PAID
|
|
156
196
|
|
|
157
|
-
|
|
197
|
+
OrderState.value?('failed')
|
|
158
198
|
# => false
|
|
159
199
|
|
|
160
|
-
|
|
200
|
+
OrderState.key('failed')
|
|
161
201
|
# => nil
|
|
162
202
|
```
|
|
163
203
|
|
|
164
204
|
### Duplicate enumerator keys or duplicate values
|
|
165
205
|
|
|
166
|
-
Defining duplicate enums
|
|
167
|
-
value is not allowed. Defining a duplicate value will raise a `Ruby::Enum::Errors::DuplicateValueError`.
|
|
168
|
-
The following declarations will both raise an exception:
|
|
206
|
+
Defining duplicate enums raises `Ruby::Enum::Errors::DuplicateKeyError`.
|
|
169
207
|
|
|
170
208
|
```ruby
|
|
171
|
-
|
|
172
|
-
|
|
209
|
+
class OrderState
|
|
210
|
+
include Ruby::Enum
|
|
173
211
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
212
|
+
define :CREATED, 'created'
|
|
213
|
+
define :CREATED, 'recreated' # raises DuplicateKeyError
|
|
214
|
+
end
|
|
215
|
+
```
|
|
177
216
|
|
|
178
|
-
|
|
179
|
-
class Colors
|
|
180
|
-
include Ruby::Enum
|
|
217
|
+
Defining a duplicate value raises `Ruby::Enum::Errors::DuplicateValueError`.
|
|
181
218
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
219
|
+
```ruby
|
|
220
|
+
class OrderState
|
|
221
|
+
include Ruby::Enum
|
|
222
|
+
|
|
223
|
+
define :CREATED, 'created'
|
|
224
|
+
define :RECREATED, 'created' # raises DuplicateValueError
|
|
225
|
+
end
|
|
185
226
|
```
|
|
186
227
|
|
|
187
|
-
The `DuplicateValueError` exception is
|
|
188
|
-
|
|
228
|
+
The `DuplicateValueError` exception is raised to be consistent with the unique key constraint. Since keys are unique, there needs to be a way to map values to keys using `OrderState.value('created')`.
|
|
229
|
+
|
|
230
|
+
### Inheritance
|
|
231
|
+
|
|
232
|
+
When inheriting from a `Ruby::Enum` class, all defined enums in the parent class will be accessible in sub classes as well. Sub classes can also provide extra enums, as usual.
|
|
189
233
|
|
|
190
|
-
|
|
234
|
+
``` ruby
|
|
235
|
+
class OrderState
|
|
236
|
+
include Ruby::Enum
|
|
237
|
+
|
|
238
|
+
define :CREATED, 'CREATED'
|
|
239
|
+
define :PAID, 'PAID'
|
|
240
|
+
end
|
|
191
241
|
|
|
192
|
-
|
|
193
|
-
|
|
242
|
+
class ShippedOrderState < OrderState
|
|
243
|
+
define :PREPARED, 'PREPARED'
|
|
244
|
+
define :SHIPPED, 'SHIPPED'
|
|
245
|
+
end
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
``` ruby
|
|
249
|
+
ShippedOrderState::CREATED # 'CREATED'
|
|
250
|
+
ShippedOrderState::PAID # 'PAID'
|
|
251
|
+
ShippedOrderState::PREPARED # 'PREPARED'
|
|
252
|
+
ShippedOrderState::SHIPPED # 'SHIPPED'
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
The `values` class method will enumerate the values from all base classes.
|
|
256
|
+
|
|
257
|
+
``` ruby
|
|
258
|
+
OrderState.values # ['CREATED', 'PAID']
|
|
259
|
+
ShippedOrderState.values # ['CREATED', 'PAID', 'PREPARED', SHIPPED']
|
|
260
|
+
```
|
|
194
261
|
|
|
195
262
|
## Contributing
|
|
196
263
|
|
|
197
|
-
You're encouraged to contribute to
|
|
264
|
+
You're encouraged to contribute to ruby-enum. See [CONTRIBUTING](CONTRIBUTING.md) for details.
|
|
198
265
|
|
|
199
266
|
## Copyright and License
|
|
200
267
|
|
|
201
|
-
Copyright (c) 2013-
|
|
268
|
+
Copyright (c) 2013-2021, Daniel Doubrovkine and [Contributors](CHANGELOG.md).
|
|
202
269
|
|
|
203
270
|
This project is licensed under the [MIT License](LICENSE.md).
|
|
204
271
|
|