nd-enum 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +21 -0
- data/README.md +33 -0
- data/Rakefile +8 -0
- data/lib/nd/enum/base.rb +52 -0
- data/lib/nd/enum/version.rb +7 -0
- data/lib/nd/enum.rb +60 -0
- data/nd-enum.gemspec +36 -0
- data/sig/nd/enum.rbs +6 -0
- metadata +14 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c3a846e4f2d4625cfa4defce4320e1b6a66d2328a5bab3aad579fee30bddd2f
|
4
|
+
data.tar.gz: b872195ecf8bfaf2d381cf8eb07647c1317d71b7fd49a7044bcd8e8256f8b98a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bab3df4478ed3eda30c254a380c69d29c690abbc371e03fc923c353fb90e5c3259aef0839cde49402a9961d6da8035006356195df81a7b9cc43f9f5d626b5019
|
7
|
+
data.tar.gz: d772f43733c33204c0acaafb9d0d38c797cebb603efc4ae311990f8afac2eb15a4538ade187b5f4df8dc489cf6e7ddd54548a0ef021663ad735f619cc37d1006
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
nd-enum (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.5.0)
|
10
|
+
rake (13.0.6)
|
11
|
+
rspec (3.11.0)
|
12
|
+
rspec-core (~> 3.11.0)
|
13
|
+
rspec-expectations (~> 3.11.0)
|
14
|
+
rspec-mocks (~> 3.11.0)
|
15
|
+
rspec-core (3.11.0)
|
16
|
+
rspec-support (~> 3.11.0)
|
17
|
+
rspec-expectations (3.11.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.11.0)
|
20
|
+
rspec-mocks (3.11.1)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.11.0)
|
23
|
+
rspec-support (3.11.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
x86_64-darwin-21
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
nd-enum!
|
30
|
+
rake (~> 13.0)
|
31
|
+
rspec (~> 3.0)
|
32
|
+
|
33
|
+
BUNDLED WITH
|
34
|
+
2.3.12
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Romain Clavel
|
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,33 @@
|
|
1
|
+
# ND::Enum
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nd/enum`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add nd-enum
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
$ gem install nd-enum
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
TODO: Write usage instructions here
|
20
|
+
|
21
|
+
## Development
|
22
|
+
|
23
|
+
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.
|
24
|
+
|
25
|
+
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).
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nd-enum.
|
30
|
+
|
31
|
+
## License
|
32
|
+
|
33
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/nd/enum/base.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ND
|
4
|
+
module Base
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class << self
|
9
|
+
include Enumerable
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
def_delegators :all, :size, :length, :[], :empty?, :last, :index
|
13
|
+
|
14
|
+
def each(&block)
|
15
|
+
all.each(&block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_h
|
19
|
+
all.map { |value| [value.to_sym, value] }.to_h
|
20
|
+
end
|
21
|
+
|
22
|
+
def [](value)
|
23
|
+
value.is_a?(Integer) ? all[value] : to_h[value.to_sym]
|
24
|
+
end
|
25
|
+
|
26
|
+
# TODO: Warning if not defined
|
27
|
+
# `t(:foo)`
|
28
|
+
# fr:
|
29
|
+
# table_name:
|
30
|
+
# attribute:
|
31
|
+
# base:
|
32
|
+
# foo: Foo
|
33
|
+
#
|
34
|
+
# or
|
35
|
+
#
|
36
|
+
# `t(:foo, :scope_a)`
|
37
|
+
# fr:
|
38
|
+
# table_name:
|
39
|
+
# attribute:
|
40
|
+
# base:
|
41
|
+
# foo: Foo
|
42
|
+
# scope_a:
|
43
|
+
# foo: Foo A
|
44
|
+
# scope_b:
|
45
|
+
# foo: Foo B
|
46
|
+
def t(value, scope = :base)
|
47
|
+
I18n.t(value, scope: "#{model.table_name}.#{attribute}.#{scope}")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/nd/enum.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'enum/version'
|
4
|
+
|
5
|
+
module ND
|
6
|
+
module Enum
|
7
|
+
class << self
|
8
|
+
def nd_enum(db: false, i18n: {}, **configuration)
|
9
|
+
set_options(binding)
|
10
|
+
enum_module = define_module(@options)
|
11
|
+
|
12
|
+
define_db_enum(db, enum_module) if @options[:db]
|
13
|
+
|
14
|
+
const_set(@options[:attribute].to_s.camelize, enum_module)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def set_options(caller_binding)
|
20
|
+
@options = method(:nd_enum).parameters.each_with_object({}) do |(_, name), options|
|
21
|
+
options[name.to_sym] = caller_binding.local_variable_get(name)
|
22
|
+
end
|
23
|
+
@options[:attribute], @options[:values] = @options.delete(:configuration).to_a.first
|
24
|
+
@options[:model] = self
|
25
|
+
end
|
26
|
+
|
27
|
+
def define_module(options)
|
28
|
+
Module.new do
|
29
|
+
include ND::Enum::Base
|
30
|
+
|
31
|
+
# Public methods
|
32
|
+
|
33
|
+
define_singleton_method(:all) { options[:values] }
|
34
|
+
|
35
|
+
# Private methods
|
36
|
+
|
37
|
+
define_singleton_method(:options) { options }
|
38
|
+
options.each_key do |name|
|
39
|
+
define_singleton_method(name) { options[name] }
|
40
|
+
end
|
41
|
+
|
42
|
+
[:options, *options.keys].each do |method_name|
|
43
|
+
singleton_class.class_eval { private method_name.to_sym }
|
44
|
+
end
|
45
|
+
|
46
|
+
# Constants
|
47
|
+
|
48
|
+
options[:values].map do |value|
|
49
|
+
const_set(value.to_s.upcase, value.to_s)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def define_db_enum(options, enum_module)
|
55
|
+
options = options.is_a?(Hash) ? options : {}
|
56
|
+
enum(@attribute => enum_module.to_h, **options)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/nd-enum.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/nd/enum/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'nd-enum'
|
7
|
+
spec.version = ND::Enum::VERSION
|
8
|
+
spec.authors = ['Romain Clavel']
|
9
|
+
spec.email = ['romain@clavel.io']
|
10
|
+
|
11
|
+
spec.summary = 'Powerful enum for Rails models.'
|
12
|
+
spec.homepage = 'https://github.com/rclavel/nd-enum'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
spec.required_ruby_version = '>= 2.6.0'
|
15
|
+
|
16
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
17
|
+
spec.metadata['source_code_uri'] = 'https://github.com/rclavel/nd-enum'
|
18
|
+
spec.metadata['changelog_uri'] = 'https://github.com/rclavel/nd-enum/blob/master/CHANGELOG.md'
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(__dir__) do
|
23
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
24
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
25
|
+
end
|
26
|
+
end
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
|
31
|
+
# Uncomment to register a new dependency of your gem
|
32
|
+
# spec.add_dependency 'example-gem', '~> 1.0'
|
33
|
+
|
34
|
+
# For more information and examples about making a new gem, check out our
|
35
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
36
|
+
end
|
data/sig/nd/enum.rbs
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nd-enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Romain Clavel
|
@@ -16,7 +16,19 @@ email:
|
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
|
-
files:
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- CHANGELOG.md
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/nd/enum.rb
|
28
|
+
- lib/nd/enum/base.rb
|
29
|
+
- lib/nd/enum/version.rb
|
30
|
+
- nd-enum.gemspec
|
31
|
+
- sig/nd/enum.rbs
|
20
32
|
homepage: https://github.com/rclavel/nd-enum
|
21
33
|
licenses:
|
22
34
|
- MIT
|