meta_enum 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +10 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/lib/meta_enum/missing_value.rb +30 -0
- data/lib/meta_enum/type.rb +75 -0
- data/lib/meta_enum/value.rb +28 -0
- data/lib/meta_enum/version.rb +3 -0
- data/lib/meta_enum.rb +4 -0
- data/meta_enum.gemspec +26 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2613b743e3671c160c9e9402e181e49e300b4cbf0d89e8c42ed93299f8095a2f
|
4
|
+
data.tar.gz: 4fb9192c6bcfb1096a6b605a574f975e00247662670b7cf51e2744a5867ff9ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 712fc2fddf6c361a890da5c24ce063556b0289d8bf437ccbc68b7051f501e7839a4926dafddc83bfbca055e8e206797c20117865586990f9e5b7c5eea75e0e26
|
7
|
+
data.tar.gz: a43af11ac01bd954776e9ddb41d557c06707cb1941e8374c7bafcffbb4b1845968699528dd56b7e5a0733e32c085b581f7a5b9c540c0d20f66684b5cff40acca
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
meta_enum (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.2)
|
10
|
+
method_source (0.9.0)
|
11
|
+
minitest (5.11.1)
|
12
|
+
pry (0.11.3)
|
13
|
+
coderay (~> 1.1.0)
|
14
|
+
method_source (~> 0.9.0)
|
15
|
+
rake (10.4.2)
|
16
|
+
|
17
|
+
PLATFORMS
|
18
|
+
ruby
|
19
|
+
|
20
|
+
DEPENDENCIES
|
21
|
+
bundler (~> 1.16)
|
22
|
+
meta_enum!
|
23
|
+
minitest (~> 5.0)
|
24
|
+
pry (~> 0.11.3)
|
25
|
+
rake (~> 10.0)
|
26
|
+
|
27
|
+
BUNDLED WITH
|
28
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jack Christensen
|
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,90 @@
|
|
1
|
+
# MetaEnum
|
2
|
+
|
3
|
+
MetaEnum is a library for handling enum types in Ruby. It makes it easy to
|
4
|
+
convert between external numbers and internal names.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'meta_enum'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install meta_enum
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Create a enum type:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'meta_enum'
|
28
|
+
|
29
|
+
ColorType = MetaEnum::Type.new(red: 0, green: 1, blue: 2)
|
30
|
+
```
|
31
|
+
|
32
|
+
Use the `[]` operator to lookup a value by name:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
ColorType[:green] # => #<MetaEnum::Value: 1 => green}>
|
36
|
+
```
|
37
|
+
|
38
|
+
Or lookup by number:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
ColorType[1] # => #<MetaEnum::Value: 1 => green}>
|
42
|
+
```
|
43
|
+
|
44
|
+
Values can also be easily compared with numbers or names:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
ColorType[:green] == 1 # => true
|
48
|
+
ColorType[:green] == :green # => true
|
49
|
+
```
|
50
|
+
|
51
|
+
Missing names would almost always be a programming error, so that will raise an exception.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
ColorType[:purple] # => raises: KeyError: key not found: :purple
|
55
|
+
```
|
56
|
+
|
57
|
+
But missing numbers could mean that there are values defined externally we do not know about. So it is preferable not to raise an exception.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
ColorType[42] # => #<MetaEnum::MissingValue: 42}>
|
61
|
+
```
|
62
|
+
|
63
|
+
Number and name can be retrieved from a `MetaEnum::Value`
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
v = ColorType[:red] # => #<MetaEnum::Value: 0 => red}>
|
67
|
+
v.number # => 0
|
68
|
+
v.name # => :red
|
69
|
+
```
|
70
|
+
|
71
|
+
Values can have extra associated data.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
AgeType = MetaEnum::Type.new(child: [0, "Less than 18"], adult: [1, "At least 18"])
|
75
|
+
AgeType[:child].data # => "Less than 18"
|
76
|
+
```
|
77
|
+
|
78
|
+
## Development
|
79
|
+
|
80
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
81
|
+
|
82
|
+
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).
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ccsalespro/meta_enum.
|
87
|
+
|
88
|
+
## License
|
89
|
+
|
90
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module MetaEnum
|
2
|
+
class MissingValue
|
3
|
+
attr_reader :number, :type
|
4
|
+
|
5
|
+
def initialize(number, type)
|
6
|
+
@number = Integer(number)
|
7
|
+
@type = type
|
8
|
+
freeze
|
9
|
+
end
|
10
|
+
|
11
|
+
def name; :missing_value; end
|
12
|
+
def data; nil; end
|
13
|
+
|
14
|
+
def ==(other)
|
15
|
+
other = type[other]
|
16
|
+
number == other.number && type == other.type
|
17
|
+
|
18
|
+
# type[] will raise for certain bad keys. Those are obviously not equal so return false.
|
19
|
+
rescue ArgumentError, KeyError
|
20
|
+
false
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_i; number; end
|
24
|
+
def to_s; name.to_s; end
|
25
|
+
|
26
|
+
def inspect
|
27
|
+
"#<#{self.class}: #{number}}>"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module MetaEnum
|
4
|
+
class Type
|
5
|
+
attr_reader :values, :values_by_number, :values_by_name
|
6
|
+
|
7
|
+
# Initialize takes a single hash of name to number.
|
8
|
+
#
|
9
|
+
# e.g. MetaEnum::Type.new(red: 0, green: 1, blue: 2)
|
10
|
+
#
|
11
|
+
# Additional data can also be associated with each value by passing an array
|
12
|
+
# of [number, extra data]. This can be used for additional description or
|
13
|
+
# any other reason.
|
14
|
+
#
|
15
|
+
# e.g. MetaEnum::Type.new(small: [0, "Less than 10], large: [1, "At least 10"]
|
16
|
+
def initialize(values)
|
17
|
+
@values_by_number = {}
|
18
|
+
@values_by_name = {}
|
19
|
+
@values = Set.new
|
20
|
+
|
21
|
+
values.each do |name, number_and_data|
|
22
|
+
number_and_data = Array(number_and_data)
|
23
|
+
v = Value.new number_and_data[0], name, number_and_data[1], self
|
24
|
+
raise ArgumentError, "duplicate number: #{v.number}" if @values_by_number.key? v.number
|
25
|
+
raise ArgumentError, "duplicate name: #{v.name}" if @values_by_name.key? v.name
|
26
|
+
@values_by_number[v.number] = v
|
27
|
+
@values_by_name[v.name] = v
|
28
|
+
@values.add(v)
|
29
|
+
end
|
30
|
+
|
31
|
+
@values_by_number.freeze
|
32
|
+
@values_by_name.freeze
|
33
|
+
@values.freeze
|
34
|
+
freeze
|
35
|
+
end
|
36
|
+
|
37
|
+
# [] is a "do what I mean" operator. It returns the Value from this type depending on the key.
|
38
|
+
#
|
39
|
+
# When key is a symbol, it is considered the name of the Value to return.
|
40
|
+
# Since symbols are used from number, it is considered an error if the key is
|
41
|
+
# not found and it raises an exception.
|
42
|
+
#
|
43
|
+
# When key can be converted to an integer by Integer(), then it is
|
44
|
+
# considered the number of the Value to return. Retrieving by number is
|
45
|
+
# presumed to converting from external data where a missing value should not
|
46
|
+
# be considered fatal. In this case it returns a MissingValue is with number
|
47
|
+
# as the key. This allows a Type to only specify the values is needs while
|
48
|
+
# passing through the others unmodified.
|
49
|
+
#
|
50
|
+
# Finally, when key is a MetaEnum::Value, it is simply returned (unless it
|
51
|
+
# belongs to a different Type in which case an ArgumentError is raised).
|
52
|
+
#
|
53
|
+
# See #values_by_number and #values_by_name for non-fuzzy value selection.
|
54
|
+
def [](key)
|
55
|
+
case key
|
56
|
+
when Value, MissingValue
|
57
|
+
raise ArgumentError, "wrong type" unless key.type == self
|
58
|
+
key
|
59
|
+
when Symbol
|
60
|
+
values_by_name.fetch(key)
|
61
|
+
else
|
62
|
+
key = Integer(key)
|
63
|
+
values_by_number.fetch(key) { MissingValue.new key, self }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def inspect
|
68
|
+
sprintf('#<%s: {%s}>', self.class, values.to_a.map { |v| "#{v.name}: #{v.number}"}.join(", "))
|
69
|
+
end
|
70
|
+
|
71
|
+
def size
|
72
|
+
values.size
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module MetaEnum
|
2
|
+
class Value
|
3
|
+
attr_reader :number, :name, :data, :type
|
4
|
+
|
5
|
+
def initialize(number, name, data, type)
|
6
|
+
@number = Integer(number)
|
7
|
+
@name = name.to_sym
|
8
|
+
@data = data
|
9
|
+
@type = type
|
10
|
+
freeze
|
11
|
+
end
|
12
|
+
|
13
|
+
def ==(other)
|
14
|
+
equal?(other) || equal?(type[other])
|
15
|
+
|
16
|
+
# type[] will raise for certain bad keys. Those are obviously not equal so return false.
|
17
|
+
rescue ArgumentError, KeyError
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_i; number; end
|
22
|
+
def to_s; name.to_s; end
|
23
|
+
|
24
|
+
def inspect
|
25
|
+
"#<#{self.class}: #{name}: #{number}, data: #{data.inspect}>"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/meta_enum.rb
ADDED
data/meta_enum.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "meta_enum/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "meta_enum"
|
7
|
+
spec.version = MetaEnum::VERSION
|
8
|
+
spec.authors = ["Jack Christensen"]
|
9
|
+
spec.email = ["jack@jackchristensen.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Friendly enum type system}
|
12
|
+
spec.homepage = "https://github.com/ccsalespro/meta_enum"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
spec.add_development_dependency "pry", "~> 0.11.3"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meta_enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jack Christensen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-26 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.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.11.3
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.11.3
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- jack@jackchristensen.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/meta_enum.rb
|
86
|
+
- lib/meta_enum/missing_value.rb
|
87
|
+
- lib/meta_enum/type.rb
|
88
|
+
- lib/meta_enum/value.rb
|
89
|
+
- lib/meta_enum/version.rb
|
90
|
+
- meta_enum.gemspec
|
91
|
+
homepage: https://github.com/ccsalespro/meta_enum
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.7.3
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Friendly enum type system
|
115
|
+
test_files: []
|