minenum 0.1.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/.rspec +3 -0
- data/.rubocop.yml +18 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/Rakefile +8 -0
- data/Steepfile +9 -0
- data/lib/minenum/enum/base.rb +14 -0
- data/lib/minenum/enum/class_builder.rb +51 -0
- data/lib/minenum/enum/values.rb +52 -0
- data/lib/minenum/enum.rb +3 -0
- data/lib/minenum/model.rb +83 -0
- data/lib/minenum/version.rb +5 -0
- data/lib/minenum.rb +4 -0
- data/minenum.gemspec +39 -0
- data/sig/minenum/enum/base.rbs +9 -0
- data/sig/minenum/enum/class_builder.rbs +19 -0
- data/sig/minenum/enum/values.rbs +16 -0
- data/sig/minenum/model.rbs +24 -0
- data/sig/minenum.rbs +4 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 62775a6aa94437fc897c0ced4c9f7209cd279ab4c51f55793c3dbd218a4cf492
|
4
|
+
data.tar.gz: 5fd974a1b3efb463a6b39aea2a5206c7acc013c729acdc80a590e0e6b1011e3c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c75161d38a61f3e67c83f7ade775ea3c9de48fc9f1a90f991c77835f6469be11bd3bbbf947a3fe7cd5d375ada00d8a56be320565c6516eb4fd62bf31d25f912
|
7
|
+
data.tar.gz: 9706434cf641116c550cc341686059721495ec9dcb9528077562fccd90e5f08ea6f45fd7dc39caaa0990a74282b06accd63e48052fb03181b6f5f76a06cfd705
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# The behavior of RuboCop can be controlled via the .rubocop.yml
|
2
|
+
# configuration file. It makes it possible to enable/disable
|
3
|
+
# certain cops (checks) and to alter their behavior if they accept
|
4
|
+
# any parameters. The file can be placed either in your home
|
5
|
+
# directory or in some project directory.
|
6
|
+
#
|
7
|
+
# RuboCop will start looking for the configuration file in the directory
|
8
|
+
# where the inspected file is and continue its way up to the root directory.
|
9
|
+
#
|
10
|
+
# See https://docs.rubocop.org/rubocop/configuration
|
11
|
+
|
12
|
+
require: rubocop-rspec
|
13
|
+
|
14
|
+
AllCops:
|
15
|
+
NewCops: enable
|
16
|
+
|
17
|
+
RSpec/NestedGroups:
|
18
|
+
Max: 4
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Loose Coupling
|
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,59 @@
|
|
1
|
+
# Minenum
|
2
|
+
|
3
|
+
Minenum is a gem that enhances Ruby objects by adding enum functionality and returning enum objects as return values. Enums simplify representing states or types within a specific range of values and can be returned directly from methods.
|
4
|
+
|
5
|
+
Features:
|
6
|
+
|
7
|
+
* Return enum objects directly from methods, enabling clearer and more expressive code.
|
8
|
+
* Simplify code by encapsulating valid value ranges within enums.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Install the gem and add to the application's Gemfile by executing:
|
13
|
+
|
14
|
+
$ bundle add minenum
|
15
|
+
|
16
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
17
|
+
|
18
|
+
$ gem install minenum
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class Shirt
|
24
|
+
include Minenum::Model
|
25
|
+
|
26
|
+
enum :size, { small: 1, medium: 2, large: 3 }
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
shirt = Shirt.new
|
32
|
+
shirt.size = 1
|
33
|
+
shirt.size.name #=> :small
|
34
|
+
shirt.size.small? #=> true
|
35
|
+
|
36
|
+
# You can also set the name
|
37
|
+
shirt.size = :medium
|
38
|
+
shirt.size.name #=> :medium
|
39
|
+
shirt.size.medium? #=> true
|
40
|
+
```
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
# You can get the enum values
|
44
|
+
Shirt.size.values #=> { small: 1, medium: 2, large: 3 }
|
45
|
+
```
|
46
|
+
|
47
|
+
## Development
|
48
|
+
|
49
|
+
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.
|
50
|
+
|
51
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nowlinuxing/minenum.
|
56
|
+
|
57
|
+
## License
|
58
|
+
|
59
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/Steepfile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
require_relative 'values'
|
5
|
+
|
6
|
+
module Minenum
|
7
|
+
module Enum
|
8
|
+
class ClassBuilder # :nodoc:
|
9
|
+
def self.build(values = {})
|
10
|
+
new(values).build
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(values)
|
14
|
+
@values = values
|
15
|
+
end
|
16
|
+
|
17
|
+
def build
|
18
|
+
values = Values.new(@values)
|
19
|
+
|
20
|
+
klass = Class.new(Base)
|
21
|
+
add_values_method(klass, values)
|
22
|
+
add_name_method(klass, values)
|
23
|
+
add_predicate_methods(klass, values)
|
24
|
+
|
25
|
+
klass
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def add_values_method(klass, values)
|
31
|
+
klass.singleton_class.define_method(:values) do
|
32
|
+
values.values
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_name_method(klass, values)
|
37
|
+
klass.define_method(:name) do
|
38
|
+
values.key(@value)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_predicate_methods(klass, values)
|
43
|
+
values.each_key do |key|
|
44
|
+
klass.define_method("#{key}?") do
|
45
|
+
values.match?(key, @value)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Minenum
|
4
|
+
module Enum
|
5
|
+
class Values # :nodoc:
|
6
|
+
attr_reader :values
|
7
|
+
|
8
|
+
def initialize(values)
|
9
|
+
@values = values.transform_keys(&:to_sym).tap(&:freeze)
|
10
|
+
freeze
|
11
|
+
end
|
12
|
+
|
13
|
+
def each_key(...)
|
14
|
+
@values.each_key(...)
|
15
|
+
end
|
16
|
+
|
17
|
+
def key(key_or_value)
|
18
|
+
if key_or_value.respond_to?(:to_sym) && (key_sym = key_or_value.to_sym) && @values.key?(key_sym)
|
19
|
+
key_sym
|
20
|
+
else
|
21
|
+
return @values.key(key_or_value) if @values.value?(key_or_value)
|
22
|
+
|
23
|
+
case key_or_value
|
24
|
+
when Symbol then @values.key(key_or_value.to_s)
|
25
|
+
when String then @values.key(key_or_value.to_sym)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def match?(key_name, key_or_value)
|
31
|
+
if key_or_value.respond_to?(:to_sym) && (key_sym = key_or_value.to_sym) && @values.key?(key_sym)
|
32
|
+
key_sym == key_name.to_sym
|
33
|
+
else
|
34
|
+
match_with_value?(key_name, key_or_value)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def match_with_value?(key_name, value)
|
41
|
+
registered_value = @values[key_name.to_sym]
|
42
|
+
return true if value == registered_value
|
43
|
+
|
44
|
+
case value
|
45
|
+
when Symbol then value.to_s == registered_value
|
46
|
+
when String then value.to_sym == registered_value
|
47
|
+
else false
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/minenum/enum.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'enum'
|
4
|
+
|
5
|
+
module Minenum
|
6
|
+
# = Minenum
|
7
|
+
#
|
8
|
+
# Minenum enhances Ruby objects by adding enum functionality.
|
9
|
+
#
|
10
|
+
# class Shirt
|
11
|
+
# include Minenum::Model
|
12
|
+
#
|
13
|
+
# enum :size, { small: 1, medium: 2, large: 3 }
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# shirt = Shirt.new
|
17
|
+
# shirt.size = 1
|
18
|
+
# shirt.size.name #=> :small
|
19
|
+
# shirt.size.small? #=> true
|
20
|
+
#
|
21
|
+
# # You can also set the name
|
22
|
+
# shirt.size = :medium
|
23
|
+
# shirt.size.name #=> :medium
|
24
|
+
# shirt.size.medium? #=> true
|
25
|
+
#
|
26
|
+
# # You can get the enum values
|
27
|
+
# Shirt.size.values #=> { small: 1, medium: 2, large: 3 }
|
28
|
+
#
|
29
|
+
module Model
|
30
|
+
module InstanceVariableAccessor # :nodoc:
|
31
|
+
def set(model, name, value)
|
32
|
+
enum = if model.instance_variable_defined?(:@_minenum_enum)
|
33
|
+
model.instance_variable_get(:@_minenum_enum)
|
34
|
+
else
|
35
|
+
model.instance_variable_set(:@_minenum_enum, {})
|
36
|
+
end
|
37
|
+
enum[name] = value
|
38
|
+
end
|
39
|
+
module_function :set
|
40
|
+
|
41
|
+
def get(model, name)
|
42
|
+
enum = model.instance_variable_get(:@_minenum_enum)
|
43
|
+
enum&.[](name)
|
44
|
+
end
|
45
|
+
module_function :get
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.included(base)
|
49
|
+
base.extend ClassMethods
|
50
|
+
end
|
51
|
+
|
52
|
+
module ClassMethods # :nodoc:
|
53
|
+
def enum(name, values, adapter: InstanceVariableAccessor)
|
54
|
+
enum_class = Enum::ClassBuilder.build(values)
|
55
|
+
|
56
|
+
const_set(classify(name.to_s), enum_class)
|
57
|
+
singleton_class.define_method(name) { enum_class }
|
58
|
+
|
59
|
+
enum_methods_module.define_method("#{name}=") do |value|
|
60
|
+
adapter.set(self, name, value)
|
61
|
+
end
|
62
|
+
|
63
|
+
enum_methods_module.define_method(name) do
|
64
|
+
enum_class.new(adapter.get(self, name))
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def enum_methods_module
|
71
|
+
@enum_methods_module ||= begin
|
72
|
+
mod = Module.new
|
73
|
+
include mod
|
74
|
+
mod
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def classify(key)
|
79
|
+
key.gsub(/([a-z\d]+)_?/) { ::Regexp.last_match(1)&.capitalize }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
data/lib/minenum.rb
ADDED
data/minenum.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/minenum/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'minenum'
|
7
|
+
spec.version = Minenum::VERSION
|
8
|
+
spec.authors = ['Loose Coupling']
|
9
|
+
spec.email = ['loosecplg@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Enum for Ruby'
|
12
|
+
spec.description = 'Enum for Ruby'
|
13
|
+
spec.homepage = 'https://github.com/nowlinuxing/minenum'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = '>= 2.7.0'
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
19
|
+
spec.metadata['changelog_uri'] = "#{spec.homepage}/CHANGELOG.md"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(__dir__) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(File.expand_path(f) == __FILE__) ||
|
26
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = 'exe'
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ['lib']
|
32
|
+
|
33
|
+
# Uncomment to register a new dependency of your gem
|
34
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
35
|
+
|
36
|
+
# For more information and examples about making a new gem, check out our
|
37
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
38
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
39
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Minenum
|
2
|
+
module Enum
|
3
|
+
module ClassBuilder
|
4
|
+
def self.build: (Hash[String | Symbol, untyped]) -> singleton(Minenum::Enum::Base)
|
5
|
+
def self.new: (Hash[String | Symbol, untyped]) -> ClassBuilder
|
6
|
+
|
7
|
+
@values: Hash[String | Symbol, untyped]
|
8
|
+
|
9
|
+
def initialize: (Hash[String | Symbol, untyped]) -> void
|
10
|
+
def build: -> untyped
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def add_values_method: (untyped, Minenum::Enum::Values) -> void
|
15
|
+
def add_name_method: (untyped, Minenum::Enum::Values) -> void
|
16
|
+
def add_predicate_methods: (untyped, Minenum::Enum::Values) -> void
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Minenum
|
2
|
+
module Enum
|
3
|
+
class Values
|
4
|
+
attr_reader values: Hash[Symbol, untyped]
|
5
|
+
|
6
|
+
def initialize: (Hash[String | Symbol, untyped]) -> void
|
7
|
+
def each_key: { (Symbol) -> untyped } -> Hash[String | Symbol, untyped]
|
8
|
+
def key: (untyped) -> Symbol?
|
9
|
+
def match?: (String | Symbol, untyped) -> bool
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def match_with_value?: (String | Symbol, untyped) -> bool
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Minenum
|
2
|
+
module Model
|
3
|
+
interface _Accessor
|
4
|
+
def set: (untyped model, String | Symbol name, untyped value) -> untyped
|
5
|
+
def get: (untyped model, String | Symbol name) -> untyped
|
6
|
+
end
|
7
|
+
|
8
|
+
module InstanceVariableAccessor
|
9
|
+
include _Accessor
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def enum: (String | Symbol name, Hash[String | Symbol, untyped], ?adapter: _Accessor) -> void
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def enum_methods_module: -> Module
|
18
|
+
def classify: (String) -> String
|
19
|
+
|
20
|
+
def const_set: [T] (String | Symbol, T) -> T
|
21
|
+
def include: (Module) -> self
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/sig/minenum.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minenum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Loose Coupling
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-04-22 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Enum for Ruby
|
14
|
+
email:
|
15
|
+
- loosecplg@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- Steepfile
|
27
|
+
- lib/minenum.rb
|
28
|
+
- lib/minenum/enum.rb
|
29
|
+
- lib/minenum/enum/base.rb
|
30
|
+
- lib/minenum/enum/class_builder.rb
|
31
|
+
- lib/minenum/enum/values.rb
|
32
|
+
- lib/minenum/model.rb
|
33
|
+
- lib/minenum/version.rb
|
34
|
+
- minenum.gemspec
|
35
|
+
- sig/minenum.rbs
|
36
|
+
- sig/minenum/enum/base.rbs
|
37
|
+
- sig/minenum/enum/class_builder.rbs
|
38
|
+
- sig/minenum/enum/values.rbs
|
39
|
+
- sig/minenum/model.rbs
|
40
|
+
homepage: https://github.com/nowlinuxing/minenum
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
homepage_uri: https://github.com/nowlinuxing/minenum
|
45
|
+
source_code_uri: https://github.com/nowlinuxing/minenum
|
46
|
+
changelog_uri: https://github.com/nowlinuxing/minenum/CHANGELOG.md
|
47
|
+
rubygems_mfa_required: 'true'
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 2.7.0
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.5.3
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Enum for Ruby
|
67
|
+
test_files: []
|