progne_tapera 1.0 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d10a2c3b33b51e33b9a5b4c10c515c1906f44b93363618e1c40e8f8287f4ca4
4
- data.tar.gz: c55437dc658e85ab1926186d358c3a7939a44a176c334baed4f96be14a3b01a1
3
+ metadata.gz: 453bfbf458de730766d872c7c38bd1f27731adc35ed619afe8202246e37fa9cd
4
+ data.tar.gz: 6e51e656f3c4df6d82fa1c7a6dc28a603c72ed615f712a246a2c2d688d35bce0
5
5
  SHA512:
6
- metadata.gz: 5b723d945d8ef7480ceadab714a395bc745b4c94c2d08d6eab6937815e8c37fa0bf9251869329c01539a7bf9be0bc7e857106f49e86928b7d385ac83294cb3cc
7
- data.tar.gz: 0e7c5d705548d63ea6e8dec96f345c31c074b4099b37fd68296a3c598f76e931200fdf2cc280e934175501a94a925cc26c7c0ca21c35d5ceb1b09edd32a0a75b
6
+ metadata.gz: 9ce9d7440bbbf8d7adefd0e713f3f9098277d5e75dbc62cd2932f1e54c35f8d520cd5cef0297174b154611de4c9058fa936a15c629bde32743ac1dc1901dbe57
7
+ data.tar.gz: c27f37405a2e5703e0a6b9ed2783237ad4bd74daed09e01caa115a77ae1bf99f3c826b25a2e632789ca041c0ad3f9733795746cf8fe2fb71800f25df116a87d7
data/CHANGELOG.md CHANGED
@@ -9,32 +9,38 @@
9
9
 
10
10
  ## v0.1.1
11
11
 
12
- 1. Improved the Ruby Gem Specification to depend on [rails](https://github.com/rails/rails) v4.2
12
+ 1. Improved the Ruby Gem Specification to depend on [rails](https://github.com/rails/rails) v4.2.
13
13
 
14
14
  ## v0.2
15
15
 
16
- 1. Improved the Enum Config concern to support the customized enum i18n name
16
+ 1. Improved the Enum Config concern to support the customized enum i18n name.
17
17
 
18
18
  ## v0.3
19
19
 
20
- 1. Improved the Enum Config concern to support the overloaded enum i18n name
20
+ 1. Improved the Enum Config concern to support the overloaded enum i18n name.
21
21
 
22
22
  ## v0.4
23
23
 
24
- 1. Improved the Enum List concern to add the .``lookup`` method
24
+ 1. Improved the Enum List concern to add the .``lookup`` method.
25
25
 
26
26
  ## v0.5
27
27
 
28
- 1. Improved the Enum Config concern to be able to be extended by lambda
28
+ 1. Improved the Enum Config concern to be able to be extended by lambda.
29
29
 
30
30
  ## v0.5.1
31
31
 
32
- 1. Improved the Enum config concern for the Item Methods module
32
+ 1. Improved the Enum config concern for the Item Methods module.
33
33
 
34
34
  ## v0.5.2
35
35
 
36
- 1. Improved the Enum Config concern for the Item Methods module
36
+ 1. Improved the Enum Config concern for the Item Methods module.
37
37
 
38
38
  ## v1.0
39
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
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,149 +1,152 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- progne_tapera (0.5.2)
4
+ progne_tapera (2.0)
5
5
  rails (~> 6.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- actioncable (6.0.2.2)
11
- actionpack (= 6.0.2.2)
10
+ actioncable (6.1.4)
11
+ actionpack (= 6.1.4)
12
+ activesupport (= 6.1.4)
12
13
  nio4r (~> 2.0)
13
14
  websocket-driver (>= 0.6.1)
14
- actionmailbox (6.0.2.2)
15
- actionpack (= 6.0.2.2)
16
- activejob (= 6.0.2.2)
17
- activerecord (= 6.0.2.2)
18
- activestorage (= 6.0.2.2)
19
- activesupport (= 6.0.2.2)
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)
20
21
  mail (>= 2.7.1)
21
- actionmailer (6.0.2.2)
22
- actionpack (= 6.0.2.2)
23
- actionview (= 6.0.2.2)
24
- activejob (= 6.0.2.2)
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)
25
27
  mail (~> 2.5, >= 2.5.4)
26
28
  rails-dom-testing (~> 2.0)
27
- actionpack (6.0.2.2)
28
- actionview (= 6.0.2.2)
29
- activesupport (= 6.0.2.2)
30
- rack (~> 2.0, >= 2.0.8)
29
+ actionpack (6.1.4)
30
+ actionview (= 6.1.4)
31
+ activesupport (= 6.1.4)
32
+ rack (~> 2.0, >= 2.0.9)
31
33
  rack-test (>= 0.6.3)
32
34
  rails-dom-testing (~> 2.0)
33
35
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
34
- actiontext (6.0.2.2)
35
- actionpack (= 6.0.2.2)
36
- activerecord (= 6.0.2.2)
37
- activestorage (= 6.0.2.2)
38
- activesupport (= 6.0.2.2)
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)
39
41
  nokogiri (>= 1.8.5)
40
- actionview (6.0.2.2)
41
- activesupport (= 6.0.2.2)
42
+ actionview (6.1.4)
43
+ activesupport (= 6.1.4)
42
44
  builder (~> 3.1)
43
45
  erubi (~> 1.4)
44
46
  rails-dom-testing (~> 2.0)
45
47
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
46
- activejob (6.0.2.2)
47
- activesupport (= 6.0.2.2)
48
+ activejob (6.1.4)
49
+ activesupport (= 6.1.4)
48
50
  globalid (>= 0.3.6)
49
- activemodel (6.0.2.2)
50
- activesupport (= 6.0.2.2)
51
- activerecord (6.0.2.2)
52
- activemodel (= 6.0.2.2)
53
- activesupport (= 6.0.2.2)
54
- activestorage (6.0.2.2)
55
- actionpack (= 6.0.2.2)
56
- activejob (= 6.0.2.2)
57
- activerecord (= 6.0.2.2)
58
- marcel (~> 0.3.1)
59
- activesupport (6.0.2.2)
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)
60
64
  concurrent-ruby (~> 1.0, >= 1.0.2)
61
- i18n (>= 0.7, < 2)
62
- minitest (~> 5.1)
63
- tzinfo (~> 1.1)
64
- zeitwerk (~> 2.2)
65
+ i18n (>= 1.6, < 2)
66
+ minitest (>= 5.1)
67
+ tzinfo (~> 2.0)
68
+ zeitwerk (~> 2.3)
65
69
  builder (3.2.4)
66
- concurrent-ruby (1.1.6)
70
+ concurrent-ruby (1.1.9)
67
71
  crass (1.0.6)
68
- diff-lcs (1.3)
69
- erubi (1.9.0)
70
- globalid (0.4.2)
71
- activesupport (>= 4.2.0)
72
- i18n (1.8.2)
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)
73
77
  concurrent-ruby (~> 1.0)
74
- loofah (2.5.0)
78
+ loofah (2.11.0)
75
79
  crass (~> 1.0.2)
76
80
  nokogiri (>= 1.5.9)
77
81
  mail (2.7.1)
78
82
  mini_mime (>= 0.1.1)
79
- marcel (0.3.3)
80
- mimemagic (~> 0.3.2)
83
+ marcel (1.0.1)
81
84
  method_source (1.0.0)
82
- mimemagic (0.3.4)
83
- mini_mime (1.0.2)
84
- mini_portile2 (2.4.0)
85
- minitest (5.14.0)
86
- nio4r (2.5.2)
87
- nokogiri (1.10.9)
88
- mini_portile2 (~> 2.4.0)
89
- rack (2.2.2)
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)
90
94
  rack-test (1.1.0)
91
95
  rack (>= 1.0, < 3)
92
- rails (6.0.2.2)
93
- actioncable (= 6.0.2.2)
94
- actionmailbox (= 6.0.2.2)
95
- actionmailer (= 6.0.2.2)
96
- actionpack (= 6.0.2.2)
97
- actiontext (= 6.0.2.2)
98
- actionview (= 6.0.2.2)
99
- activejob (= 6.0.2.2)
100
- activemodel (= 6.0.2.2)
101
- activerecord (= 6.0.2.2)
102
- activestorage (= 6.0.2.2)
103
- activesupport (= 6.0.2.2)
104
- bundler (>= 1.3.0)
105
- railties (= 6.0.2.2)
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)
106
110
  sprockets-rails (>= 2.0.0)
107
111
  rails-dom-testing (2.0.3)
108
112
  activesupport (>= 4.2.0)
109
113
  nokogiri (>= 1.6)
110
114
  rails-html-sanitizer (1.3.0)
111
115
  loofah (~> 2.3)
112
- railties (6.0.2.2)
113
- actionpack (= 6.0.2.2)
114
- activesupport (= 6.0.2.2)
116
+ railties (6.1.4)
117
+ actionpack (= 6.1.4)
118
+ activesupport (= 6.1.4)
115
119
  method_source
116
- rake (>= 0.8.7)
117
- thor (>= 0.20.3, < 2.0)
118
- rake (13.0.1)
119
- rspec (3.9.0)
120
- rspec-core (~> 3.9.0)
121
- rspec-expectations (~> 3.9.0)
122
- rspec-mocks (~> 3.9.0)
123
- rspec-core (3.9.1)
124
- rspec-support (~> 3.9.1)
125
- rspec-expectations (3.9.1)
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)
126
130
  diff-lcs (>= 1.2.0, < 2.0)
127
- rspec-support (~> 3.9.0)
128
- rspec-mocks (3.9.1)
131
+ rspec-support (~> 3.10.0)
132
+ rspec-mocks (3.10.2)
129
133
  diff-lcs (>= 1.2.0, < 2.0)
130
- rspec-support (~> 3.9.0)
131
- rspec-support (3.9.2)
132
- sprockets (4.0.0)
134
+ rspec-support (~> 3.10.0)
135
+ rspec-support (3.10.2)
136
+ sprockets (4.0.2)
133
137
  concurrent-ruby (~> 1.0)
134
138
  rack (> 1, < 3)
135
- sprockets-rails (3.2.1)
139
+ sprockets-rails (3.2.2)
136
140
  actionpack (>= 4.0)
137
141
  activesupport (>= 4.0)
138
142
  sprockets (>= 3.0.0)
139
- thor (1.0.1)
140
- thread_safe (0.3.6)
141
- tzinfo (1.2.7)
142
- thread_safe (~> 0.1)
143
- websocket-driver (0.7.1)
143
+ thor (1.1.0)
144
+ tzinfo (2.0.4)
145
+ concurrent-ruby (~> 1.0)
146
+ websocket-driver (0.7.5)
144
147
  websocket-extensions (>= 0.1.0)
145
- websocket-extensions (0.1.4)
146
- zeitwerk (2.3.0)
148
+ websocket-extensions (0.1.5)
149
+ zeitwerk (2.4.2)
147
150
 
148
151
  PLATFORMS
149
152
  ruby
@@ -152,7 +155,7 @@ DEPENDENCIES
152
155
  bundler (~> 2.0)
153
156
  progne_tapera!
154
157
  rake (~> 13.0)
155
- rspec (~> 3.9)
158
+ rspec (~> 3.0)
156
159
 
157
160
  BUNDLED WITH
158
- 2.1.4
161
+ 2.2.13
data/ROADMAP.md CHANGED
@@ -9,36 +9,38 @@
9
9
 
10
10
  ## v0.1.1
11
11
 
