enums 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -6
- data/lib/enums.rb +24 -14
- data/lib/enums/version.rb +5 -4
- data/test/test_enum.rb +19 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb53c7f0aba659b47bf538728d3c984e10f227c6
|
4
|
+
data.tar.gz: 35d975e75d3325e1ae884ed310b11bf73baed647
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d232ae4c94f1a115b71cd9fa172e2b5f6ba607a08a79e13624f4c0d1b69bcc1528e10824d19dc4ac37b78a98b158ca1017fdadbf493fa8ed6fd2a79279701c0d
|
7
|
+
data.tar.gz: 0663413b82dff6ebc503219df199f3b1755d74df81515660877d7cf58766fdd0f36b7fc466d61e6e62edba1815b978ba41342d128144066e2a443b7e0250a44c
|
data/README.md
CHANGED
@@ -44,11 +44,11 @@ Why? Why not? Discuss.
|
|
44
44
|
## Usage
|
45
45
|
|
46
46
|
``` ruby
|
47
|
-
Enum.new(
|
47
|
+
Enum.new( :Color, :red, :green, :blue )
|
48
48
|
# or
|
49
|
-
enum
|
49
|
+
enum :Color, :red, :green, :blue
|
50
50
|
# or
|
51
|
-
enum
|
51
|
+
enum :Color, [:red, :green, :blue]
|
52
52
|
```
|
53
53
|
|
54
54
|
|
@@ -131,11 +131,11 @@ color.is_a? Color #=> true
|
|
131
131
|
Let's try another example:
|
132
132
|
|
133
133
|
``` ruby
|
134
|
-
Enum.new(
|
134
|
+
Enum.new( :State, :fundraising, :expired_refund, :successful )
|
135
135
|
# or
|
136
|
-
enum
|
136
|
+
enum :State, :fundraising, :expired_refund, :successful
|
137
137
|
# or
|
138
|
-
enum
|
138
|
+
enum :State, [:fundraising, :expired_refund, :successful]
|
139
139
|
|
140
140
|
|
141
141
|
State.values #=> [0, 1, 2]
|
data/lib/enums.rb
CHANGED
@@ -11,7 +11,7 @@ require 'enums/version' # note: let version always go first
|
|
11
11
|
## auto-create/builds enum class.
|
12
12
|
##
|
13
13
|
## Example:
|
14
|
-
##
|
14
|
+
## Enum.new( :State, :fundraising, :expired_refund, :successful)
|
15
15
|
## auto-creates/builds:
|
16
16
|
##
|
17
17
|
## class Enum
|
@@ -46,19 +46,29 @@ require 'enums/version' # note: let version always go first
|
|
46
46
|
## pp state.successful? #=> false
|
47
47
|
|
48
48
|
|
49
|
+
|
49
50
|
module Safe
|
50
51
|
|
51
52
|
## note: use ClassMethods pattern for auto-including class methods
|
53
|
+
## note ClassMethods module is called SafeHelper
|
52
54
|
def self.included( klass )
|
53
|
-
klass.extend(
|
55
|
+
klass.extend( SafeHelper )
|
54
56
|
end
|
55
|
-
module ClassMethods; end
|
56
57
|
|
58
|
+
module SafeHelper; end
|
59
|
+
## note: also extends (include a helper methods to Safe itself)
|
60
|
+
## lets you use:
|
61
|
+
## module Safe
|
62
|
+
## enum :Color, :red, :green, :blue
|
63
|
+
## end
|
64
|
+
extend SafeHelper
|
57
65
|
|
58
66
|
|
59
67
|
## base class for enum
|
60
68
|
class Enum
|
61
69
|
## return a new Enum read-only class
|
70
|
+
attr_reader :key
|
71
|
+
attr_reader :value
|
62
72
|
|
63
73
|
def initialize( key, value )
|
64
74
|
@key = key
|
@@ -67,10 +77,7 @@ class Enum
|
|
67
77
|
self
|
68
78
|
end
|
69
79
|
|
70
|
-
|
71
|
-
## just use attr_reader!!! - why? why not?
|
72
|
-
def key() @key; end
|
73
|
-
def value() @value; end
|
80
|
+
|
74
81
|
|
75
82
|
def self.value( index )
|
76
83
|
## todo/fix: check for out-of-bound / unknown enum
|
@@ -80,6 +87,8 @@ class Enum
|
|
80
87
|
members[ index ]
|
81
88
|
end
|
82
89
|
|
90
|
+
def zero?() self == self.class.zero; end
|
91
|
+
|
83
92
|
def self.zero
|
84
93
|
members[0]
|
85
94
|
end
|
@@ -157,14 +166,14 @@ RUBY
|
|
157
166
|
end
|
158
167
|
RUBY
|
159
168
|
|
160
|
-
## note: use
|
161
|
-
## make all enums convenience converters (always) global
|
169
|
+
## note: use Kernel for "namespacing"
|
170
|
+
## make all enums Kernel convenience converters (always) global
|
162
171
|
## including uppercase methods (e.g. State(), Color(), etc.) does NOT work otherwise (with other module includes)
|
163
172
|
|
164
173
|
## add global convenience converter function
|
165
174
|
## e.g. State(0) is same as State.convert(0)
|
166
175
|
## Color(0) is same as Color.convert(0)
|
167
|
-
|
176
|
+
Kernel.class_eval( <<RUBY )
|
168
177
|
def #{class_name}( arg )
|
169
178
|
#{class_name}.convert( arg )
|
170
179
|
end
|
@@ -175,6 +184,7 @@ RUBY
|
|
175
184
|
Safe.const_set( class_name, klass ) ## returns klass (plus sets global constant class name)
|
176
185
|
end
|
177
186
|
|
187
|
+
|
178
188
|
class << self
|
179
189
|
alias_method :old_new, :new # note: store "old" orginal version of new
|
180
190
|
alias_method :new, :build_class # replace original version with create
|
@@ -182,13 +192,13 @@ RUBY
|
|
182
192
|
end # class Enum
|
183
193
|
|
184
194
|
|
185
|
-
module
|
195
|
+
module SafeHelper
|
186
196
|
def enum( class_name, *args )
|
187
197
|
########################################
|
188
198
|
# note: lets you use:
|
189
|
-
# enum
|
199
|
+
# enum :Color, :red, :green, :blue
|
190
200
|
# -or-
|
191
|
-
# enum
|
201
|
+
# enum :Color, [:red, :green, :blue]
|
192
202
|
if args[0].is_a?( Array )
|
193
203
|
keys = args[0]
|
194
204
|
else
|
@@ -197,7 +207,7 @@ module ClassMethods
|
|
197
207
|
|
198
208
|
Enum.new( class_name, *keys )
|
199
209
|
end
|
200
|
-
end # module
|
210
|
+
end # module SafeHelper
|
201
211
|
end # module Safe
|
202
212
|
|
203
213
|
|
data/lib/enums/version.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
## note: use a (new)
|
4
|
-
|
3
|
+
## note: use a (new) module Enums (with s!! at the end) for version info
|
4
|
+
## why? avoids "pollution" Enum namespace with (library) version info etc.
|
5
|
+
module Enums
|
5
6
|
|
6
7
|
MAJOR = 1
|
7
8
|
MINOR = 1
|
8
|
-
PATCH =
|
9
|
+
PATCH = 1
|
9
10
|
VERSION = [MAJOR,MINOR,PATCH].join('.')
|
10
11
|
|
11
12
|
def self.version
|
@@ -20,4 +21,4 @@ class Enums
|
|
20
21
|
"#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
|
21
22
|
end
|
22
23
|
|
23
|
-
end #
|
24
|
+
end # module Enums
|
data/test/test_enum.rb
CHANGED
@@ -8,28 +8,30 @@
|
|
8
8
|
require 'helper'
|
9
9
|
|
10
10
|
|
11
|
-
|
11
|
+
module Safe
|
12
|
+
## Enum.new( 'State', :fundraising, :expired_refund, :successful )
|
13
|
+
enum :State, :fundraising, :expired_refund, :successful
|
12
14
|
|
13
|
-
|
15
|
+
pp State
|
16
|
+
pp State(0)
|
14
17
|
|
18
|
+
puts "Safe.constants:"
|
19
|
+
pp Safe.constants #=> [:SafeHelper, :Enum, :State, :Color]
|
20
|
+
puts "Enum.constants:"
|
21
|
+
pp Enum.constants #=> []
|
22
|
+
end
|
15
23
|
|
16
|
-
## Enum.new( 'State', :fundraising, :expired_refund, :successful )
|
17
|
-
enum 'State', :fundraising, :expired_refund, :successful
|
18
24
|
|
19
|
-
|
20
|
-
|
25
|
+
class TestEnum < MiniTest::Test
|
26
|
+
|
27
|
+
include Safe ## make all enums (and "convenience" converters) global
|
21
28
|
|
22
29
|
## Enum.new( 'Color', :red, :green, :blue )
|
23
|
-
enum
|
30
|
+
enum :Color, :red, :green, :blue
|
24
31
|
|
25
32
|
pp Color
|
26
33
|
pp Color(0)
|
27
34
|
|
28
|
-
puts "Safe.constants:"
|
29
|
-
pp Safe.constants #=> [:ClassMethods, :Enum, :State, :Color]
|
30
|
-
puts "Enum.constants:"
|
31
|
-
pp Enum.constants #=> []
|
32
|
-
|
33
35
|
|
34
36
|
def test_state
|
35
37
|
assert_equal [:FUNDRAISING, :EXPIRED_REFUND, :SUCCESSFUL], State.constants
|
@@ -71,6 +73,11 @@ def test_state
|
|
71
73
|
assert_equal true, State(0) == State.zero
|
72
74
|
assert_equal false, State(1) == State.zero
|
73
75
|
|
76
|
+
assert_equal true, State(0).zero?
|
77
|
+
assert_equal true, State.zero.zero?
|
78
|
+
assert_equal false, State(1).zero?
|
79
|
+
assert_equal false, State(2).zero?
|
80
|
+
|
74
81
|
assert_equal State.fundraising, State.value(0)
|
75
82
|
assert_equal State.fundraising, State.key(:fundraising)
|
76
83
|
assert_equal State.fundraising, State[:fundraising]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enums
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-03-
|
11
|
+
date: 2019-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|