enum_class 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +155 -0
- data/Rakefile +6 -0
- data/bin/console +13 -0
- data/bin/setup +8 -0
- data/enum_class.gemspec +25 -0
- data/lib/enum.rb +246 -0
- data/lib/enum_class/version.rb +3 -0
- data/lib/enum_class.rb +7 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fb01aaee4990bae960203ed343c56c3138dafc9d
|
4
|
+
data.tar.gz: fac9f38332e3d05bdd69346debd68dbac255d173
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 519b31b4e7485d849ad9fd6c368ee996206d1f8109564bb420d8cf2fec6b8110e80754a746703476df985dba6ede49519d5214b7d0ff30d20d771c0ecb053b64
|
7
|
+
data.tar.gz: 3c297a574c757079148b82f891f5597e57e9d4b70da6a284a7575a6b2d8ae072029f21b5dc70455e96733f8ab1a9fb7b1af688c5c41665f059bffd7b6f5980a4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at michele.piccirillo@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Michele Piccirillo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
# EnumClass
|
2
|
+
|
3
|
+
This gem is an attempt to provide a lightweight solution for __object-oriented enumerations in Ruby__. It consists
|
4
|
+
in a single class (`Enum`), that can be subclassed to create enumerations and comes with no dependencies. It's similar
|
5
|
+
in design to some [other](https://github.com/capnregex/enum) [gems](https://github.com/dblock/ruby-enum) available, but
|
6
|
+
it stemmed out from the necessity of handling reliably missing features like inheritance and instance methods of the enumerated
|
7
|
+
object.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'enum_class'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install enum_class
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Basic usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Foo < Enum
|
31
|
+
value :A
|
32
|
+
value :B
|
33
|
+
end
|
34
|
+
|
35
|
+
Foo.inspect # => Foo(A, B)
|
36
|
+
|
37
|
+
# Instances are accessible as constants
|
38
|
+
Foo::A.class # => Foo
|
39
|
+
Foo::B.class # => Foo
|
40
|
+
Foo::C # => raises NameError
|
41
|
+
|
42
|
+
# Enumerated values are implicitly ordered, depending on order of definition
|
43
|
+
Foo::A == Foo::B # => false
|
44
|
+
Foo::A < Foo::B # => true
|
45
|
+
|
46
|
+
# Instances can be retrieved by key
|
47
|
+
Foo[:a] # => Foo::A
|
48
|
+
Foo['a'] # => Foo::A
|
49
|
+
Foo.for('A') # => Foo::A
|
50
|
+
Foo.for('X') # => raises KeyError
|
51
|
+
|
52
|
+
Foo.new # => raises an error
|
53
|
+
|
54
|
+
# Helper methods
|
55
|
+
Foo.a # => Foo::A
|
56
|
+
Foo.b # => Foo::B
|
57
|
+
|
58
|
+
Foo::A.a? # => true
|
59
|
+
Foo::A.b? # => false
|
60
|
+
```
|
61
|
+
|
62
|
+
### Creating enums with a shortcut
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
Bar = Enum.create(:a, :b, :c) # => Bar(A, B, C)
|
66
|
+
```
|
67
|
+
|
68
|
+
### Custom initializer
|
69
|
+
```ruby
|
70
|
+
class Foobar < Enum
|
71
|
+
def initialize(name)
|
72
|
+
@name = name
|
73
|
+
end
|
74
|
+
|
75
|
+
attr_reader :name
|
76
|
+
|
77
|
+
value :A, 'foo'
|
78
|
+
value :B, 'bar'
|
79
|
+
end
|
80
|
+
|
81
|
+
Foobar::A.name # => 'foo'
|
82
|
+
Foobar::B.name # => 'bar'
|
83
|
+
```
|
84
|
+
|
85
|
+
### Inheritance
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class Base < Enum
|
89
|
+
value :A
|
90
|
+
value :B
|
91
|
+
end
|
92
|
+
|
93
|
+
class Child < Base
|
94
|
+
value :C
|
95
|
+
end
|
96
|
+
|
97
|
+
Base.inspect # => Base(A, B)
|
98
|
+
Child.inspect # => Child(A, B, C)
|
99
|
+
```
|
100
|
+
|
101
|
+
:warning: If you reopen the base class later, the changes won't be reflected in the child.
|
102
|
+
The values to inherit are evaluated at the time of the class definition.
|
103
|
+
|
104
|
+
### Advanced inheritance with custom initializer
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
class Base < Enum
|
108
|
+
def initialize(name)
|
109
|
+
@name = name
|
110
|
+
end
|
111
|
+
|
112
|
+
attr_reader :name
|
113
|
+
|
114
|
+
value :A, 'foo'
|
115
|
+
value :B, 'bar'
|
116
|
+
end
|
117
|
+
|
118
|
+
class Child < Base
|
119
|
+
def initialize(name, number)
|
120
|
+
super(name)
|
121
|
+
@number = number
|
122
|
+
end
|
123
|
+
|
124
|
+
def initialize_from_superclass(superclass_enum_value)
|
125
|
+
super # default behavior is to copy all the instance variables
|
126
|
+
@number = superclass_enum_value.ord + 1
|
127
|
+
end
|
128
|
+
|
129
|
+
attr_reader :number
|
130
|
+
|
131
|
+
value :C, 'baz', 3
|
132
|
+
end
|
133
|
+
|
134
|
+
Child::C.name # => 'baz'
|
135
|
+
Child::C.number # => 3
|
136
|
+
|
137
|
+
Child::B.name # => 'bar'
|
138
|
+
Child::B.number # => 2
|
139
|
+
```
|
140
|
+
|
141
|
+
## Development
|
142
|
+
|
143
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
144
|
+
|
145
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
146
|
+
|
147
|
+
## Contributing
|
148
|
+
|
149
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/LIQIDTechnology/enum_class. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
150
|
+
|
151
|
+
|
152
|
+
## License
|
153
|
+
|
154
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
155
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "enum_class"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
|
11
|
+
require "pry"
|
12
|
+
Pry.start
|
13
|
+
|
data/bin/setup
ADDED
data/enum_class.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enum_class/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "enum_class"
|
8
|
+
spec.version = EnumClass::VERSION
|
9
|
+
spec.authors = ["Michele Piccirillo"]
|
10
|
+
spec.email = ["michele@liqid.de"]
|
11
|
+
|
12
|
+
spec.summary = 'Object-oriented enumerations for Ruby'
|
13
|
+
spec.homepage = "https://github.com/LIQIDTechnology/enum_class"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
end
|
data/lib/enum.rb
ADDED
@@ -0,0 +1,246 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# An Enum is a class with a predefinite set of instances.
|
4
|
+
# Instances have an implicit ordering (dependent on the order of definition), can be retrieved by key and be enumerated.
|
5
|
+
#
|
6
|
+
# @example Basic usage
|
7
|
+
# class Foo < Enum
|
8
|
+
# value :A
|
9
|
+
# value :B
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# Foo.inspect # => Foo(A, B)
|
13
|
+
#
|
14
|
+
# # Instances are accessible as constants
|
15
|
+
# Foo::A.class # => Foo
|
16
|
+
# Foo::B.class # => Foo
|
17
|
+
# Foo::C # => raises NameError
|
18
|
+
#
|
19
|
+
# # Enumerated values are implicitly ordered, depending on order of definition
|
20
|
+
# Foo::A == Foo::B # => false
|
21
|
+
# Foo::A < Foo::B # => true
|
22
|
+
#
|
23
|
+
# # Instances can be retrieved by key
|
24
|
+
# Foo[:a] # => Foo::A
|
25
|
+
# Foo['a'] # => Foo::A
|
26
|
+
# Foo.for('A') # => Foo::A
|
27
|
+
# Foo.for('X') # => raises KeyError
|
28
|
+
#
|
29
|
+
# Foo.new # => raises an error
|
30
|
+
#
|
31
|
+
# # Helper methods
|
32
|
+
# Foo.a # => Foo::A
|
33
|
+
# Foo.b # => Foo::B
|
34
|
+
#
|
35
|
+
# Foo::A.a? # => true
|
36
|
+
# Foo::A.b? # => false
|
37
|
+
#
|
38
|
+
# @example Creating enums with a shortcut
|
39
|
+
# Bar = Enum.create(:a, :b, :c) # => Bar(A, B, C)
|
40
|
+
#
|
41
|
+
# @example Custom initializer
|
42
|
+
# class Foobar < Enum
|
43
|
+
# def initialize(name)
|
44
|
+
# @name = name
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# attr_reader :name
|
48
|
+
#
|
49
|
+
# value :A, 'foo'
|
50
|
+
# value :B, 'bar'
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# Foobar::A.name # => 'foo'
|
54
|
+
# Foobar::B.name # => 'bar'
|
55
|
+
#
|
56
|
+
# @example Inheritance
|
57
|
+
# class Base < Enum
|
58
|
+
# value :A
|
59
|
+
# value :B
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# class Child < Base
|
63
|
+
# value :C
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# Base.inspect # => Base(A, B)
|
67
|
+
# Child.inspect # => Child(A, B, C)
|
68
|
+
#
|
69
|
+
# # NOTE: If you reopen the base class later, the changes won't be reflected in the child.
|
70
|
+
# # The values to inherit are evaluated at the time of the class definition
|
71
|
+
#
|
72
|
+
# @example Advanced inheritance with custom initializer
|
73
|
+
# class Base < Enum
|
74
|
+
# def initialize(name)
|
75
|
+
# @name = name
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# attr_reader :name
|
79
|
+
#
|
80
|
+
# value :A, 'foo'
|
81
|
+
# value :B, 'bar'
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# class Child < Base
|
85
|
+
# def initialize(name, number)
|
86
|
+
# super(name)
|
87
|
+
# @number = number
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
# def initialize_from_superclass(superclass_enum_value)
|
91
|
+
# super # default behavior is to copy all the instance variables
|
92
|
+
# @number = superclass_enum_value.ord + 1
|
93
|
+
# end
|
94
|
+
#
|
95
|
+
# attr_reader :number
|
96
|
+
#
|
97
|
+
# value :C, 'baz', 3
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# Child::C.name # => 'baz'
|
101
|
+
# Child::C.number # => 3
|
102
|
+
#
|
103
|
+
# Child::B.name # => 'bar'
|
104
|
+
# Child::B.number # => 2
|
105
|
+
class Enum
|
106
|
+
extend Forwardable
|
107
|
+
extend Enumerable
|
108
|
+
include Comparable
|
109
|
+
|
110
|
+
# Raised when a value is declared twice in an Enum
|
111
|
+
class DuplicateEnumKey < StandardError; end
|
112
|
+
|
113
|
+
# The key of this value in the enumeration
|
114
|
+
# @return [Symbol]
|
115
|
+
attr_reader :key
|
116
|
+
|
117
|
+
# Relative ordering index of the instance
|
118
|
+
# @return [Integer]
|
119
|
+
attr_reader :ord
|
120
|
+
|
121
|
+
# Returns a representation of this instance (e.g. `EnumName::VALUE_1`)
|
122
|
+
# @return [String]
|
123
|
+
def inspect
|
124
|
+
"#{self.class.name || 'Enum'}::#{key}"
|
125
|
+
end
|
126
|
+
|
127
|
+
def_delegators :key, :to_s
|
128
|
+
|
129
|
+
# (see Object#hash)
|
130
|
+
def hash
|
131
|
+
key.to_s.hash
|
132
|
+
end
|
133
|
+
|
134
|
+
# Tests the objects for equality.
|
135
|
+
# Two enum values are considered equal if they have the same key and belong to
|
136
|
+
# the same enumeration.
|
137
|
+
#
|
138
|
+
# @param other [Object] object to compare to
|
139
|
+
# @return [Boolean]
|
140
|
+
def ==(other)
|
141
|
+
return false if other.nil?
|
142
|
+
other = self.class.for(other) if other.is_a?(Symbol) || other.is_a?(String)
|
143
|
+
instance_of?(other.class) && key == other.key
|
144
|
+
end
|
145
|
+
|
146
|
+
alias eql? ==
|
147
|
+
alias to_sym key
|
148
|
+
|
149
|
+
# (see Comparable#<=>)
|
150
|
+
# Objects are compared based on their implicit ordering (order of definition).
|
151
|
+
# @see #ord
|
152
|
+
def <=>(other)
|
153
|
+
other = self.class.for(other) if other.is_a?(Symbol) || other.is_a?(String)
|
154
|
+
ord <=> other.ord
|
155
|
+
end
|
156
|
+
|
157
|
+
protected
|
158
|
+
|
159
|
+
# Initializes
|
160
|
+
def initialize_from_superclass(enum_value)
|
161
|
+
enum_value.instance_variables.each do |ivar|
|
162
|
+
instance_variable_set(ivar, enum_value.instance_variable_get(ivar))
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
class << self
|
167
|
+
extend Forwardable
|
168
|
+
|
169
|
+
# @param value [String, Symbol, Class]
|
170
|
+
# @return [self]
|
171
|
+
# @raise [KeyError] if there is no enumerated value with the specified key
|
172
|
+
def for(value)
|
173
|
+
return value if value.is_a? self.class
|
174
|
+
enum_key = value&.upcase&.to_sym
|
175
|
+
values.find(proc { raise KeyError, "Enumerated value not found: #{value.inspect}" }) { |v| v.key == enum_key }
|
176
|
+
end
|
177
|
+
|
178
|
+
# @return [Class]
|
179
|
+
def create(*keys)
|
180
|
+
Class.new(self).tap do |enum|
|
181
|
+
keys.each { |key| enum.value(key) }
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
alias [] for
|
186
|
+
|
187
|
+
def inspect
|
188
|
+
"#{name || 'Enum'}(#{values.map(&:to_s).join(', ')})"
|
189
|
+
end
|
190
|
+
|
191
|
+
def const_missing(sym)
|
192
|
+
self.for(sym)
|
193
|
+
rescue
|
194
|
+
super
|
195
|
+
end
|
196
|
+
|
197
|
+
def_delegators :values, :each, *Enumerable.instance_methods(false)
|
198
|
+
|
199
|
+
protected
|
200
|
+
|
201
|
+
def superclass_values
|
202
|
+
if superclass < Enum
|
203
|
+
superclass.values.map do |superclass_enum_value|
|
204
|
+
inherited_value = allocate
|
205
|
+
inherited_value.send(:initialize_from_superclass, superclass_enum_value)
|
206
|
+
inherited_value
|
207
|
+
end
|
208
|
+
else
|
209
|
+
SortedSet.new
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def values
|
214
|
+
@values ||= SortedSet.new(superclass_values)
|
215
|
+
end
|
216
|
+
|
217
|
+
# Defines a value for the enumeration.
|
218
|
+
#
|
219
|
+
# @param key [Symbol] the name of the enumerated value. Will be a constant on the class
|
220
|
+
# @param args [Array] additional arguments, will be passed to the constructor when
|
221
|
+
# instantiating the enumerated value.
|
222
|
+
# @return [void]
|
223
|
+
#
|
224
|
+
# @!macro [attach] value
|
225
|
+
# @!attribute $1 [r] The value `$1` of the enumeration
|
226
|
+
# @!scope class
|
227
|
+
# @return [self] the singleton instance of the enumeration for this value
|
228
|
+
def value(key, *args)
|
229
|
+
key = key.to_s.upcase.to_sym
|
230
|
+
enum_value = send(:new, *args)
|
231
|
+
enum_value.instance_variable_set(:@key, key)
|
232
|
+
|
233
|
+
raise DuplicateEnumKey, "#{enum_value.inspect} is already defined" if values.include?(enum_value)
|
234
|
+
|
235
|
+
max_ord = values.max&.ord || -1
|
236
|
+
enum_value.instance_variable_set(:@ord, max_ord + 1)
|
237
|
+
|
238
|
+
values << enum_value
|
239
|
+
|
240
|
+
define_method("#{key.downcase}?") { @key == key }
|
241
|
+
define_singleton_method(key.to_s.downcase) { self.for(key) }
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
private_class_method :new
|
246
|
+
end
|
data/lib/enum_class.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enum_class
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michele Piccirillo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- michele@liqid.de
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- enum_class.gemspec
|
87
|
+
- lib/enum.rb
|
88
|
+
- lib/enum_class.rb
|
89
|
+
- lib/enum_class/version.rb
|
90
|
+
homepage: https://github.com/LIQIDTechnology/enum_class
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.5.1
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Object-oriented enumerations for Ruby
|
114
|
+
test_files: []
|