enums 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3 -0
- data/LICENSE.md +116 -0
- data/Manifest.txt +9 -0
- data/README.md +138 -0
- data/Rakefile +29 -0
- data/lib/enums.rb +168 -0
- data/lib/enums/version.rb +23 -0
- data/test/helper.rb +7 -0
- data/test/test_enum.rb +88 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ca648558b0ebcd1b42f7f6d587ef888c7f7f58aa
|
4
|
+
data.tar.gz: fa6ce8d8931379a42491296016e498ec769c4194
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fdbc9303c337e5196444164cef1268a05f70252acc47163d2002c22b807dae50bb088b02e0b56f975753f5442492506a2fb37f555a49ff36b3011db1900b927a
|
7
|
+
data.tar.gz: 759a5706d1621c805fbfa985591cd1fbe3b832a2be191555ea1c9b44850548c906abd8b1cb8f5e01abbedecced1b3335a23a5eaf8f5b28edaed2665ce602725d
|
data/CHANGELOG.md
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
CC0 1.0 Universal
|
2
|
+
|
3
|
+
Statement of Purpose
|
4
|
+
|
5
|
+
The laws of most jurisdictions throughout the world automatically confer
|
6
|
+
exclusive Copyright and Related Rights (defined below) upon the creator and
|
7
|
+
subsequent owner(s) (each and all, an "owner") of an original work of
|
8
|
+
authorship and/or a database (each, a "Work").
|
9
|
+
|
10
|
+
Certain owners wish to permanently relinquish those rights to a Work for the
|
11
|
+
purpose of contributing to a commons of creative, cultural and scientific
|
12
|
+
works ("Commons") that the public can reliably and without fear of later
|
13
|
+
claims of infringement build upon, modify, incorporate in other works, reuse
|
14
|
+
and redistribute as freely as possible in any form whatsoever and for any
|
15
|
+
purposes, including without limitation commercial purposes. These owners may
|
16
|
+
contribute to the Commons to promote the ideal of a free culture and the
|
17
|
+
further production of creative, cultural and scientific works, or to gain
|
18
|
+
reputation or greater distribution for their Work in part through the use and
|
19
|
+
efforts of others.
|
20
|
+
|
21
|
+
For these and/or other purposes and motivations, and without any expectation
|
22
|
+
of additional consideration or compensation, the person associating CC0 with a
|
23
|
+
Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
|
24
|
+
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
|
25
|
+
and publicly distribute the Work under its terms, with knowledge of his or her
|
26
|
+
Copyright and Related Rights in the Work and the meaning and intended legal
|
27
|
+
effect of CC0 on those rights.
|
28
|
+
|
29
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
30
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
31
|
+
Related Rights"). Copyright and Related Rights include, but are not limited
|
32
|
+
to, the following:
|
33
|
+
|
34
|
+
i. the right to reproduce, adapt, distribute, perform, display, communicate,
|
35
|
+
and translate a Work;
|
36
|
+
|
37
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
38
|
+
|
39
|
+
iii. publicity and privacy rights pertaining to a person's image or likeness
|
40
|
+
depicted in a Work;
|
41
|
+
|
42
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
43
|
+
subject to the limitations in paragraph 4(a), below;
|
44
|
+
|
45
|
+
v. rights protecting the extraction, dissemination, use and reuse of data in
|
46
|
+
a Work;
|
47
|
+
|
48
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
49
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
50
|
+
protection of databases, and under any national implementation thereof,
|
51
|
+
including any amended or successor version of such directive); and
|
52
|
+
|
53
|
+
vii. other similar, equivalent or corresponding rights throughout the world
|
54
|
+
based on applicable law or treaty, and any national implementations thereof.
|
55
|
+
|
56
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention of,
|
57
|
+
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
|
58
|
+
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
|
59
|
+
and Related Rights and associated claims and causes of action, whether now
|
60
|
+
known or unknown (including existing as well as future claims and causes of
|
61
|
+
action), in the Work (i) in all territories worldwide, (ii) for the maximum
|
62
|
+
duration provided by applicable law or treaty (including future time
|
63
|
+
extensions), (iii) in any current or future medium and for any number of
|
64
|
+
copies, and (iv) for any purpose whatsoever, including without limitation
|
65
|
+
commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
|
66
|
+
the Waiver for the benefit of each member of the public at large and to the
|
67
|
+
detriment of Affirmer's heirs and successors, fully intending that such Waiver
|
68
|
+
shall not be subject to revocation, rescission, cancellation, termination, or
|
69
|
+
any other legal or equitable action to disrupt the quiet enjoyment of the Work
|
70
|
+
by the public as contemplated by Affirmer's express Statement of Purpose.
|
71
|
+
|
72
|
+
3. Public License Fallback. Should any part of the Waiver for any reason be
|
73
|
+
judged legally invalid or ineffective under applicable law, then the Waiver
|
74
|
+
shall be preserved to the maximum extent permitted taking into account
|
75
|
+
Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
|
76
|
+
is so judged Affirmer hereby grants to each affected person a royalty-free,
|
77
|
+
non transferable, non sublicensable, non exclusive, irrevocable and
|
78
|
+
unconditional license to exercise Affirmer's Copyright and Related Rights in
|
79
|
+
the Work (i) in all territories worldwide, (ii) for the maximum duration
|
80
|
+
provided by applicable law or treaty (including future time extensions), (iii)
|
81
|
+
in any current or future medium and for any number of copies, and (iv) for any
|
82
|
+
purpose whatsoever, including without limitation commercial, advertising or
|
83
|
+
promotional purposes (the "License"). The License shall be deemed effective as
|
84
|
+
of the date CC0 was applied by Affirmer to the Work. Should any part of the
|
85
|
+
License for any reason be judged legally invalid or ineffective under
|
86
|
+
applicable law, such partial invalidity or ineffectiveness shall not
|
87
|
+
invalidate the remainder of the License, and in such case Affirmer hereby
|
88
|
+
affirms that he or she will not (i) exercise any of his or her remaining
|
89
|
+
Copyright and Related Rights in the Work or (ii) assert any associated claims
|
90
|
+
and causes of action with respect to the Work, in either case contrary to
|
91
|
+
Affirmer's express Statement of Purpose.
|
92
|
+
|
93
|
+
4. Limitations and Disclaimers.
|
94
|
+
|
95
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
96
|
+
surrendered, licensed or otherwise affected by this document.
|
97
|
+
|
98
|
+
b. Affirmer offers the Work as-is and makes no representations or warranties
|
99
|
+
of any kind concerning the Work, express, implied, statutory or otherwise,
|
100
|
+
including without limitation warranties of title, merchantability, fitness
|
101
|
+
for a particular purpose, non infringement, or the absence of latent or
|
102
|
+
other defects, accuracy, or the present or absence of errors, whether or not
|
103
|
+
discoverable, all to the greatest extent permissible under applicable law.
|
104
|
+
|
105
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
106
|
+
that may apply to the Work or any use thereof, including without limitation
|
107
|
+
any person's Copyright and Related Rights in the Work. Further, Affirmer
|
108
|
+
disclaims responsibility for obtaining any necessary consents, permissions
|
109
|
+
or other rights required for any use of the Work.
|
110
|
+
|
111
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
112
|
+
party to this document and has no duty or obligation with respect to this
|
113
|
+
CC0 or use of the Work.
|
114
|
+
|
115
|
+
For more information, please see
|
116
|
+
<http://creativecommons.org/publicdomain/zero/1.0/>
|
data/Manifest.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
# Enums - Safe Enumeration Types
|
2
|
+
|
3
|
+
enums library / gem - safe enumeration types - a set of symbolic keys bound to unique integer numbers
|
4
|
+
|
5
|
+
* home :: [github.com/s6ruby/enums](https://github.com/s6ruby/enums)
|
6
|
+
* bugs :: [github.com/s6ruby/enums/issues](https://github.com/s6ruby/enums/issues)
|
7
|
+
* gem :: [rubygems.org/gems/enums](https://rubygems.org/gems/enums)
|
8
|
+
* rdoc :: [rubydoc.info/gems/enums](http://rubydoc.info/gems/enums)
|
9
|
+
* forum :: [wwwmake](http://groups.google.com/group/wwwmake)
|
10
|
+
|
11
|
+
|
12
|
+
## Why `enums` in Ruby?
|
13
|
+
|
14
|
+
No library needed, just use symbols :-) or use constants. Example:
|
15
|
+
|
16
|
+
``` ruby
|
17
|
+
Color = [:red, :blue, :green]
|
18
|
+
Color[0] #=> :red
|
19
|
+
Color[1] #=> :blue
|
20
|
+
|
21
|
+
# -or-
|
22
|
+
|
23
|
+
Color = {red: 0, blue: 1, green: 2}
|
24
|
+
Color[:red] #=> 0
|
25
|
+
Color[:blue] #=> 1
|
26
|
+
Color.keys #=> [:red, :blue, :green]
|
27
|
+
Color.values #=> [0, 1, 2]
|
28
|
+
|
29
|
+
# -or-
|
30
|
+
|
31
|
+
module Color
|
32
|
+
RED = 0
|
33
|
+
BLUE = 1
|
34
|
+
GREEN = 2
|
35
|
+
end
|
36
|
+
Color::RED #=> 0
|
37
|
+
Color::BLUE #=> 1
|
38
|
+
Color.constants #=> [:RED, :BLUE, :GREEN]
|
39
|
+
# ...
|
40
|
+
```
|
41
|
+
|
42
|
+
Why? Why not? Discuss.
|
43
|
+
|
44
|
+
|
45
|
+
## Usage
|
46
|
+
|
47
|
+
``` ruby
|
48
|
+
Enum.new( 'Color', :red, :green, :blue )
|
49
|
+
```
|
50
|
+
|
51
|
+
(auto-)builds a class and code like:
|
52
|
+
|
53
|
+
``` ruby
|
54
|
+
class Enum
|
55
|
+
def initialize( key, value )
|
56
|
+
@key = key
|
57
|
+
@value = value
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class Color < Enum
|
62
|
+
|
63
|
+
RED = new(:red, 0)
|
64
|
+
BLUE = new(:blue, 1)
|
65
|
+
GREEN = new(:green, 2)
|
66
|
+
|
67
|
+
def self.red() RED; end
|
68
|
+
def self.blue() BLUE; end
|
69
|
+
def self.green() GREEN; end
|
70
|
+
|
71
|
+
def self.values() [0,1,2]; end
|
72
|
+
def self.keys() [:red, :blue, :green]; end
|
73
|
+
def self.members() [RED, BLUE, GREEN]; end
|
74
|
+
|
75
|
+
def self.value( index ) @members[index]; end
|
76
|
+
def self.zero() value(0); end
|
77
|
+
|
78
|
+
def self.key( key )
|
79
|
+
@hash ||= Hash[ keys.zip( members ) ]
|
80
|
+
@hash[ key ]
|
81
|
+
end
|
82
|
+
def self.[]( key ) self.key( key ); end
|
83
|
+
|
84
|
+
def red?() self == RED; end
|
85
|
+
def blue?() self == BLUE; end
|
86
|
+
def green?() self == GREEN; end
|
87
|
+
end
|
88
|
+
|
89
|
+
def Color( arg )
|
90
|
+
Color.value( arg )
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
|
95
|
+
Use like:
|
96
|
+
|
97
|
+
``` ruby
|
98
|
+
Color::RED #=> <Color @key=:red, @value=0>
|
99
|
+
Color(0) #=> Color::RED -or- <Color @key=:red, @value=0>
|
100
|
+
Color.zero #=> same as Color(0)
|
101
|
+
Color.red #=> Color::RED
|
102
|
+
Color.values #=> [0, 1, 2]
|
103
|
+
Color.keys #=> [:red, :green, :blue]
|
104
|
+
Color.members #=> [RED, GREEN, BLUE]
|
105
|
+
# -or-
|
106
|
+
# [<Color @key=:red, @value=0>,
|
107
|
+
# <Color @key=:green, @value=1>,
|
108
|
+
# <Color @key=:blue, @value=2>]
|
109
|
+
Color(1) #=> Color::GREEN
|
110
|
+
Color.value(1) # same as Color(1)
|
111
|
+
Color[:red] #=> Color::RED
|
112
|
+
Color.key(:red) # same as Color[:red]
|
113
|
+
color = Color.red
|
114
|
+
color.red? #=> true
|
115
|
+
color == Color.red #=> true
|
116
|
+
color.value #=> 0
|
117
|
+
color.key #=> :red
|
118
|
+
color.blue? #=> false
|
119
|
+
color == Color.blue #=> false
|
120
|
+
color.is_a? Enum #=> true
|
121
|
+
color.is_a? Color #=> true
|
122
|
+
...
|
123
|
+
```
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
## License
|
128
|
+
|
129
|
+
![](https://publicdomainworks.github.io/buttons/zero88x31.png)
|
130
|
+
|
131
|
+
The `enums` scripts are dedicated to the public domain.
|
132
|
+
Use it as you please with no restrictions whatsoever.
|
133
|
+
|
134
|
+
|
135
|
+
## Questions? Comments?
|
136
|
+
|
137
|
+
Send them along to the [wwwmake forum](http://groups.google.com/group/wwwmake).
|
138
|
+
Thanks!
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'hoe'
|
2
|
+
require './lib/enums/version.rb'
|
3
|
+
|
4
|
+
Hoe.spec 'enums' do
|
5
|
+
|
6
|
+
self.version = Enums::VERSION
|
7
|
+
|
8
|
+
self.summary = "enums - safe enumeration types - a set of symbolic keys bound to unique integer numbers"
|
9
|
+
self.description = summary
|
10
|
+
|
11
|
+
self.urls = ['https://github.com/s6ruby/enums']
|
12
|
+
|
13
|
+
self.author = 'Gerald Bauer'
|
14
|
+
self.email = 'wwwmake@googlegroups.com'
|
15
|
+
|
16
|
+
# switch extension to .markdown for gihub formatting
|
17
|
+
self.readme_file = 'README.md'
|
18
|
+
self.history_file = 'CHANGELOG.md'
|
19
|
+
|
20
|
+
self.extra_deps = [
|
21
|
+
]
|
22
|
+
|
23
|
+
self.licenses = ['Public Domain']
|
24
|
+
|
25
|
+
self.spec_extras = {
|
26
|
+
required_ruby_version: '>= 2.2.2'
|
27
|
+
}
|
28
|
+
|
29
|
+
end
|
data/lib/enums.rb
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
## stdlibs
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
|
7
|
+
## our own code
|
8
|
+
require 'enums/version' # note: let version always go first
|
9
|
+
|
10
|
+
##################################
|
11
|
+
## auto-create/builds enum class.
|
12
|
+
##
|
13
|
+
## Example:
|
14
|
+
## State = Enum.new( :fundraising, :expired_refund, :successful)
|
15
|
+
## auto-creates/builds:
|
16
|
+
##
|
17
|
+
## class Enum
|
18
|
+
## def initialize( key, value )
|
19
|
+
## @key = key
|
20
|
+
## @value = value
|
21
|
+
## end
|
22
|
+
## end
|
23
|
+
##
|
24
|
+
## class State < Enum
|
25
|
+
##
|
26
|
+
## FUNDRAISING = new(:fundraising, 0)
|
27
|
+
## EXPIRED_REFUND = new(:expired_refund, 1)
|
28
|
+
## SUCCESSFUL = new(:successful, 2)
|
29
|
+
##
|
30
|
+
## def self.fundraising() FUNDRAISING; end
|
31
|
+
## def self.expired_refund() EXPIRED_REFUND; end
|
32
|
+
## def self.successful() SUCCESSFUL; end
|
33
|
+
##
|
34
|
+
## def fundraising?() self == FUNDRAISING; end
|
35
|
+
## def expired_refund?() self == EXPIRED_REFUND; end
|
36
|
+
## def successful?() self == SUCCESSFUL; end
|
37
|
+
## end
|
38
|
+
##
|
39
|
+
## pp state = State.fundraising #=> #<State @key=:fundraising, @value=0>
|
40
|
+
## pp state.fundraising? #=> true
|
41
|
+
## pp state.expired_refund? #=> false
|
42
|
+
## pp state.successful? #=> false
|
43
|
+
## pp state = State.expired_refund #=> #<State @key=:expired_refund, @value=1>
|
44
|
+
## pp state.fundraising? #=> false
|
45
|
+
## pp state.expired_refund? #=> true
|
46
|
+
## pp state.successful? #=> false
|
47
|
+
|
48
|
+
|
49
|
+
module Safe
|
50
|
+
|
51
|
+
|
52
|
+
## base class for enum
|
53
|
+
class Enum
|
54
|
+
## return a new Enum read-only class
|
55
|
+
|
56
|
+
def initialize( key, value )
|
57
|
+
@key = key
|
58
|
+
@value = value
|
59
|
+
self.freeze ## make "immutable"
|
60
|
+
self
|
61
|
+
end
|
62
|
+
|
63
|
+
## add read-only attribute readers
|
64
|
+
## just use attr_reader!!! - why? why not?
|
65
|
+
def key() @key; end
|
66
|
+
def value() @value; end
|
67
|
+
|
68
|
+
def self.value( index )
|
69
|
+
## todo/fix: check for out-of-bound / unknown enum
|
70
|
+
## puts "#{name}.value(#{index})"
|
71
|
+
## pp keys
|
72
|
+
## pp values
|
73
|
+
members[ index ]
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.zero
|
77
|
+
members[0]
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.key( key )
|
81
|
+
@hash ||= Hash[ keys.zip( members ) ].freeze
|
82
|
+
@hash[key]
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.[]( key ) ## convenience alias for key
|
86
|
+
self.key( key )
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.values
|
90
|
+
@values ||= (0...keys.size).to_a.freeze
|
91
|
+
@values
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.convert( arg )
|
95
|
+
## todo/check: support keys too - why? why not?
|
96
|
+
## e.g. Color(0), Color(1)
|
97
|
+
## Color(:red), Color(:blue) - why? why not?
|
98
|
+
value( arg )
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
###################
|
104
|
+
## meta-programming "macro" - build class (on the fly)
|
105
|
+
def self.build_class( class_name, *keys )
|
106
|
+
|
107
|
+
## check if all keys are symbols and follow the ruby id(entifier) naming rules
|
108
|
+
keys.each do |key|
|
109
|
+
if key.is_a?( Symbol ) && key =~ /\A[a-z][a-zA-Z0-9_]*\z/
|
110
|
+
else
|
111
|
+
raise ArgumentError.new( "[Enum] arguments to Enum.new must be all symbols following the ruby id naming rules; >#{key}< failed" )
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
klass = Class.new( Enum )
|
116
|
+
|
117
|
+
## add self.new too - note: call/forward to "old" orginal self.new of Event (base) class
|
118
|
+
klass.define_singleton_method( :new ) do |*args|
|
119
|
+
old_new( *args )
|
120
|
+
end
|
121
|
+
|
122
|
+
keys.each_with_index do |key,index|
|
123
|
+
klass.class_eval( <<RUBY )
|
124
|
+
#{key.upcase} = new( :#{key}, #{index} )
|
125
|
+
|
126
|
+
def #{key}?
|
127
|
+
self == #{key.upcase}
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.#{key}
|
131
|
+
#{key.upcase}
|
132
|
+
end
|
133
|
+
RUBY
|
134
|
+
end
|
135
|
+
|
136
|
+
klass.class_eval( <<RUBY )
|
137
|
+
def self.keys
|
138
|
+
@keys ||= #{keys}.freeze
|
139
|
+
end
|
140
|
+
|
141
|
+
def self.members
|
142
|
+
@members ||= [#{keys.map {|key|key.upcase}.join(',')}].freeze
|
143
|
+
end
|
144
|
+
RUBY
|
145
|
+
|
146
|
+
## note: use Safe (module) and NOT Object for namespacing
|
147
|
+
## use include Enum to make all enums and convenience converters global
|
148
|
+
|
149
|
+
## add global convenience converter function
|
150
|
+
## e.g. State(0) is same as State.convert(0)
|
151
|
+
Safe.class_eval( <<RUBY )
|
152
|
+
def #{class_name}( arg )
|
153
|
+
#{class_name}.convert( arg )
|
154
|
+
end
|
155
|
+
RUBY
|
156
|
+
|
157
|
+
Safe.const_set( class_name, klass ) ## returns klass (plus sets global constant class name)
|
158
|
+
end
|
159
|
+
|
160
|
+
class << self
|
161
|
+
alias_method :old_new, :new # note: store "old" orginal version of new
|
162
|
+
alias_method :new, :build_class # replace original version with create
|
163
|
+
end
|
164
|
+
end # class Enum
|
165
|
+
end # module Safe
|
166
|
+
|
167
|
+
|
168
|
+
puts Enums.banner # say hello
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
## note: use a (new) class Enums (with s!! at the end) for version info
|
4
|
+
class Enums
|
5
|
+
|
6
|
+
MAJOR = 0
|
7
|
+
MINOR = 0
|
8
|
+
PATCH = 1
|
9
|
+
VERSION = [MAJOR,MINOR,PATCH].join('.')
|
10
|
+
|
11
|
+
def self.version
|
12
|
+
VERSION
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.banner
|
16
|
+
"enums/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.root
|
20
|
+
"#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
|
21
|
+
end
|
22
|
+
|
23
|
+
end # class Enums
|
data/test/helper.rb
ADDED
data/test/test_enum.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_enum.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
class TestEnum < MiniTest::Test
|
12
|
+
|
13
|
+
include Safe ## make all enums (and "convenience" converters) global
|
14
|
+
|
15
|
+
|
16
|
+
def test_state
|
17
|
+
pp Enum.new( 'State', :fundraising, :expired_refund, :successful )
|
18
|
+
|
19
|
+
|
20
|
+
puts "Safe.constants:"
|
21
|
+
pp Safe.constants
|
22
|
+
puts "Enum.constants:"
|
23
|
+
pp Enum.constants
|
24
|
+
puts "State.constants:"
|
25
|
+
pp State.constants
|
26
|
+
|
27
|
+
|
28
|
+
pp State.values
|
29
|
+
pp State.keys
|
30
|
+
## pp State(0)
|
31
|
+
|
32
|
+
pp State.members
|
33
|
+
pp State.members[0].key
|
34
|
+
pp State.members[0].value
|
35
|
+
pp State.members[1].key
|
36
|
+
pp State.members[1].value
|
37
|
+
|
38
|
+
pp State.fundraising.value
|
39
|
+
pp State.fundraising.key
|
40
|
+
pp State::FUNDRAISING.value
|
41
|
+
pp State::FUNDRAISING.key
|
42
|
+
|
43
|
+
|
44
|
+
pp State
|
45
|
+
state = State.fundraising
|
46
|
+
pp state.fundraising?
|
47
|
+
pp state
|
48
|
+
pp state.value
|
49
|
+
|
50
|
+
|
51
|
+
pp State(0)
|
52
|
+
pp State(1)
|
53
|
+
pp State(2)
|
54
|
+
pp State(3)
|
55
|
+
|
56
|
+
pp State.zero
|
57
|
+
pp State(0) == State.zero
|
58
|
+
pp State(1) == State.zero
|
59
|
+
|
60
|
+
pp State.value(0)
|
61
|
+
pp State.key(:fundraising)
|
62
|
+
pp State[:fundraising]
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_color
|
66
|
+
pp Enum.new( 'Color', :red, :green, :blue )
|
67
|
+
pp Color.zero
|
68
|
+
pp Color(0)
|
69
|
+
pp Color.red
|
70
|
+
pp Color.values
|
71
|
+
pp Color.keys
|
72
|
+
pp Color.constants
|
73
|
+
pp Color.members
|
74
|
+
pp Color(1)
|
75
|
+
pp Color.value(1)
|
76
|
+
pp Color.key(:red)
|
77
|
+
pp Color[:red]
|
78
|
+
color = Color.red
|
79
|
+
pp color.red?
|
80
|
+
pp color == Color.red
|
81
|
+
pp color.blue?
|
82
|
+
pp color == Color.blue
|
83
|
+
|
84
|
+
pp Color::RED
|
85
|
+
pp Color.members
|
86
|
+
end
|
87
|
+
|
88
|
+
end # class TestEnum
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enums
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gerald Bauer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rdoc
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hoe
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.16'
|
41
|
+
description: enums - safe enumeration types - a set of symbolic keys bound to unique
|
42
|
+
integer numbers
|
43
|
+
email: wwwmake@googlegroups.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files:
|
47
|
+
- CHANGELOG.md
|
48
|
+
- LICENSE.md
|
49
|
+
- Manifest.txt
|
50
|
+
- README.md
|
51
|
+
files:
|
52
|
+
- CHANGELOG.md
|
53
|
+
- LICENSE.md
|
54
|
+
- Manifest.txt
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- lib/enums.rb
|
58
|
+
- lib/enums/version.rb
|
59
|
+
- test/helper.rb
|
60
|
+
- test/test_enum.rb
|
61
|
+
homepage: https://github.com/s6ruby/enums
|
62
|
+
licenses:
|
63
|
+
- Public Domain
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options:
|
67
|
+
- "--main"
|
68
|
+
- README.md
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.2.2
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.5.2
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: enums - safe enumeration types - a set of symbolic keys bound to unique integer
|
87
|
+
numbers
|
88
|
+
test_files: []
|