You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
9
10
10
## Table of Contents
11
11
@@ -23,65 +23,65 @@ Enum-like behavior for Ruby, heavily inspired by [this](http://www.rubyfleebie.c
23
23
-[Mapping keys to values](#mapping-keys-to-values)
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
-
-[Inheritance behavior](#inheritance-behavior)
26
+
-[Inheritance](#inheritance)
27
27
-[Contributing](#contributing)
28
28
-[Copyright and License](#copyright-and-license)
29
29
-[Related Projects](#related-projects)
30
30
31
31
## Usage
32
32
33
-
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.
The `DuplicateValueError` exception is thrown to be consistent with the unique key constraint.
227
-
Since keys are unique there is no way to map values to keys using `Colors.value('red')`
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')`.
228
229
229
-
### Inheritance behavior
230
+
### Inheritance
230
231
231
-
Inheriting from a `Ruby::Enum` class, all defined enums in the parent class will be accessible in sub classes as well.
232
-
Sub classes can also provide extra enums as usual.
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
233
234
234
```ruby
235
-
classPrimaryColors
235
+
classOrderState
236
236
includeRuby::Enum
237
237
238
-
define :RED, 'RED'
239
-
define :GREEN, 'GREEN'
240
-
define :BLUE, 'BLUE'
238
+
define :CREATED, 'CREATED'
239
+
define :PAID, 'PAID'
241
240
end
242
241
243
-
classRainbowColors < PrimaryColors
244
-
define :ORANGE, 'ORANGE'
245
-
define :YELLOW, 'YELLOW'
246
-
define :INIDGO, 'INIDGO'
247
-
define :VIOLET, 'VIOLET'
242
+
classShippedOrderState < OrderState
243
+
define :PREPARED, 'PREPARED'
244
+
define :SHIPPED, 'SHIPPED'
248
245
end
249
246
```
250
247
251
248
```ruby
252
-
RainbowColors::RED# 'RED'
253
-
RainbowColors::ORANGE# 'ORANGE'
254
-
RainbowColors::YELLOW# 'YELLOW'
255
-
RainbowColors::GREEN# 'GREEN'
256
-
RainbowColors::BLUE# 'BLUE'
257
-
RainbowColors::INIDGO# 'INIDGO'
258
-
RainbowColors::VIOLET# 'VIOLET'
249
+
ShippedOrderState::CREATED# 'CREATED'
250
+
ShippedOrderState::PAID# 'PAID'
251
+
ShippedOrderState::PREPARED# 'PREPARED'
252
+
ShippedOrderState::SHIPPED# 'SHIPPED'
259
253
```
260
254
261
255
The `values` class method will enumerate the values from all base classes.
0 commit comments