progne_tapera 1.0 → 3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d10a2c3b33b51e33b9a5b4c10c515c1906f44b93363618e1c40e8f8287f4ca4
4
- data.tar.gz: c55437dc658e85ab1926186d358c3a7939a44a176c334baed4f96be14a3b01a1
3
+ metadata.gz: f8a03b7710e44b14b9406327a27d5ef29579272a6a0c6930ad01dbf11764b571
4
+ data.tar.gz: 8569b0191c09cc271d59cdba974cefc6e7e466ca5f58b8d11461e42b9dd98a80
5
5
  SHA512:
6
- metadata.gz: 5b723d945d8ef7480ceadab714a395bc745b4c94c2d08d6eab6937815e8c37fa0bf9251869329c01539a7bf9be0bc7e857106f49e86928b7d385ac83294cb3cc
7
- data.tar.gz: 0e7c5d705548d63ea6e8dec96f345c31c074b4099b37fd68296a3c598f76e931200fdf2cc280e934175501a94a925cc26c7c0ca21c35d5ceb1b09edd32a0a75b
6
+ metadata.gz: 4e948bf7a6e7cbc16d8170c36faabd19baae825007741a58f3c26822be85ed2c3c9fa181b18e6d3240d2d7a2651e8daaea92bfcf7eb5006cebb7f8ea10ac4b24
7
+ data.tar.gz: 243fc013194d3b19b4480b322d44252afb7dd79adad9567ac470bebebcf5ac73a97bf17c0edf32326c135c4ace6d80ebfe6f63a8aecf7c14639cc18584e878a0
data/CHANGELOG.md CHANGED
@@ -9,32 +9,42 @@
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](https://bundler.io/) v2.0, [Rake](http://github.com/ruby/rake) v13.0, & [RSpec](https://github.com/rspec/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.
47
+
48
+ ## v3.0
49
+
50
+ 1. Improved the compatibility for Rails [Rails](https://github.com/rails/rails) v7.x.
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](https://bundler.io/) v2.0, [Rake](http://github.com/ruby/rake) v13.0, & [RSpec](https://github.com/rspec/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.
@@ -1,31 +1,35 @@
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
+ class_methods do
14
+
15
+ ##
16
+ # 为关注的类提供以下逻辑:
17
+ # 1. 验证 ``field``_code 字段的值,是否在枚举类型的定义中。
18
+ # 2. 定义 ``field`` 方法,用于获取枚举型的值。
19
+ # 3. 定义 ``field``= 方法,用于为枚举型字段赋值。
20
+ def code(field, enum)
21
+ code_field_name = :"#{field}_code"
22
+ validates code_field_name, inclusion: enum.all.map { |item| item.code }
23
+ instance_eval do
24
+ define_method field do
25
+ enum.select { |item| item.code==send(code_field_name.to_sym) }.first
26
+ end
27
+ define_method "#{field}=" do |value|
28
+ send "#{code_field_name}=".to_sym, value.code
29
+ end
27
30
  end
28
31
  end
32
+
29
33
  end
30
34
 
31
35
  end
@@ -1,69 +1,65 @@
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
8
+ extend ActiveSupport::Concern
9
+ include ProgneTapera::EnumList
11
10
 
12
- module ClassMethods
11
+ included do |includer|
12
+ end
13
13
 
14
- ##
15
- # 为枚举类型提供 .enum 方法。如:
16
- # enum :gender
17
- # # 或
18
- # enum :gender, :localized_gender
19
- # 第一个参数是枚举类型在配置文件中存储的枚举项的 key 。
20
- # 第二个参数是枚举类型在国际化配置文件中存储的枚举项文本的 key 。如果不提供第二个参数,则自动使用第一个参数进行查找。
21
- def enum(name = nil, localized_name = name)
14
+ class_methods do
22
15
 
23
- if block_given?
16
+ ##
17
+ # 为枚举类型提供 .enum 方法。如:
18
+ # enum :gender
19
+ # # 或
20
+ # enum :gender, :localized_gender
21
+ # 第一个参数是枚举类型在配置文件中存储的枚举项的 key 。
22
+ # 第二个参数是枚举类型在国际化配置文件中存储的枚举项文本的 key 。如果不提供第二个参数,则自动使用第一个参数进行查找。
23
+ def enum(name = nil, localized_name = name)
24
24
 
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
25
+ if block_given?
31
26
 
32
- else
27
+ yield.each do |key, value|
28
+ options = value.map { |k, v| [ k.to_sym, v ] }.to_h
29
+ #options[:optional] = true if options[:optional].nil?
30
+ code = options.delete :code
31
+ safe_add_item ProgneTapera::EnumItem.new(code, key.to_s, options)
32
+ end
33
33
 
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?
34
+ else
37
35
 
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
36
+ name = enum_name(name).to_s
37
+ enumerations = Rails.configuration.enum[name]
38
+ raise ArgumentError.new("The enum.#{name} was not configured in the config/enum.yml file.") if enumerations.blank?
44
39
 
45
- item_method_module = "#{self.name}::ItemMethods".safe_constantize
46
- item.class_eval do
47
- include item_method_module if item_method_module.present?
40
+ enumerations.each do |key, value|
41
+ options = value.map { |k, v| [ k.to_sym, v ] }.to_h
42
+ #options[:optional] = false
43
+ code = options.delete :code
44
+ options[:localized_name] = I18n.t "enum.#{localized_name||name}.#{key}"
45
+ item = ProgneTapera::EnumItem.new code, key, options
46
+
47
+ safe_add_item item
48
48
  end
49
- #class << item
50
- # include item_method_module if item_method_module.present?
51
- #end
52
49
 
53
- safe_add_item item
54
50
  end
55
51
 
56
52
  end
57
53
 
58
- end
54
+ ##
55
+ # 为枚举类型提供 .overload_enum_i18n 方法。如:
56
+ # overload_enum_i18n :your_gender_i18n_key
57
+ def overload_enum_i18n(localized_name = nil)
59
58
 
60
- ##
61
- # 为枚举类型提供 .overload_enum_i18n 方法。如:
62
- # overload_enum_i18n :your_gender_i18n_key
63
- def overload_enum_i18n(localized_name = nil)
59
+ each do |enum_item|
60
+ enum_item.options[:localized_name] = I18n.t "enum.#{localized_name}.#{enum_item.name}"
61
+ end
64
62
 
65
- each do |enum_item|
66
- enum_item.options[:localized_name] = I18n.t "enum.#{localized_name}.#{enum_item.name}"
67
63
  end
68
64
 
69
65
  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,114 @@
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
+ class_methods do
14
+
15
+ include Enumerable
16
+
17
+ # Define the Enum type
18
+
19
+ ##
20
+ # 向枚举类型提供枚举的配置名。如果不提供配置名则根据枚举类型的名称自动猜测。如:
21
+ # eumu_name
22
+ # #
23
+ # enum_name :gender
24
+ def enum_name(name = nil)
25
+ return @enum_name if @enum_name.present?
26
+ @enum_name = (name||self.name.demodulize.underscore).to_sym
27
+ end
28
+
29
+ # Define the Enum Items
30
+
31
+ ##
32
+ # 判断指定的枚举项是否已经定义。如:
33
+ # item_defined? male
34
+ def item_defined?(item)
35
+ const_defined? item.constant_name
36
+ end
37
+
38
+ def add_item(item)
39
+ raise ArgumentError.new "The #{item.inspect} item should be an instance of ProgneTapera::EnumItem." unless item.is_a? ProgneTapera::EnumItem
40
+ raise ArgumentError.new "The #{item.constant_name} enum item was defined already." if item_defined? item
41
+
42
+ const_set item.constant_name, item
43
+ after_add_item item
44
+ end
45
+
46
+ ##
47
+ # 以安全的方式向一个枚举列表中添加一个枚举项。如果该枚举项已经在枚举列表中,则不添加。如:
48
+ # Gender.safe_add_item male
49
+ # Gender.safe_add_item female
50
+ # Gender.safe_add_item male # male won't be added twice.
51
+ def safe_add_item(item)
52
+ const_set item.constant_name, item if item.is_a?(ProgneTapera::EnumItem)&&!item_defined?(item)
53
+ after_add_item item
54
+ end
55
+
56
+ def after_add_item(item)
57
+ item_method_module = "#{self.name}::ItemMethods".safe_constantize
58
+ item.instance_eval do
59
+ item.extend item_method_module
60
+ end if item_method_module.present?
61
+ end
62
+
63
+ # Destroy or Update the Enum Items
64
+ #def clear_optional_items
65
+ #
66
+ #end
67
+
68
+ # Infrastructure for the Enumerable
69
+ def enum_constants
70
+ constants.select { |constant|
71
+ value = const_get constant
72
+ value.is_a? ProgneTapera::EnumItem
73
+ }
74
+ end
75
+
76
+ ##
77
+ # 列出所有的枚举型。如:
78
+ # Gender.all
79
+ def all
80
+ enum_constants.map { |constant| const_get constant }
81
+ end
82
+
83
+ def selected
84
+ block_given? ? yield(all) : all
85
+ end
86
+
87
+ # Enumerable
88
+ def each(&block)
89
+ all.each &block
90
+ end
91
+
92
+ # Lookup
93
+ def lookup(code)
94
+ select { |item| item.code==code }.first
95
+ end
96
+
97
+ # Form Option
98
+ def form_options(&block)
99
+ items = block_given? ? selected(&block) : selected
100
+ items.map { |item| [ item.localized_name, item.code ] }.to_h
101
+ end
102
+
103
+ # ActiveRecord::Type::Value
104
+ def deserialize(value)
105
+ select { |item| item.code==value }
106
+ end
107
+
108
+ def serialize(value)
109
+ value.respond_to?(:code) ? value.code : value
110
+ end
96
111
 
97
- def serialize(value)
98
- value.respond_to?(:code) ? value.code : value
99
112
  end
100
113
 
101
114
  end
@@ -1,3 +1,3 @@
1
1
  module ProgneTapera
2
- VERSION = '1.0'
2
+ VERSION = '3.0'
3
3
  end
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'
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = [ 'topbit.du@gmail.com' ]
11
11
  spec.summary = 'Rails-based configurable enumeration 基于 Rails 的可配置的枚举'
12
12
  spec.description = 'Progne Tapera is a Rails-based configurable enumeration implementation. Progne Tapera is the Brown-chested Martin in Latin. Progne Tapera 是基于 Rails 的可配置的枚举实现。Progne Tapera 是棕胸崖燕的拉丁学名。'
13
- spec.homepage = 'https://github.com/topbitdu/progne_tapera'
13
+ spec.homepage = 'https://gitee.com/Unidom/progne-tapera'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -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', '~> 6.0'
21
+ spec.add_dependency 'rails', '~> 7.0'
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: '3.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: 2024-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '6.0'
19
+ version: '7.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: '6.0'
26
+ version: '7.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -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 是棕胸崖燕的拉丁学名。
@@ -94,11 +94,11 @@ files:
94
94
  - spec/progne_tapera/enum_item_spec.rb
95
95
  - spec/progne_tapera/enum_list_spec.rb
96
96
  - spec/spec_helper.rb
97
- homepage: https://github.com/topbitdu/progne_tapera
97
+ homepage: https://gitee.com/Unidom/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.5.6
117
+ signing_key:
118
118
  specification_version: 4
119
119
  summary: Rails-based configurable enumeration 基于 Rails 的可配置的枚举
120
120
  test_files: