progne_tapera 0.5 → 2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +29 -5
- data/Gemfile.lock +128 -100
- data/README.md +46 -23
- data/ROADMAP.md +29 -5
- data/lib/progne_tapera.rb +2 -0
- data/lib/progne_tapera/enum_code.rb +27 -14
- data/lib/progne_tapera/enum_config.rb +48 -26
- data/lib/progne_tapera/enum_item.rb +37 -26
- data/lib/progne_tapera/enum_list.rb +105 -70
- data/lib/progne_tapera/version.rb +1 -1
- data/progne_tapera.gemspec +4 -4
- data/spec/progne_tapera/enum_list_spec.rb +4 -3
- metadata +16 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 453bfbf458de730766d872c7c38bd1f27731adc35ed619afe8202246e37fa9cd
|
4
|
+
data.tar.gz: 6e51e656f3c4df6d82fa1c7a6dc28a603c72ed615f712a246a2c2d688d35bce0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ce9d7440bbbf8d7adefd0e713f3f9098277d5e75dbc62cd2932f1e54c35f8d520cd5cef0297174b154611de4c9058fa936a15c629bde32743ac1dc1901dbe57
|
7
|
+
data.tar.gz: c27f37405a2e5703e0a6b9ed2783237ad4bd74daed09e01caa115a77ae1bf99f3c826b25a2e632789ca041c0ad3f9733795746cf8fe2fb71800f25df116a87d7
|
data/CHANGELOG.md
CHANGED
@@ -1,22 +1,46 @@
|
|
1
1
|
# Progne Tapera Change Log 枚举库变更日志
|
2
2
|
|
3
3
|
## v0.1
|
4
|
+
|
4
5
|
1. Enum Item class
|
5
6
|
2. Enum List concern
|
6
7
|
3. Enum Config concern
|
7
8
|
4. Enum Code concern
|
8
9
|
|
9
10
|
## v0.1.1
|
10
|
-
|
11
|
+
|
12
|
+
1. Improved the Ruby Gem Specification to depend on [rails](https://github.com/rails/rails) v4.2.
|
11
13
|
|
12
14
|
## v0.2
|
13
|
-
|
15
|
+
|
16
|
+
1. Improved the Enum Config concern to support the customized enum i18n name.
|
14
17
|
|
15
18
|
## v0.3
|
16
|
-
|
19
|
+
|
20
|
+
1. Improved the Enum Config concern to support the overloaded enum i18n name.
|
17
21
|
|
18
22
|
## v0.4
|
19
|
-
|
23
|
+
|
24
|
+
1. Improved the Enum List concern to add the .``lookup`` method.
|
20
25
|
|
21
26
|
## v0.5
|
22
|
-
|
27
|
+
|
28
|
+
1. Improved the Enum Config concern to be able to be extended by lambda.
|
29
|
+
|
30
|
+
## v0.5.1
|
31
|
+
|
32
|
+
1. Improved the Enum config concern for the Item Methods module.
|
33
|
+
|
34
|
+
## v0.5.2
|
35
|
+
|
36
|
+
1. Improved the Enum Config concern for the Item Methods module.
|
37
|
+
|
38
|
+
## v1.0
|
39
|
+
|
40
|
+
1. Improved the Ruby Gem Specification to depend on [rails](https://github.com/rails/rails) v6.0, bundler v2.0, rake v13.0, & rspec v3.9.
|
41
|
+
|
42
|
+
## v2.0
|
43
|
+
|
44
|
+
1. Improved the compatibility for Ruby 3.0.
|
45
|
+
2. Improved the Enum List concern for the Item Methods module.
|
46
|
+
3. Improved the Enum Config concern to remove support for the Item Methods module.
|
data/Gemfile.lock
CHANGED
@@ -1,133 +1,161 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
progne_tapera (0
|
5
|
-
rails (
|
4
|
+
progne_tapera (2.0)
|
5
|
+
rails (~> 6.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
actioncable (
|
11
|
-
actionpack (=
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
activejob (=
|
10
|
+
actioncable (6.1.4)
|
11
|
+
actionpack (= 6.1.4)
|
12
|
+
activesupport (= 6.1.4)
|
13
|
+
nio4r (~> 2.0)
|
14
|
+
websocket-driver (>= 0.6.1)
|
15
|
+
actionmailbox (6.1.4)
|
16
|
+
actionpack (= 6.1.4)
|
17
|
+
activejob (= 6.1.4)
|
18
|
+
activerecord (= 6.1.4)
|
19
|
+
activestorage (= 6.1.4)
|
20
|
+
activesupport (= 6.1.4)
|
21
|
+
mail (>= 2.7.1)
|
22
|
+
actionmailer (6.1.4)
|
23
|
+
actionpack (= 6.1.4)
|
24
|
+
actionview (= 6.1.4)
|
25
|
+
activejob (= 6.1.4)
|
26
|
+
activesupport (= 6.1.4)
|
18
27
|
mail (~> 2.5, >= 2.5.4)
|
19
28
|
rails-dom-testing (~> 2.0)
|
20
|
-
actionpack (
|
21
|
-
actionview (=
|
22
|
-
activesupport (=
|
23
|
-
rack (~> 2.0)
|
24
|
-
rack-test (
|
29
|
+
actionpack (6.1.4)
|
30
|
+
actionview (= 6.1.4)
|
31
|
+
activesupport (= 6.1.4)
|
32
|
+
rack (~> 2.0, >= 2.0.9)
|
33
|
+
rack-test (>= 0.6.3)
|
25
34
|
rails-dom-testing (~> 2.0)
|
26
|
-
rails-html-sanitizer (~> 1.0, >= 1.0
|
27
|
-
|
28
|
-
|
35
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
36
|
+
actiontext (6.1.4)
|
37
|
+
actionpack (= 6.1.4)
|
38
|
+
activerecord (= 6.1.4)
|
39
|
+
activestorage (= 6.1.4)
|
40
|
+
activesupport (= 6.1.4)
|
41
|
+
nokogiri (>= 1.8.5)
|
42
|
+
actionview (6.1.4)
|
43
|
+
activesupport (= 6.1.4)
|
29
44
|
builder (~> 3.1)
|
30
|
-
|
45
|
+
erubi (~> 1.4)
|
31
46
|
rails-dom-testing (~> 2.0)
|
32
|
-
rails-html-sanitizer (~> 1.
|
33
|
-
activejob (
|
34
|
-
activesupport (=
|
47
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
48
|
+
activejob (6.1.4)
|
49
|
+
activesupport (= 6.1.4)
|
35
50
|
globalid (>= 0.3.6)
|
36
|
-
activemodel (
|
37
|
-
activesupport (=
|
38
|
-
activerecord (
|
39
|
-
activemodel (=
|
40
|
-
activesupport (=
|
41
|
-
|
42
|
-
|
51
|
+
activemodel (6.1.4)
|
52
|
+
activesupport (= 6.1.4)
|
53
|
+
activerecord (6.1.4)
|
54
|
+
activemodel (= 6.1.4)
|
55
|
+
activesupport (= 6.1.4)
|
56
|
+
activestorage (6.1.4)
|
57
|
+
actionpack (= 6.1.4)
|
58
|
+
activejob (= 6.1.4)
|
59
|
+
activerecord (= 6.1.4)
|
60
|
+
activesupport (= 6.1.4)
|
61
|
+
marcel (~> 1.0.0)
|
62
|
+
mini_mime (>= 1.1.0)
|
63
|
+
activesupport (6.1.4)
|
43
64
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
44
|
-
i18n (
|
45
|
-
minitest (
|
46
|
-
tzinfo (~>
|
47
|
-
|
48
|
-
builder (3.2.
|
49
|
-
concurrent-ruby (1.
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
65
|
+
i18n (>= 1.6, < 2)
|
66
|
+
minitest (>= 5.1)
|
67
|
+
tzinfo (~> 2.0)
|
68
|
+
zeitwerk (~> 2.3)
|
69
|
+
builder (3.2.4)
|
70
|
+
concurrent-ruby (1.1.9)
|
71
|
+
crass (1.0.6)
|
72
|
+
diff-lcs (1.4.4)
|
73
|
+
erubi (1.10.0)
|
74
|
+
globalid (0.5.2)
|
75
|
+
activesupport (>= 5.0)
|
76
|
+
i18n (1.8.10)
|
77
|
+
concurrent-ruby (~> 1.0)
|
78
|
+
loofah (2.11.0)
|
79
|
+
crass (~> 1.0.2)
|
56
80
|
nokogiri (>= 1.5.9)
|
57
|
-
mail (2.
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
rack
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
81
|
+
mail (2.7.1)
|
82
|
+
mini_mime (>= 0.1.1)
|
83
|
+
marcel (1.0.1)
|
84
|
+
method_source (1.0.0)
|
85
|
+
mini_mime (1.1.0)
|
86
|
+
mini_portile2 (2.6.1)
|
87
|
+
minitest (5.14.4)
|
88
|
+
nio4r (2.5.8)
|
89
|
+
nokogiri (1.12.2)
|
90
|
+
mini_portile2 (~> 2.6.1)
|
91
|
+
racc (~> 1.4)
|
92
|
+
racc (1.5.2)
|
93
|
+
rack (2.2.3)
|
94
|
+
rack-test (1.1.0)
|
95
|
+
rack (>= 1.0, < 3)
|
96
|
+
rails (6.1.4)
|
97
|
+
actioncable (= 6.1.4)
|
98
|
+
actionmailbox (= 6.1.4)
|
99
|
+
actionmailer (= 6.1.4)
|
100
|
+
actionpack (= 6.1.4)
|
101
|
+
actiontext (= 6.1.4)
|
102
|
+
actionview (= 6.1.4)
|
103
|
+
activejob (= 6.1.4)
|
104
|
+
activemodel (= 6.1.4)
|
105
|
+
activerecord (= 6.1.4)
|
106
|
+
activestorage (= 6.1.4)
|
107
|
+
activesupport (= 6.1.4)
|
108
|
+
bundler (>= 1.15.0)
|
109
|
+
railties (= 6.1.4)
|
82
110
|
sprockets-rails (>= 2.0.0)
|
83
|
-
rails-dom-testing (2.0.
|
84
|
-
activesupport (>= 4.2.0
|
85
|
-
nokogiri (
|
86
|
-
rails-html-sanitizer (1.0
|
87
|
-
loofah (~> 2.
|
88
|
-
railties (
|
89
|
-
actionpack (=
|
90
|
-
activesupport (=
|
111
|
+
rails-dom-testing (2.0.3)
|
112
|
+
activesupport (>= 4.2.0)
|
113
|
+
nokogiri (>= 1.6)
|
114
|
+
rails-html-sanitizer (1.3.0)
|
115
|
+
loofah (~> 2.3)
|
116
|
+
railties (6.1.4)
|
117
|
+
actionpack (= 6.1.4)
|
118
|
+
activesupport (= 6.1.4)
|
91
119
|
method_source
|
92
|
-
rake (>= 0.
|
93
|
-
thor (
|
94
|
-
rake (
|
95
|
-
rspec (3.
|
96
|
-
rspec-core (~> 3.
|
97
|
-
rspec-expectations (~> 3.
|
98
|
-
rspec-mocks (~> 3.
|
99
|
-
rspec-core (3.
|
100
|
-
rspec-support (~> 3.
|
101
|
-
rspec-expectations (3.
|
120
|
+
rake (>= 0.13)
|
121
|
+
thor (~> 1.0)
|
122
|
+
rake (13.0.6)
|
123
|
+
rspec (3.10.0)
|
124
|
+
rspec-core (~> 3.10.0)
|
125
|
+
rspec-expectations (~> 3.10.0)
|
126
|
+
rspec-mocks (~> 3.10.0)
|
127
|
+
rspec-core (3.10.1)
|
128
|
+
rspec-support (~> 3.10.0)
|
129
|
+
rspec-expectations (3.10.1)
|
102
130
|
diff-lcs (>= 1.2.0, < 2.0)
|
103
|
-
rspec-support (~> 3.
|
104
|
-
rspec-mocks (3.
|
131
|
+
rspec-support (~> 3.10.0)
|
132
|
+
rspec-mocks (3.10.2)
|
105
133
|
diff-lcs (>= 1.2.0, < 2.0)
|
106
|
-
rspec-support (~> 3.
|
107
|
-
rspec-support (3.
|
108
|
-
sprockets (
|
134
|
+
rspec-support (~> 3.10.0)
|
135
|
+
rspec-support (3.10.2)
|
136
|
+
sprockets (4.0.2)
|
109
137
|
concurrent-ruby (~> 1.0)
|
110
138
|
rack (> 1, < 3)
|
111
|
-
sprockets-rails (3.2.
|
139
|
+
sprockets-rails (3.2.2)
|
112
140
|
actionpack (>= 4.0)
|
113
141
|
activesupport (>= 4.0)
|
114
142
|
sprockets (>= 3.0.0)
|
115
|
-
thor (
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
websocket-driver (0.6.4)
|
143
|
+
thor (1.1.0)
|
144
|
+
tzinfo (2.0.4)
|
145
|
+
concurrent-ruby (~> 1.0)
|
146
|
+
websocket-driver (0.7.5)
|
120
147
|
websocket-extensions (>= 0.1.0)
|
121
|
-
websocket-extensions (0.1.
|
148
|
+
websocket-extensions (0.1.5)
|
149
|
+
zeitwerk (2.4.2)
|
122
150
|
|
123
151
|
PLATFORMS
|
124
152
|
ruby
|
125
153
|
|
126
154
|
DEPENDENCIES
|
127
|
-
bundler (~>
|
155
|
+
bundler (~> 2.0)
|
128
156
|
progne_tapera!
|
129
|
-
rake (~>
|
130
|
-
rspec (~> 3.
|
157
|
+
rake (~> 13.0)
|
158
|
+
rspec (~> 3.0)
|
131
159
|
|
132
160
|
BUNDLED WITH
|
133
|
-
|
161
|
+
2.2.13
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Progne Tapera
|
2
2
|
|
3
|
+
[![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](http://www.rubydoc.info/gems/progne_tapera/frames)
|
3
4
|
[![License](https://img.shields.io/badge/license-MIT-green.svg)](http://opensource.org/licenses/MIT)
|
5
|
+
|
4
6
|
[![Gem Version](https://badge.fury.io/rb/progne_tapera.svg)](https://badge.fury.io/rb/progne_tapera)
|
5
7
|
[![Dependency Status](https://gemnasium.com/badges/github.com/topbitdu/progne_tapera.svg)](https://gemnasium.com/github.com/topbitdu/progne_tapera)
|
6
8
|
|
@@ -97,21 +99,28 @@ app/types/ethnicity.rb
|
|
97
99
|
```ruby
|
98
100
|
class Ethnicity < ActiveRecord::Type::Value
|
99
101
|
|
100
|
-
|
102
|
+
# The Item Methods module must be defined before the ``enum :china_ethnicity`` code block, so it could be called there.
|
103
|
+
module ItemMethods
|
101
104
|
|
102
|
-
|
103
|
-
# 用 :china_ethnicity 指定的 i18n 资源。
|
105
|
+
extend ActiveSupport::Concern
|
104
106
|
|
105
|
-
|
107
|
+
included do |includer|
|
108
|
+
|
109
|
+
def major?
|
110
|
+
'HA'==code
|
111
|
+
end
|
112
|
+
# Ethnicity::HAN.major? returns true
|
113
|
+
# Ethnicity::MONGEL.major? returns false
|
106
114
|
|
107
|
-
def major?
|
108
|
-
'HA'==code
|
109
115
|
end
|
110
|
-
# Ethnicity::HAN.major? returns true
|
111
|
-
# Ethnicity::MONGEL.major? returns false
|
112
116
|
|
113
117
|
end
|
114
118
|
|
119
|
+
include ProgneTapera::EnumConfig
|
120
|
+
|
121
|
+
enum :china_ethnicity
|
122
|
+
# 用 :china_ethnicity 指定的 i18n 资源。
|
123
|
+
|
115
124
|
end
|
116
125
|
```
|
117
126
|
|
@@ -169,19 +178,31 @@ include ProgneTapera::EnumCode
|
|
169
178
|
|
170
179
|
### Enum List concern
|
171
180
|
|
172
|
-
The Enum List concern do the following tasks for the includer automatically:
|
173
|
-
1. Include the Enumerable module
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
181
|
+
The Enum List concern do the following tasks for the includer automatically:
|
182
|
+
1. Include the Enumerable module
|
183
|
+
|
184
|
+
2. Define the .enum_name method as: ``enum_name(name = nil)``
|
185
|
+
|
186
|
+
3. Define the .item_defined? method as: ``item_defined?(item)``
|
187
|
+
|
188
|
+
4. Define the .add_item method as: ``add_item(item)``
|
189
|
+
|
190
|
+
5. Define the .safe_add_item method as: ``safe_add_item(item)``
|
191
|
+
|
192
|
+
6. Define the .enum_constants method as: ``enum_constants``
|
193
|
+
|
194
|
+
7. Define the .all method as: ``all``
|
195
|
+
|
196
|
+
8. Define the .selected method as: ``selected(&block)``
|
197
|
+
|
198
|
+
9. Define the .each method as: ``each(&block)``
|
199
|
+
|
182
200
|
10. Define the .lookup method as: ``lookup(code)``
|
183
|
-
|
184
|
-
|
201
|
+
|
202
|
+
11. Define the .form_options method as: ``form_options(&block)``
|
203
|
+
|
204
|
+
12. Define the .deserialize method as: ``deserialize(value)``
|
205
|
+
|
185
206
|
13. Define the .serialize method as: ``serialize(value)``
|
186
207
|
|
187
208
|
```ruby
|
@@ -194,9 +215,11 @@ Gender.form_options # { '男' => '1', '女' => '2', '未指定' => '9' }
|
|
194
215
|
|
195
216
|
### Enum Config concern
|
196
217
|
|
197
|
-
The Enum Config concern do the following tasks for the includer automatically:
|
198
|
-
1. Include the Enum List concern
|
218
|
+
The Enum Config concern do the following tasks for the includer automatically:
|
219
|
+
1. Include the Enum List concern
|
220
|
+
|
199
221
|
2. Define the .enum method as: ``enum(name = nil, localized_name = name, &block)``
|
222
|
+
|
200
223
|
3. Define the .overload_enum_i18n method as: ``overload_enum_i18n(localized_name = nil)``
|
201
224
|
|
202
225
|
config/locales/enum.zh-CN.yml
|
@@ -239,7 +262,7 @@ Ethnicity::YI.localized_name # '彝'
|
|
239
262
|
|
240
263
|
### Enum Code concern
|
241
264
|
|
242
|
-
The Enum Code concern do the following tasks for the includer automatically:
|
265
|
+
The Enum Code concern do the following tasks for the includer automatically:
|
243
266
|
1. Define the .code method as: ``code(field, enum)``
|
244
267
|
|
245
268
|
|
data/ROADMAP.md
CHANGED
@@ -1,22 +1,46 @@
|
|
1
1
|
# Progne Tapera Road Map 枚举库线路图
|
2
2
|
|
3
3
|
## v0.1
|
4
|
+
|
4
5
|
1. Enum Item class
|
5
6
|
2. Enum List concern
|
6
7
|
3. Enum Config concern
|
7
8
|
4. Enum Code concern
|
8
9
|
|
9
10
|
## v0.1.1
|
10
|
-
|
11
|
+
|
12
|
+
1. Improve the Ruby Gem Specification to depend on [rails](https://github.com/rails/rails) v4.2.
|
11
13
|
|
12
14
|
## v0.2
|
13
|
-
|
15
|
+
|
16
|
+
1. Improve the Enum Config concern to support the customized enum i18n name.
|
14
17
|
|
15
18
|
## v0.3
|
16
|
-
|
19
|
+
|
20
|
+
1. Improve the Enum Config concern to support the overloaded enum i18n name.
|
17
21
|
|
18
22
|
## v0.4
|
19
|
-
|
23
|
+
|
24
|
+
1. Improve the Enum List concern to add the .``lookup`` method.
|
20
25
|
|
21
26
|
## v0.5
|
22
|
-
|
27
|
+
|
28
|
+
1. Improve the Enum Code concern to be able to be extended by lambda.
|
29
|
+
|
30
|
+
## v0.5.1
|
31
|
+
|
32
|
+
1. Improve the Enum config concern for the Item Methods module.
|
33
|
+
|
34
|
+
## v0.5.2
|
35
|
+
|
36
|
+
1. Improve the Enum Config concern for the Item Methods module.
|
37
|
+
|
38
|
+
## v1.0
|
39
|
+
|
40
|
+
1. Improve the Ruby Gem Specification to depend on [rails](https://github.com/rails/rails) v6.0, bundler v2.0, rake v13.0, & rspec v3.9.
|
41
|
+
|
42
|
+
## v2.0
|
43
|
+
|
44
|
+
1. Improve the compatibility for Ruby 3.0.
|
45
|
+
2. Improve the Enum List concern for the Item Methods module.
|
46
|
+
3. Improve the Enum Config concern to remove support for the Item Methods module.
|
data/lib/progne_tapera.rb
CHANGED
@@ -1,23 +1,36 @@
|
|
1
|
-
|
1
|
+
##
|
2
|
+
# Enum Code 是在模型层配置知识层枚举的关注点,提供 .code 宏方法。
|
2
3
|
|
3
|
-
|
4
|
+
module ProgneTapera
|
4
5
|
|
5
|
-
|
6
|
-
end
|
6
|
+
module EnumCode
|
7
7
|
|
8
|
-
|
8
|
+
extend ActiveSupport::Concern
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
10
|
+
included do |includer|
|
11
|
+
end
|
12
|
+
|
13
|
+
#module ClassMethods
|
14
|
+
class_methods do
|
15
|
+
|
16
|
+
##
|
17
|
+
# 为关注的类提供以下逻辑:
|
18
|
+
# 1. 验证 ``field``_code 字段的值,是否在枚举类型的定义中。
|
19
|
+
# 2. 定义 ``field`` 方法,用于获取枚举型的值。
|
20
|
+
# 3. 定义 ``field``= 方法,用于为枚举型字段赋值。
|
21
|
+
def code(field, enum)
|
22
|
+
code_field_name = :"#{field}_code"
|
23
|
+
validates code_field_name, inclusion: enum.all.map { |item| item.code }
|
24
|
+
instance_eval do
|
25
|
+
define_method field do
|
26
|
+
enum.select { |item| item.code==send(code_field_name.to_sym) }.first
|
27
|
+
end
|
28
|
+
define_method "#{field}=" do |value|
|
29
|
+
send "#{code_field_name}=".to_sym, value.code
|
30
|
+
end
|
19
31
|
end
|
20
32
|
end
|
33
|
+
|
21
34
|
end
|
22
35
|
|
23
36
|
end
|
@@ -1,44 +1,66 @@
|
|
1
|
-
|
1
|
+
##
|
2
|
+
# Enum Config 是知识层枚举类型的配置关注点。提供 .enum 和 .overload_enum_i18n 方法。
|
2
3
|
|
3
|
-
|
4
|
-
include ProgneTapera::EnumList
|
4
|
+
module ProgneTapera
|
5
5
|
|
6
|
-
|
7
|
-
end
|
6
|
+
module EnumConfig
|
8
7
|
|
9
|
-
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
include ProgneTapera::EnumList
|
10
10
|
|
11
|
-
|
11
|
+
included do |includer|
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
+
#module ClassMethods
|
15
|
+
class_methods do
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
##
|
18
|
+
# 为枚举类型提供 .enum 方法。如:
|
19
|
+
# enum :gender
|
20
|
+
# # 或
|
21
|
+
# enum :gender, :localized_gender
|
22
|
+
# 第一个参数是枚举类型在配置文件中存储的枚举项的 key 。
|
23
|
+
# 第二个参数是枚举类型在国际化配置文件中存储的枚举项文本的 key 。如果不提供第二个参数,则自动使用第一个参数进行查找。
|
24
|
+
def enum(name = nil, localized_name = name)
|
25
|
+
|
26
|
+
if block_given?
|
27
|
+
|
28
|
+
yield.each do |key, value|
|
29
|
+
options = value.map { |k, v| [ k.to_sym, v ] }.to_h
|
30
|
+
#options[:optional] = true if options[:optional].nil?
|
31
|
+
code = options.delete :code
|
32
|
+
safe_add_item ProgneTapera::EnumItem.new(code, key.to_s, options)
|
33
|
+
end
|
20
34
|
|
21
|
-
|
35
|
+
else
|
22
36
|
|
23
|
-
|
24
|
-
|
25
|
-
|
37
|
+
name = enum_name(name).to_s
|
38
|
+
enumerations = Rails.configuration.enum[name]
|
39
|
+
raise ArgumentError.new("The enum.#{name} was not configured in the config/enum.yml file.") if enumerations.blank?
|
40
|
+
|
41
|
+
enumerations.each do |key, value|
|
42
|
+
options = value.map { |k, v| [ k.to_sym, v ] }.to_h
|
43
|
+
#options[:optional] = false
|
44
|
+
code = options.delete :code
|
45
|
+
options[:localized_name] = I18n.t "enum.#{localized_name||name}.#{key}"
|
46
|
+
item = ProgneTapera::EnumItem.new code, key, options
|
47
|
+
|
48
|
+
safe_add_item item
|
49
|
+
end
|
26
50
|
|
27
|
-
enumerations.each do |key, value|
|
28
|
-
options = value.map { |k, v| [ k.to_sym, v ] }.to_h
|
29
|
-
code = options.delete :code
|
30
|
-
options[:localized_name] = I18n.t "enum.#{localized_name||name}.#{key}"
|
31
|
-
safe_add_item ProgneTapera::EnumItem.new(code, key, options)
|
32
51
|
end
|
33
52
|
|
34
53
|
end
|
35
54
|
|
36
|
-
|
55
|
+
##
|
56
|
+
# 为枚举类型提供 .overload_enum_i18n 方法。如:
|
57
|
+
# overload_enum_i18n :your_gender_i18n_key
|
58
|
+
def overload_enum_i18n(localized_name = nil)
|
37
59
|
|
38
|
-
|
60
|
+
each do |enum_item|
|
61
|
+
enum_item.options[:localized_name] = I18n.t "enum.#{localized_name}.#{enum_item.name}"
|
62
|
+
end
|
39
63
|
|
40
|
-
each do |enum_item|
|
41
|
-
enum_item.options[:localized_name] = I18n.t "enum.#{localized_name}.#{enum_item.name}"
|
42
64
|
end
|
43
65
|
|
44
66
|
end
|
@@ -1,42 +1,53 @@
|
|
1
|
-
|
1
|
+
##
|
2
|
+
# Enum Item 是枚举项。
|
3
|
+
# enum_item = ProgneTapera::EnumItem.new '1', 'male', { localized_name: '男' }
|
2
4
|
|
3
|
-
|
5
|
+
module ProgneTapera
|
4
6
|
|
5
|
-
|
7
|
+
class EnumItem
|
6
8
|
|
7
|
-
|
8
|
-
# name = han
|
9
|
-
# options = { localized_name: '汉' }
|
10
|
-
# -> constant: HAN
|
11
|
-
def initialize(code, name, options = {})
|
9
|
+
attr_reader :code, :name, :options
|
12
10
|
|
13
|
-
|
14
|
-
|
11
|
+
##
|
12
|
+
# code = HA (value)
|
13
|
+
# name = han
|
14
|
+
# options = { localized_name: '汉' }
|
15
|
+
# -> constant: HAN
|
16
|
+
def initialize(code, name, options = {})
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
@options = options
|
18
|
+
raise ArgumentError.new('The code argument is required.') if code.blank?
|
19
|
+
raise ArgumentError.new('The name argument is required.') if name.blank?
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
@code = code
|
22
|
+
@name = name
|
23
|
+
@options = options
|
24
|
+
|
25
|
+
@options.each do |key, value|
|
26
|
+
EnumItem.class_eval do
|
27
|
+
define_method(key.to_sym) do
|
28
|
+
@options[key] #value
|
29
|
+
end
|
24
30
|
end
|
25
31
|
end
|
32
|
+
|
26
33
|
end
|
27
34
|
|
28
|
-
|
35
|
+
##
|
36
|
+
# 判断两个枚举项的 #code 字段是否相等。
|
37
|
+
def ==(that)
|
38
|
+
self.code==that.code
|
39
|
+
end
|
29
40
|
|
30
|
-
|
31
|
-
|
32
|
-
|
41
|
+
##
|
42
|
+
# 根据 #code 字段的值,比较大小。
|
43
|
+
def <=>(that)
|
44
|
+
self.code<=>that.code
|
45
|
+
end
|
33
46
|
|
34
|
-
|
35
|
-
|
36
|
-
|
47
|
+
def constant_name
|
48
|
+
name.to_s.upcase
|
49
|
+
end
|
37
50
|
|
38
|
-
def constant_name
|
39
|
-
name.to_s.upcase
|
40
51
|
end
|
41
52
|
|
42
53
|
end
|
@@ -1,80 +1,115 @@
|
|
1
|
-
|
1
|
+
##
|
2
|
+
# Enum List 是知识层枚举列表的领域逻辑关注点。
|
2
3
|
|
3
|
-
module ProgneTapera
|
4
|
+
module ProgneTapera
|
4
5
|
|
5
|
-
|
6
|
+
module EnumList
|
6
7
|
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
|
12
|
-
include Enumerable
|
13
|
-
|
14
|
-
# Define the Enum type
|
15
|
-
def enum_name(name = nil)
|
16
|
-
return @enum_name if @enum_name.present?
|
17
|
-
@enum_name = (name||self.name.demodulize.underscore).to_sym
|
18
|
-
end
|
19
|
-
|
20
|
-
# Define the Enum Items
|
21
|
-
def item_defined?(item)
|
22
|
-
const_defined? item.constant_name
|
23
|
-
end
|
24
|
-
|
25
|
-
def add_item(item)
|
26
|
-
raise ArgumentError.new "The #{item.inspect} item should be an instance of ProgneTapera::EnumItem." unless item.is_a? ProgneTapera::EnumItem
|
27
|
-
raise ArgumentError.new "The #{item.constant_name} enum item was defined already." if item_defined? item
|
28
|
-
|
29
|
-
item_methods_module = "#{self.name}::ItemMethods".safe_constantize
|
30
|
-
item.class.include item_methods_module if item_methods_module.present?
|
31
|
-
|
32
|
-
const_set item.constant_name, item
|
33
|
-
end
|
34
|
-
|
35
|
-
def safe_add_item(item)
|
36
|
-
const_set item.constant_name, item if item.is_a?(ProgneTapera::EnumItem)&&!item_defined?(item)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Infrastructure for the Enumerable
|
40
|
-
def enum_constants
|
41
|
-
constants.select { |constant|
|
42
|
-
value = const_get constant
|
43
|
-
value.is_a? ProgneTapera::EnumItem
|
44
|
-
}
|
45
|
-
end
|
46
|
-
|
47
|
-
def all
|
48
|
-
enum_constants.map { |constant| const_get constant }
|
49
|
-
end
|
50
|
-
|
51
|
-
def selected
|
52
|
-
block_given? ? yield(all) : all
|
53
|
-
end
|
54
|
-
|
55
|
-
# Enumerable
|
56
|
-
def each(&block)
|
57
|
-
all.each &block
|
58
|
-
end
|
8
|
+
extend ActiveSupport::Concern
|
59
9
|
|
60
|
-
|
61
|
-
def lookup(code)
|
62
|
-
select { |item| item.code==code }.first
|
10
|
+
included do |includer|
|
63
11
|
end
|
64
12
|
|
65
|
-
#
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
13
|
+
# module ClassMethods
|
14
|
+
class_methods do
|
15
|
+
|
16
|
+
include Enumerable
|
17
|
+
|
18
|
+
# Define the Enum type
|
19
|
+
|
20
|
+
##
|
21
|
+
# 向枚举类型提供枚举的配置名。如果不提供配置名则根据枚举类型的名称自动猜测。如:
|
22
|
+
# eumu_name
|
23
|
+
# # 或
|
24
|
+
# enum_name :gender
|
25
|
+
def enum_name(name = nil)
|
26
|
+
return @enum_name if @enum_name.present?
|
27
|
+
@enum_name = (name||self.name.demodulize.underscore).to_sym
|
28
|
+
end
|
29
|
+
|
30
|
+
# Define the Enum Items
|
31
|
+
|
32
|
+
##
|
33
|
+
# 判断指定的枚举项是否已经定义。如:
|
34
|
+
# item_defined? male
|
35
|
+
def item_defined?(item)
|
36
|
+
const_defined? item.constant_name
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_item(item)
|
40
|
+
raise ArgumentError.new "The #{item.inspect} item should be an instance of ProgneTapera::EnumItem." unless item.is_a? ProgneTapera::EnumItem
|
41
|
+
raise ArgumentError.new "The #{item.constant_name} enum item was defined already." if item_defined? item
|
42
|
+
|
43
|
+
const_set item.constant_name, item
|
44
|
+
after_add_item item
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# 以安全的方式向一个枚举列表中添加一个枚举项。如果该枚举项已经在枚举列表中,则不添加。如:
|
49
|
+
# Gender.safe_add_item male
|
50
|
+
# Gender.safe_add_item female
|
51
|
+
# Gender.safe_add_item male # male won't be added twice.
|
52
|
+
def safe_add_item(item)
|
53
|
+
const_set item.constant_name, item if item.is_a?(ProgneTapera::EnumItem)&&!item_defined?(item)
|
54
|
+
after_add_item item
|
55
|
+
end
|
56
|
+
|
57
|
+
def after_add_item(item)
|
58
|
+
item_method_module = "#{self.name}::ItemMethods".safe_constantize
|
59
|
+
item.instance_eval do
|
60
|
+
item.extend item_method_module
|
61
|
+
end if item_method_module.present?
|
62
|
+
end
|
63
|
+
|
64
|
+
# Destroy or Update the Enum Items
|
65
|
+
#def clear_optional_items
|
66
|
+
#
|
67
|
+
#end
|
68
|
+
|
69
|
+
# Infrastructure for the Enumerable
|
70
|
+
def enum_constants
|
71
|
+
constants.select { |constant|
|
72
|
+
value = const_get constant
|
73
|
+
value.is_a? ProgneTapera::EnumItem
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
##
|
78
|
+
# 列出所有的枚举型。如:
|
79
|
+
# Gender.all
|
80
|
+
def all
|
81
|
+
enum_constants.map { |constant| const_get constant }
|
82
|
+
end
|
83
|
+
|
84
|
+
def selected
|
85
|
+
block_given? ? yield(all) : all
|
86
|
+
end
|
87
|
+
|
88
|
+
# Enumerable
|
89
|
+
def each(&block)
|
90
|
+
all.each &block
|
91
|
+
end
|
92
|
+
|
93
|
+
# Lookup
|
94
|
+
def lookup(code)
|
95
|
+
select { |item| item.code==code }.first
|
96
|
+
end
|
97
|
+
|
98
|
+
# Form Option
|
99
|
+
def form_options(&block)
|
100
|
+
items = block_given? ? selected(&block) : selected
|
101
|
+
items.map { |item| [ item.localized_name, item.code ] }.to_h
|
102
|
+
end
|
103
|
+
|
104
|
+
# ActiveRecord::Type::Value
|
105
|
+
def deserialize(value)
|
106
|
+
select { |item| item.code==value }
|
107
|
+
end
|
108
|
+
|
109
|
+
def serialize(value)
|
110
|
+
value.respond_to?(:code) ? value.code : value
|
111
|
+
end
|
75
112
|
|
76
|
-
def serialize(value)
|
77
|
-
value.respond_to?(:code) ? value.code : value
|
78
113
|
end
|
79
114
|
|
80
115
|
end
|
data/progne_tapera.gemspec
CHANGED
@@ -18,10 +18,10 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = [ 'lib' ]
|
20
20
|
|
21
|
-
spec.add_dependency 'rails', '
|
21
|
+
spec.add_dependency 'rails', '~> 6.0'
|
22
22
|
|
23
|
-
spec.add_development_dependency 'bundler', '~>
|
24
|
-
spec.add_development_dependency 'rake', '~>
|
25
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
23
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
24
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
26
26
|
|
27
27
|
end
|
@@ -5,6 +5,7 @@ describe ProgneTapera::EnumList do
|
|
5
5
|
let :subject do
|
6
6
|
class GenderEnum
|
7
7
|
include ProgneTapera::EnumList
|
8
|
+
#include ProgneTapera::EnumConfig
|
8
9
|
module ItemMethods
|
9
10
|
def hello(name)
|
10
11
|
case code
|
@@ -23,9 +24,9 @@ describe ProgneTapera::EnumList do
|
|
23
24
|
let :not_specified do ProgneTapera::EnumItem.new '9', :not_specified, localized_name: '未指定' end
|
24
25
|
|
25
26
|
before :each do
|
26
|
-
subject.
|
27
|
-
subject.
|
28
|
-
subject.
|
27
|
+
subject.safe_add_item male
|
28
|
+
subject.safe_add_item female
|
29
|
+
subject.safe_add_item not_specified
|
29
30
|
end
|
30
31
|
|
31
32
|
after :each do
|
metadata
CHANGED
@@ -1,71 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: progne_tapera
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0
|
4
|
+
version: '2.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Topbit Du
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '2.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '2.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '13.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '13.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '3.
|
61
|
+
version: '3.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '3.
|
68
|
+
version: '3.0'
|
69
69
|
description: Progne Tapera is a Rails-based configurable enumeration implementation.
|
70
70
|
Progne Tapera is the Brown-chested Martin in Latin. Progne Tapera 是基于 Rails 的可配置的枚举实现。Progne
|
71
71
|
Tapera 是棕胸崖燕的拉丁学名。
|
@@ -98,7 +98,7 @@ homepage: https://github.com/topbitdu/progne_tapera
|
|
98
98
|
licenses:
|
99
99
|
- MIT
|
100
100
|
metadata: {}
|
101
|
-
post_install_message:
|
101
|
+
post_install_message:
|
102
102
|
rdoc_options: []
|
103
103
|
require_paths:
|
104
104
|
- lib
|
@@ -113,9 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
- !ruby/object:Gem::Version
|
114
114
|
version: '0'
|
115
115
|
requirements: []
|
116
|
-
|
117
|
-
|
118
|
-
signing_key:
|
116
|
+
rubygems_version: 3.2.3
|
117
|
+
signing_key:
|
119
118
|
specification_version: 4
|
120
119
|
summary: Rails-based configurable enumeration 基于 Rails 的可配置的枚举
|
121
120
|
test_files:
|