12
- 1. Improve the Ruby Gem Specification to depend on [rails](https://github.com/rails/rails) v4.2
12
+ 1. Improve the Ruby Gem Specification to depend on [rails](https://github.com/rails/rails) v4.2.
13
13
 
14
14
  ## v0.2
15
15
 
16
- 1. Improve the Enum Config concern to support the customized enum i18n name
16
+ 1. Improve the Enum Config concern to support the customized enum i18n name.
17
17
 
18
18
  ## v0.3
19
19
 
20
- 1. Improve the Enum Config concern to support the overloaded enum i18n name
20
+ 1. Improve the Enum Config concern to support the overloaded enum i18n name.
21
21
 
22
22
  ## v0.4
23
23
 
24
- 1. Improve the Enum List concern to add the .``lookup`` method
24
+ 1. Improve the Enum List concern to add the .``lookup`` method.
25
25
 
26
26
  ## v0.5
27
27
 
28
- 1. Improve the Enum Code concern to be able to be extended by lambda
28
+ 1. Improve the Enum Code concern to be able to be extended by lambda.
29
29
 
30
30
  ## v0.5.1
31
31
 
32
- 1. Improve the Enum config concern for the Item Methods module
32
+ 1. Improve the Enum config concern for the Item Methods module.
33
33
 
34
34
  ## v0.5.2
35
35
 
36
- 1. Improve the Enum Config concern for the Item Methods module
36
+ 1. Improve the Enum Config concern for the Item Methods module.
37
37
 
38
38
  ## v1.0
39
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
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
41
 
42
- ## v1.1
42
+ ## v2.0
43
43
 
44
- 1. Improve the Enum Config concern to make the enum items be able to be updated or destroyable through the #enum method
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,5 +1,7 @@
1
1
  require 'progne_tapera/version'
2
2
 
3
+ require 'rails'
4
+
3
5
  require 'progne_tapera/enum_list'
4
6
  require 'progne_tapera/enum_item'
5
7
  require 'progne_tapera/enum_config'
@@ -1,31 +1,36 @@
1
1
  ##
2
2
  # Enum Code 是在模型层配置知识层枚举的关注点,提供 .code 宏方法。
3
3
 
4
- module ProgneTapera::EnumCode
4
+ module ProgneTapera
5
5
 
6
- extend ActiveSupport::Concern
6
+ module EnumCode
7
7
 
8
- included do |includer|
9
- end
8
+ extend ActiveSupport::Concern
10
9
 
11
- module ClassMethods
12
-
13
- ##
14
- # 为关注的类提供以下逻辑:
15
- # 1. 验证 ``field``_code 字段的值,是否在枚举类型的定义中。
16
- # 2. 定义 ``field`` 方法,用于获取枚举型的值。
17
- # 3. 定义 ``field``= 方法,用于为枚举型字段赋值。
18
- def code(field, enum)
19
- code_field_name = :"#{field}_code"
20
- validates code_field_name, inclusion: enum.all.map { |item| item.code }
21
- instance_eval do
22
- define_method field do
23
- enum.select { |item| item.code==send(code_field_name.to_sym) }.first
24
- end
25
- define_method "#{field}=" do |value|
26
- send "#{code_field_name}=".to_sym, value.code
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
27
31
  end
28
32
  end
33
+
29
34
  end
30
35
 
31
36
  end
@@ -1,69 +1,66 @@
1
1
  ##
2
2
  # Enum Config 是知识层枚举类型的配置关注点。提供 .enum 和 .overload_enum_i18n 方法。
3
3
 
4
- module ProgneTapera::EnumConfig
4
+ module ProgneTapera
5
5
 
6
- extend ActiveSupport::Concern
7
- include ProgneTapera::EnumList
6
+ module EnumConfig
8
7
 
9
- included do |includer|
10
- end
11
-
12
- module ClassMethods
8
+ extend ActiveSupport::Concern
9
+ include ProgneTapera::EnumList
13
10
 
14
- ##
15
- # 为枚举类型提供 .enum 方法。如:
16
- # enum :gender
17
- # # 或
18
- # enum :gender, :localized_gender
19
- # 第一个参数是枚举类型在配置文件中存储的枚举项的 key 。
20
- # 第二个参数是枚举类型在国际化配置文件中存储的枚举项文本的 key 。如果不提供第二个参数,则自动使用第一个参数进行查找。
21
- def enum(name = nil, localized_name = name)
22
-
23
- if block_given?
11
+ included do |includer|
12
+ end
24
13
 
25
- yield.each do |key, value|
26
- options = value.map { |k, v| [ k.to_sym, v ] }.to_h
27
- #options[:optional] = true if options[:optional].nil?
28
- code = options.delete :code
29
- safe_add_item ProgneTapera::EnumItem.new(code, key.to_s, options)
30
- end
14
+ #module ClassMethods
15
+ class_methods do
16
+
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
31
34
 
32
- else
35
+ else
33
36
 
34
- name = enum_name(name).to_s
35
- enumerations = Rails.configuration.enum[name]
36
- raise ArgumentError.new("The enum.#{name} was not configured in the config/enum.yml file.") if enumerations.blank?
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?
37
40
 
38
- enumerations.each do |key, value|
39
- options = value.map { |k, v| [ k.to_sym, v ] }.to_h
40
- #options[:optional] = false
41
- code = options.delete :code
42
- options[:localized_name] = I18n.t "enum.#{localized_name||name}.#{key}"
43
- item = ProgneTapera::EnumItem.new code, key, options
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
44
47
 
45
- item_method_module = "#{self.name}::ItemMethods".safe_constantize
46
- item.class_eval do
47
- include item_method_module if item_method_module.present?
48
+ safe_add_item item
48
49
  end
49
- #class << item
50
- # include item_method_module if item_method_module.present?
51
- #end
52
50
 
53
- safe_add_item item
54
51
  end
55
52
 
56
53
  end
57
54
 
58
- end
55
+ ##
56
+ # 为枚举类型提供 .overload_enum_i18n 方法。如:
57
+ # overload_enum_i18n :your_gender_i18n_key
58
+ def overload_enum_i18n(localized_name = nil)
59
59
 
60
- ##
61
- # 为枚举类型提供 .overload_enum_i18n 方法。如:
62
- # overload_enum_i18n :your_gender_i18n_key
63
- def overload_enum_i18n(localized_name = nil)
60
+ each do |enum_item|
61
+ enum_item.options[:localized_name] = I18n.t "enum.#{localized_name}.#{enum_item.name}"
62
+ end
64
63
 
65
- each do |enum_item|
66
- enum_item.options[:localized_name] = I18n.t "enum.#{localized_name}.#{enum_item.name}"
67
64
  end
68
65
 
69
66
  end
@@ -1,51 +1,53 @@
1
- require 'rails'
2
-
3
1
  ##
4
2
  # Enum Item 是枚举项。
5
3
  # enum_item = ProgneTapera::EnumItem.new '1', 'male', { localized_name: '男' }
6
4
 
7
- class ProgneTapera::EnumItem
5
+ module ProgneTapera
6
+
7
+ class EnumItem
8
8
 
9
- attr_reader :code, :name, :options
9
+ attr_reader :code, :name, :options
10
10
 
11
- ##
12
- # code = HA (value)
13
- # name = han
14
- # options = { localized_name: '汉' }
15
- # -> constant: HAN
16
- def initialize(code, name, options = {})
11
+ ##
12
+ # code = HA (value)
13
+ # name = han
14
+ # options = { localized_name: '汉' }
15
+ # -> constant: HAN
16
+ def initialize(code, name, options = {})
17
17
 
18
- raise ArgumentError.new('The code argument is required.') if code.blank?
19
- raise ArgumentError.new('The name argument is required.') if name.blank?
18
+ raise ArgumentError.new('The code argument is required.') if code.blank?
19
+ raise ArgumentError.new('The name argument is required.') if name.blank?
20
20
 
21
- @code = code
22
- @name = name
23
- @options = options
21
+ @code = code
22
+ @name = name
23
+ @options = options
24
24
 
25
- @options.each do |key, value|
26
- class_eval do
27
- define_method(key.to_sym) do
28
- @options[key] #value
25
+ @options.each do |key, value|
26
+ EnumItem.class_eval do
27
+ define_method(key.to_sym) do
28
+ @options[key] #value
29
+ end
29
30
  end
30
31
  end
32
+
31
33
  end
32
34
 
33
- end
35
+ ##
36
+ # 判断两个枚举项的 #code 字段是否相等。
37
+ def ==(that)
38
+ self.code==that.code
39
+ end
34
40
 
35
- ##
36
- # 判断两个枚举项的 #code 字段是否相等。
37
- def ==(that)
38
- self.code==that.code
39
- end
41
+ ##
42
+ # 根据 #code 字段的值,比较大小。
43
+ def <=>(that)
44
+ self.code<=>that.code
45
+ end
40
46
 
41
- ##
42
- # 根据 #code 字段的值,比较大小。
43
- def <=>(that)
44
- self.code<=>that.code
45
- end
47
+ def constant_name
48
+ name.to_s.upcase
49
+ end
46
50
 
47
- def constant_name
48
- name.to_s.upcase
49
51
  end
50
52
 
51
53
  end
@@ -1,101 +1,115 @@
1
1
  ##
2
2
  # Enum List 是知识层枚举列表的领域逻辑关注点。
3
3
 
4
- module ProgneTapera::EnumList
4
+ module ProgneTapera
5
5
 
6
- extend ActiveSupport::Concern
6
+ module EnumList
7
7
 
8
- included do |includer|
9
- end
10
-
11
- module ClassMethods
12
-
13
- include Enumerable
14
-
15
- # Define the Enum type
16
-
17
- ##
18
- # 向枚举类型提供枚举的配置名。如果不提供配置名则根据枚举类型的名称自动猜测。如:
19
- # eumu_name
20
- # # 或
21
- # enum_name :gender
22
- def enum_name(name = nil)
23
- return @enum_name if @enum_name.present?
24
- @enum_name = (name||self.name.demodulize.underscore).to_sym
25
- end
26
-
27
- # Define the Enum Items
8
+ extend ActiveSupport::Concern
28
9
 
29
- ##
30
- # 判断指定的枚举项是否已经定义。如:
31
- # item_defined? male
32
- def item_defined?(item)
33
- const_defined? item.constant_name
10
+ included do |includer|
34
11
  end
35
12
 
36
- def add_item(item)
37
- raise ArgumentError.new "The #{item.inspect} item should be an instance of ProgneTapera::EnumItem." unless item.is_a? ProgneTapera::EnumItem
38
- raise ArgumentError.new "The #{item.constant_name} enum item was defined already." if item_defined? item
39
-
40
- const_set item.constant_name, item
41
- end
42
-
43
- ##
44
- # 以安全的方式向一个枚举列表中添加一个枚举项。如果该枚举项已经在枚举列表中,则不添加。如:
45
- # Gender.safe_add_item male
46
- # Gender.safe_add_item female
47
- # Gender.safe_add_item male # male won't be added twice.
48
- def safe_add_item(item)
49
- const_set item.constant_name, item if item.is_a?(ProgneTapera::EnumItem)&&!item_defined?(item)
50
- end
51
-
52
- # Destroy or Update the Enum Items
53
- #def clear_optional_items
54
- #
55
- #end
56
-
57
- # Infrastructure for the Enumerable
58
- def enum_constants
59
- constants.select { |constant|
60
- value = const_get constant
61
- value.is_a? ProgneTapera::EnumItem
62
- }
63
- end
64
-
65
- ##
66
- # 列出所有的枚举型。如:
67
- # Gender.all
68
- def all
69
- enum_constants.map { |constant| const_get constant }
70
- end
71
-
72
- def selected
73
- block_given? ? yield(all) : all
74
- end
75
-
76
- # Enumerable
77
- def each(&block)
78
- all.each &block
79
- end
80
-
81
- # Lookup
82
- def lookup(code)
83
- select { |item| item.code==code }.first
84
- end
85
-
86
- # Form Option
87
- def form_options(&block)
88
- items = block_given? ? selected(&block) : selected
89
- items.map { |item| [ item.localized_name, item.code ] }.to_h
90
- end
91
-
92
- # ActiveRecord::Type::Value
93
- def deserialize(value)
94
- select { |item| item.code==value }
95
- end
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
96
112
 
97
- def serialize(value)
98
- value.respond_to?(:code) ? value.code : value
99
113
  end
100
114
 
101
115
  end
@@ -1,3 +1,3 @@
1
1
  module ProgneTapera
2
- VERSION = '1.0'
2
+ VERSION = '2.0'
3
3
  end
@@ -22,6 +22,6 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency 'bundler', '~> 2.0'
24
24
  spec.add_development_dependency 'rake', '~> 13.0'
25
- spec.add_development_dependency 'rspec', '~> 3.9'
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.add_item male unless subject.item_defined? male
27
- subject.add_item female unless subject.item_defined? female
28
- subject.add_item not_specified unless subject.item_defined? not_specified
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: progne_tapera
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.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: 2020-04-28 00:00:00.000000000 Z
11
+ date: 2021-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.9'
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.9'
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,8 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  requirements: []
116
- rubygems_version: 3.1.2
117
- signing_key:
116
+ rubygems_version: 3.2.3
117
+ signing_key:
118
118
  specification_version: 4
119
119
  summary: Rails-based configurable enumeration 基于 Rails 的可配置的枚举
120
120
  test_files: