enum_label 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/README.md +73 -0
- data/Rakefile +8 -0
- data/lib/enum_label/version.rb +5 -0
- data/lib/enum_label.rb +46 -0
- data/sig/enum_label.rbs +4 -0
- data/spec/enum_label_spec.rb +66 -0
- data/spec/spec_helper.rb +28 -0
- metadata +79 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5d800732afc90ddb02ef79efe26fcd49abf77845c3b2f4765f624096a46e399d
|
|
4
|
+
data.tar.gz: 47a0d6b03f26ba883b91074727f0afcb9e8d1dc3d110bfc474b9c481a18f7ecf
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7f753ebfa6239f5f121499ba1bfc8ac0e239c31a6b020e22a9258e1f2433efbd4535fd80aaaf14c85e007ca5ba022b6f71bd0bcb50f40faacf509e7d5dfbde96
|
|
7
|
+
data.tar.gz: 417b89a7d11ace9a3edcc3b1964253df2d30d6b425d31679c57d1616c4a78fb744f74b94d0b267c0ede72ae1c9368f8ff14a9637439781f1bb64c4acdd973ecd
|
data/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# EnumLabel
|
|
2
|
+
|
|
3
|
+
Rails の enum に人間が読めるラベルを簡単に付けられる gem です。
|
|
4
|
+
|
|
5
|
+
日本語はもちろん、どの言語のラベルでも使えます。
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
# Gemfile
|
|
11
|
+
gem "enum_label"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bundle install
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
class Article < ApplicationRecord
|
|
22
|
+
enum :status, { draft: 0, published: 1, archived: 2 }
|
|
23
|
+
enum_label :status, draft: "下書き", published: "公開済み", archived: "アーカイブ"
|
|
24
|
+
end
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Instance method
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
article = Article.new(status: :draft)
|
|
31
|
+
article.status_label #=> "下書き"
|
|
32
|
+
|
|
33
|
+
article.status = :published
|
|
34
|
+
article.status_label #=> "公開済み"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Class method
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
Article.status_labels
|
|
41
|
+
#=> { "draft" => "下書き", "published" => "公開済み", "archived" => "アーカイブ" }
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
セレクトボックスにそのまま使えます:
|
|
45
|
+
|
|
46
|
+
```erb
|
|
47
|
+
<%= f.select :status, Article.status_labels.invert %>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Multiple enums
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
class Article < ApplicationRecord
|
|
54
|
+
enum :status, { draft: 0, published: 1, archived: 2 }
|
|
55
|
+
enum_label :status, draft: "下書き", published: "公開済み", archived: "アーカイブ"
|
|
56
|
+
|
|
57
|
+
enum :visibility, { visible: 0, hidden: 1 }
|
|
58
|
+
enum_label :visibility, visible: "公開", hidden: "非公開"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
article = Article.new(status: :draft, visibility: :hidden)
|
|
62
|
+
article.status_label #=> "下書き"
|
|
63
|
+
article.visibility_label #=> "非公開"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Requirements
|
|
67
|
+
|
|
68
|
+
- Ruby >= 3.2
|
|
69
|
+
- Rails >= 7.0
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/enum_label.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "enum_label/version"
|
|
4
|
+
require "active_support/concern"
|
|
5
|
+
|
|
6
|
+
module EnumLabel
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
class Error < StandardError; end
|
|
10
|
+
|
|
11
|
+
class_methods do
|
|
12
|
+
# Define human-readable labels for an enum attribute.
|
|
13
|
+
#
|
|
14
|
+
# class Article < ApplicationRecord
|
|
15
|
+
# enum :status, { draft: 0, published: 1, archived: 2 }
|
|
16
|
+
# enum_label :status, draft: "下書き", published: "公開済み", archived: "アーカイブ"
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# article = Article.new(status: :draft)
|
|
20
|
+
# article.status_label #=> "下書き"
|
|
21
|
+
# Article.status_labels #=> { "draft" => "下書き", "published" => "公開済み", "archived" => "アーカイブ" }
|
|
22
|
+
#
|
|
23
|
+
def enum_label(attribute, labels = {})
|
|
24
|
+
labels_map = labels.transform_keys(&:to_s).freeze
|
|
25
|
+
attribute = attribute.to_sym
|
|
26
|
+
|
|
27
|
+
# Store labels on the class for introspection
|
|
28
|
+
singleton_class.module_eval do
|
|
29
|
+
define_method(:"#{attribute}_labels") do
|
|
30
|
+
labels_map
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Instance method: article.status_label
|
|
35
|
+
define_method(:"#{attribute}_label") do
|
|
36
|
+
value = send(attribute)
|
|
37
|
+
labels_map[value.to_s] if value
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Auto-include into ActiveRecord if available
|
|
44
|
+
ActiveSupport.on_load(:active_record) do
|
|
45
|
+
include EnumLabel
|
|
46
|
+
end
|
data/sig/enum_label.rbs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe EnumLabel do
|
|
6
|
+
it "has a version number" do
|
|
7
|
+
expect(EnumLabel::VERSION).not_to be_nil
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
describe "#status_label" do
|
|
11
|
+
it "returns the correct label" do
|
|
12
|
+
article = Article.new(status: :draft)
|
|
13
|
+
expect(article.status_label).to eq "下書き"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "returns the updated label after change" do
|
|
17
|
+
article = Article.new(status: :draft)
|
|
18
|
+
article.status = :published
|
|
19
|
+
expect(article.status_label).to eq "公開済み"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "returns the label for each enum value" do
|
|
23
|
+
expect(Article.new(status: :draft).status_label).to eq "下書き"
|
|
24
|
+
expect(Article.new(status: :published).status_label).to eq "公開済み"
|
|
25
|
+
expect(Article.new(status: :archived).status_label).to eq "アーカイブ"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "returns nil when attribute is nil" do
|
|
29
|
+
article = Article.new
|
|
30
|
+
article[:status] = nil
|
|
31
|
+
expect(article.status_label).to be_nil
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
describe ".status_labels" do
|
|
36
|
+
it "returns all labels as a hash" do
|
|
37
|
+
expected = { "draft" => "下書き", "published" => "公開済み", "archived" => "アーカイブ" }
|
|
38
|
+
expect(Article.status_labels).to eq expected
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "returns a frozen hash" do
|
|
42
|
+
expect(Article.status_labels).to be_frozen
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe "multiple enums" do
|
|
47
|
+
it "supports multiple enum_label on the same model" do
|
|
48
|
+
article = Article.new(status: :draft, visibility: :hidden)
|
|
49
|
+
expect(article.status_label).to eq "下書き"
|
|
50
|
+
expect(article.visibility_label).to eq "非公開"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "returns class-level labels for each enum" do
|
|
54
|
+
expect(Article.visibility_labels).to eq({ "visible" => "公開", "hidden" => "非公開" })
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
describe "persisted record" do
|
|
59
|
+
it "returns the label after reload" do
|
|
60
|
+
article = Article.create!(status: :published, visibility: :visible)
|
|
61
|
+
reloaded = Article.find(article.id)
|
|
62
|
+
expect(reloaded.status_label).to eq "公開済み"
|
|
63
|
+
expect(reloaded.visibility_label).to eq "公開"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_record"
|
|
4
|
+
require "enum_label"
|
|
5
|
+
|
|
6
|
+
# In-memory SQLite database for testing
|
|
7
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
|
8
|
+
|
|
9
|
+
ActiveRecord::Schema.define do
|
|
10
|
+
create_table :articles, force: true do |t|
|
|
11
|
+
t.integer :status, default: 0, null: false
|
|
12
|
+
t.integer :visibility, default: 0, null: false
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class Article < ActiveRecord::Base
|
|
17
|
+
enum :status, { draft: 0, published: 1, archived: 2 }
|
|
18
|
+
enum_label :status, draft: "下書き", published: "公開済み", archived: "アーカイブ"
|
|
19
|
+
|
|
20
|
+
enum :visibility, { visible: 0, hidden: 1 }
|
|
21
|
+
enum_label :visibility, visible: "公開", hidden: "非公開"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
RSpec.configure do |config|
|
|
25
|
+
config.expect_with :rspec do |expectations|
|
|
26
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
|
27
|
+
end
|
|
28
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: enum_label
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- aluto144
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: activerecord
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '7.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '7.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: activesupport
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '7.0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '7.0'
|
|
40
|
+
description: Easily add human-readable labels (Japanese, English, etc.) to your Rails
|
|
41
|
+
enum attributes with a simple DSL.
|
|
42
|
+
email:
|
|
43
|
+
- attwelve@icloud.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- README.md
|
|
49
|
+
- Rakefile
|
|
50
|
+
- lib/enum_label.rb
|
|
51
|
+
- lib/enum_label/version.rb
|
|
52
|
+
- sig/enum_label.rbs
|
|
53
|
+
- spec/enum_label_spec.rb
|
|
54
|
+
- spec/spec_helper.rb
|
|
55
|
+
homepage: https://github.com/aluto12/enum_label
|
|
56
|
+
licenses:
|
|
57
|
+
- MIT
|
|
58
|
+
metadata:
|
|
59
|
+
allowed_push_host: https://rubygems.org
|
|
60
|
+
homepage_uri: https://github.com/aluto12/enum_label
|
|
61
|
+
source_code_uri: https://github.com/aluto12/enum_label
|
|
62
|
+
rdoc_options: []
|
|
63
|
+
require_paths:
|
|
64
|
+
- lib
|
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: 3.2.0
|
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
requirements: []
|
|
76
|
+
rubygems_version: 3.6.9
|
|
77
|
+
specification_version: 4
|
|
78
|
+
summary: Human-readable labels for Rails enums
|
|
79
|
+
test_files: []
|