simpler_enum 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +18 -0
- data/lib/simpler_enum/generator.rb +11 -1
- data/lib/simpler_enum/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -25,6 +25,22 @@ Any class can include `SimplerEnum` and you can define an enumerated type like t
|
|
25
25
|
```ruby
|
26
26
|
require "simpler_enum"
|
27
27
|
|
28
|
+
class Person
|
29
|
+
include SimplerEnum
|
30
|
+
|
31
|
+
simpler_enum mood: %i(awesome excellent great good fine)
|
32
|
+
|
33
|
+
def initialize(mood: :fine)
|
34
|
+
self.mood = mood
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Or,
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require "simpler_enum"
|
43
|
+
|
28
44
|
class Person
|
29
45
|
include SimplerEnum
|
30
46
|
|
@@ -42,6 +58,8 @@ class Person
|
|
42
58
|
end
|
43
59
|
```
|
44
60
|
|
61
|
+
Both are behave like this:
|
62
|
+
|
45
63
|
```ruby
|
46
64
|
[1] pry(main)> Person.moods
|
47
65
|
=> {:awesome=>0, :excellent=>1, :great=>2, :good=>3, :fine=>4}
|
@@ -3,7 +3,17 @@ module SimplerEnum
|
|
3
3
|
def initialize(klass, enum_name, enum_values)
|
4
4
|
@klass = klass
|
5
5
|
@enum_name = enum_name
|
6
|
-
@enum_values =
|
6
|
+
@enum_values =
|
7
|
+
case
|
8
|
+
when enum_values.is_a?(Array)
|
9
|
+
enum_values.zip(0...enum_values.size).reduce({}) do |hash, (key, val)|
|
10
|
+
hash.merge!(key => val)
|
11
|
+
end
|
12
|
+
when enum_values.is_a?(Hash)
|
13
|
+
enum_values
|
14
|
+
else
|
15
|
+
fail ArgumentError
|
16
|
+
end
|
7
17
|
end
|
8
18
|
|
9
19
|
def execute!
|
data/lib/simpler_enum/version.rb
CHANGED