ruby-enum 0.6.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 +5 -5
- data/CHANGELOG.md +32 -6
- data/Dangerfile +4 -0
- data/Gemfile +14 -2
- data/Gemfile.lock +116 -33
- data/LICENSE.md +1 -1
- data/README.md +176 -64
- data/RELEASING.md +5 -12
- data/Rakefile +3 -1
- 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.rb +2 -0
- data/lib/ruby-enum/enum.rb +59 -21
- data/lib/ruby-enum/errors/base.rb +3 -1
- data/lib/ruby-enum/errors/duplicate_key_error.rb +2 -0
- data/lib/ruby-enum/errors/duplicate_value_error.rb +2 -0
- data/lib/ruby-enum/errors/uninitialized_constant_error.rb +2 -0
- data/lib/ruby-enum/version.rb +3 -1
- data/lib/ruby_enum.rb +2 -0
- data/pkg/ruby-enum-0.8.0.gem +0 -0
- data/ruby-enum.gemspec +3 -1
- data/spec/ruby-enum/enum_spec.rb +125 -9
- data/spec/ruby-enum/version_spec.rb +2 -0
- data/spec/spec_helper.rb +6 -0
- metadata +40 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
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,24 +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)
|
10
|
+
|
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).
|
12
|
+
* [#22](https://github.com/dblock/ruby-enum/pull/22): Dropped support for Ruby 2.2 - [@dblock](https://github.com/dblock).
|
13
|
+
* [#22](https://github.com/dblock/ruby-enum/pull/22): Upgraded RuboCop to 0.80.1 - [@dblock](https://github.com/dblock).
|
14
|
+
|
15
|
+
### 0.7.2 (2017/03/15)
|
16
|
+
|
17
|
+
* [#18](https://github.com/dblock/ruby-enum/pull/18): Added support for non constant definition - [@laertispappas](https://github.com/laertispappas).
|
18
|
+
|
19
|
+
### 0.7.1 (2017/02/23)
|
20
|
+
|
21
|
+
* [#16](https://github.com/dblock/ruby-enum/pull/16): Replaced `const_missing` with `const_set` - [@laertispappas](https://github.com/laertispappas).
|
22
|
+
|
23
|
+
### 0.7.0 (2017/02/21)
|
24
|
+
|
25
|
+
* [#3](https://github.com/dblock/ruby-enum/pull/13): Added support for subclassing an Enum - [@laertispappas](https://github.com/laertispappas).
|
26
|
+
|
27
|
+
### 0.6.0 (2016/05/12)
|
2
28
|
|
3
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).
|
4
30
|
|
5
|
-
### 0.5.0 (11/20
|
31
|
+
### 0.5.0 (2015/11/20)
|
6
32
|
|
7
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).
|
8
34
|
|
9
|
-
### 0.4.0 (
|
35
|
+
### 0.4.0 (2014/06/29)
|
10
36
|
|
11
37
|
* [#5](https://github.com/dblock/ruby-enum/pull/5): Mixed in `Enumerable` - [@kgann](https://github.com/kgann).
|
12
38
|
|
13
|
-
### 0.3.0 (
|
39
|
+
### 0.3.0 (2014/05/19)
|
14
40
|
|
15
41
|
* [#4](https://github.com/dblock/ruby-enum/pull/4): Added `Ruby::Enum#map` - [@ArnaudRinquin](https://github.com/ArnaudRinquin).
|
16
42
|
|
17
|
-
### 0.2.1 (
|
43
|
+
### 0.2.1 (2013/05/15)
|
18
44
|
|
19
45
|
* Added `Ruby::Enum#values`, `Ruby::Enum#keys` and `Ruby::Enum#to_h` - [@dblock](https://github.com/dblock).
|
20
46
|
* A `Ruby::Enum::Errors::UninitializedConstantError` will now be raised when referencing an undefined enum - [@dblock](https://github.com/dblock).
|
21
47
|
|
22
|
-
### 0.1.0 (
|
48
|
+
### 0.1.0 (2013/05/14)
|
23
49
|
|
24
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
@@ -1,7 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source 'http://rubygems.org'
|
2
4
|
|
3
5
|
gemspec
|
4
6
|
|
5
|
-
gem 'rspec', '~> 3.4.0'
|
6
7
|
gem 'rake'
|
7
|
-
|
8
|
+
|
9
|
+
group :development, :test do
|
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'
|
15
|
+
end
|
16
|
+
|
17
|
+
group :test do
|
18
|
+
gem 'simplecov', require: false
|
19
|
+
end
|
data/Gemfile.lock
CHANGED
@@ -1,53 +1,136 @@
|
|
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
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
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)
|
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)
|
61
|
+
concurrent-ruby (~> 1.0)
|
62
|
+
jaro_winkler (1.5.4)
|
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)
|
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)
|
27
90
|
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
-
rspec-support (~> 3.
|
29
|
-
rspec-mocks (3.
|
91
|
+
rspec-support (~> 3.9.0)
|
92
|
+
rspec-mocks (3.9.1)
|
30
93
|
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
-
rspec-support (~> 3.
|
32
|
-
rspec-support (3.
|
33
|
-
rubocop (0.
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
rainbow (>=
|
94
|
+
rspec-support (~> 3.9.0)
|
95
|
+
rspec-support (3.9.2)
|
96
|
+
rubocop (0.80.1)
|
97
|
+
jaro_winkler (~> 1.5.1)
|
98
|
+
parallel (~> 1.10)
|
99
|
+
parser (>= 2.7.0.1)
|
100
|
+
rainbow (>= 2.2.2, < 4.0)
|
101
|
+
rexml
|
38
102
|
ruby-progressbar (~> 1.7)
|
39
|
-
|
40
|
-
ruby-progressbar (1.
|
41
|
-
|
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)
|
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)
|
119
|
+
unicode-display_width (1.6.1)
|
120
|
+
zeitwerk (2.4.2)
|
42
121
|
|
43
122
|
PLATFORMS
|
44
123
|
ruby
|
45
124
|
|
46
125
|
DEPENDENCIES
|
126
|
+
danger
|
127
|
+
danger-changelog (= 0.6.1)
|
128
|
+
danger-toc (= 0.2.0)
|
47
129
|
rake
|
48
|
-
rspec (~> 3.
|
49
|
-
rubocop (= 0.
|
130
|
+
rspec (~> 3.0)
|
131
|
+
rubocop (= 0.80.1)
|
50
132
|
ruby-enum!
|
133
|
+
simplecov
|
51
134
|
|
52
135
|
BUNDLED WITH
|
53
|
-
1.
|
136
|
+
2.1.4
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -2,158 +2,270 @@ Ruby::Enum
|
|
2
2
|
==========
|
3
3
|
|
4
4
|
[](http://badge.fury.io/rb/ruby-enum)
|
5
|
-
[](https://gemnasium.com/dblock/ruby-enum)
|
5
|
+
[](https://github.com/dblock/ruby-enum/actions)
|
7
6
|
[](https://codeclimate.com/github/dblock/ruby-enum)
|
8
7
|
|
9
|
-
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)
|
10
30
|
|
11
31
|
## Usage
|
12
32
|
|
33
|
+
Enums can be defined and accessed either as constants, or class methods, which is a matter of preference.
|
34
|
+
|
35
|
+
### Constants
|
36
|
+
|
37
|
+
Define enums, and reference them as constants.
|
38
|
+
|
13
39
|
``` ruby
|
14
|
-
class
|
40
|
+
class OrderState
|
15
41
|
include Ruby::Enum
|
16
42
|
|
17
|
-
define :
|
18
|
-
define :
|
43
|
+
define :CREATED, 'created'
|
44
|
+
define :PAID, 'paid'
|
19
45
|
end
|
20
46
|
```
|
21
47
|
|
22
|
-
|
48
|
+
``` ruby
|
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' }
|
55
|
+
```
|
56
|
+
|
57
|
+
### Class Methods
|
58
|
+
|
59
|
+
Define enums, and reference them as class methods.
|
23
60
|
|
24
61
|
``` ruby
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
62
|
+
class OrderState
|
63
|
+
include Ruby::Enum
|
64
|
+
|
65
|
+
define :created, 'created'
|
66
|
+
define :paid, 'paid'
|
67
|
+
end
|
31
68
|
```
|
32
69
|
|
33
|
-
|
70
|
+
```ruby
|
71
|
+
OrderState.created # 'created'
|
72
|
+
OrderState.paid # 'paid'
|
73
|
+
OrderState.undefined # NoMethodError is raised
|
74
|
+
OrderState.keys # [ :created, :paid ]
|
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
|
95
|
+
```
|
96
|
+
|
97
|
+
### Enumerating
|
98
|
+
|
99
|
+
Enums support all `Enumerable` methods.
|
34
100
|
|
35
101
|
#### Iterating
|
36
102
|
|
37
103
|
``` ruby
|
38
|
-
|
39
|
-
# key and enum.key
|
40
|
-
# enum.value is
|
104
|
+
OrderState.each do |key, enum|
|
105
|
+
# key and enum.key are :CREATED, :PAID
|
106
|
+
# enum.value is 'created', 'paid'
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
``` ruby
|
111
|
+
OrderState.each_key do |key|
|
112
|
+
# :CREATED, :PAID
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
``` ruby
|
117
|
+
OrderState.each_value do |value|
|
118
|
+
# 'created', 'paid'
|
41
119
|
end
|
42
120
|
```
|
43
121
|
|
44
122
|
#### Mapping
|
45
123
|
|
46
124
|
``` ruby
|
47
|
-
|
48
|
-
# key and enum.key
|
49
|
-
# enum.value is
|
125
|
+
OrderState.map do |key, enum|
|
126
|
+
# key and enum.key are :CREATED, :PAID
|
127
|
+
# enum.value is 'created', 'paid'
|
50
128
|
[enum.value, key]
|
51
129
|
end
|
52
130
|
|
53
|
-
# => [ ['
|
131
|
+
# => [ ['created', :CREATED], ['paid', :PAID] ]
|
54
132
|
```
|
55
133
|
|
56
134
|
#### Reducing
|
57
135
|
|
58
136
|
``` ruby
|
59
|
-
|
60
|
-
# key and enum.key
|
61
|
-
# enum.value is
|
137
|
+
OrderState.reduce([]) do |arr, (key, enum)|
|
138
|
+
# key and enum.key are :CREATED, :PAID
|
139
|
+
# enum.value is 'created', 'paid'
|
62
140
|
arr << [enum.value, key]
|
63
141
|
end
|
64
142
|
|
65
|
-
# => [ ['
|
143
|
+
# => [ ['created', :CREATED], ['paid', :PAID] ]
|
66
144
|
```
|
67
145
|
|
68
146
|
#### Sorting
|
147
|
+
|
69
148
|
``` ruby
|
70
|
-
|
71
|
-
# key and enum.key
|
72
|
-
# enum.value is
|
73
|
-
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
|
74
153
|
end
|
75
154
|
|
76
|
-
# => [
|
155
|
+
# => [[:PAID, #<OrderState:0x0 @key=:PAID, @value="paid">], [:CREATED, #<OrderState:0x1 @key=:CREATED, @value="created">]]
|
77
156
|
```
|
78
157
|
|
79
|
-
###
|
158
|
+
### Hashing
|
159
|
+
|
160
|
+
Several hash-like methods are supported.
|
80
161
|
|
81
162
|
#### Retrieving keys and values
|
82
163
|
|
83
164
|
``` ruby
|
84
|
-
|
85
|
-
# => [:
|
165
|
+
OrderState.keys
|
166
|
+
# => [:CREATED, :PAID]
|
86
167
|
|
87
|
-
|
88
|
-
# => [
|
168
|
+
OrderState.values
|
169
|
+
# => ['created', 'paid']
|
89
170
|
```
|
90
171
|
|
91
172
|
#### Mapping keys to values
|
92
173
|
|
93
174
|
``` ruby
|
94
|
-
|
175
|
+
OrderState.key?(:CREATED)
|
95
176
|
# => true
|
96
177
|
|
97
|
-
|
98
|
-
# =>
|
178
|
+
OrderState.value(:CREATED)
|
179
|
+
# => 'created'
|
99
180
|
|
100
|
-
|
181
|
+
OrderState.key?(:FAILED)
|
101
182
|
# => false
|
102
183
|
|
103
|
-
|
184
|
+
OrderState.value(:FAILED)
|
104
185
|
# => nil
|
105
186
|
```
|
106
187
|
|
107
188
|
#### Mapping values to keys
|
108
189
|
|
109
190
|
``` ruby
|
110
|
-
|
191
|
+
OrderState.value?('paid')
|
111
192
|
# => true
|
112
193
|
|
113
|
-
|
114
|
-
# => :
|
194
|
+
OrderState.key('paid')
|
195
|
+
# => :PAID
|
115
196
|
|
116
|
-
|
197
|
+
OrderState.value?('failed')
|
117
198
|
# => false
|
118
199
|
|
119
|
-
|
200
|
+
OrderState.key('failed')
|
120
201
|
# => nil
|
121
202
|
```
|
122
203
|
|
204
|
+
### Duplicate enumerator keys or duplicate values
|
123
205
|
|
124
|
-
|
206
|
+
Defining duplicate enums raises `Ruby::Enum::Errors::DuplicateKeyError`.
|
125
207
|
|
126
|
-
|
127
|
-
|
128
|
-
|
208
|
+
```ruby
|
209
|
+
class OrderState
|
210
|
+
include Ruby::Enum
|
211
|
+
|
212
|
+
define :CREATED, 'created'
|
213
|
+
define :CREATED, 'recreated' # raises DuplicateKeyError
|
214
|
+
end
|
215
|
+
```
|
216
|
+
|
217
|
+
Defining a duplicate value raises `Ruby::Enum::Errors::DuplicateValueError`.
|
129
218
|
|
130
219
|
```ruby
|
131
|
-
|
132
|
-
|
220
|
+
class OrderState
|
221
|
+
include Ruby::Enum
|
133
222
|
|
134
|
-
|
135
|
-
|
136
|
-
|
223
|
+
define :CREATED, 'created'
|
224
|
+
define :RECREATED, 'created' # raises DuplicateValueError
|
225
|
+
end
|
226
|
+
```
|
137
227
|
|
138
|
-
|
139
|
-
class Colots
|
140
|
-
include Ruby::Enum
|
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')`.
|
141
229
|
|
142
|
-
|
143
|
-
|
144
|
-
|
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.
|
233
|
+
|
234
|
+
``` ruby
|
235
|
+
class OrderState
|
236
|
+
include Ruby::Enum
|
237
|
+
|
238
|
+
define :CREATED, 'CREATED'
|
239
|
+
define :PAID, 'PAID'
|
240
|
+
end
|
241
|
+
|
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'
|
145
253
|
```
|
146
254
|
|
147
|
-
The `
|
148
|
-
|
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
|
+
```
|
149
261
|
|
150
262
|
## Contributing
|
151
263
|
|
152
|
-
You're encouraged to contribute to
|
264
|
+
You're encouraged to contribute to ruby-enum. See [CONTRIBUTING](CONTRIBUTING.md) for details.
|
153
265
|
|
154
266
|
## Copyright and License
|
155
267
|
|
156
|
-
Copyright (c) 2013-
|
268
|
+
Copyright (c) 2013-2021, Daniel Doubrovkine and [Contributors](CHANGELOG.md).
|
157
269
|
|
158
270
|
This project is licensed under the [MIT License](LICENSE.md).
|
159
271
|
|