enum_ish 1.3.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +47 -0
- data/CHANGELOG.md +25 -0
- data/README.md +16 -0
- data/gemfiles/rails61.gemfile +5 -0
- data/lib/enum_ish.rb +9 -0
- data/lib/enum_ish/active_record_definer.rb +11 -2
- data/lib/enum_ish/definer.rb +21 -6
- data/lib/enum_ish/dictionary.rb +40 -30
- data/lib/enum_ish/version.rb +1 -1
- metadata +5 -4
- data/.travis.yml +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34d21d01c11cb32143d1ce656228165fc93059f820b60f2cc109668a61e70680
|
4
|
+
data.tar.gz: a26baa4da8d0ae6e5ad1dce8cdf169f24fbe79ec8ddc88acf5fb2180db817b6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc05a7517f723a1df0f521e031d2c171ed6a3ad0614974326c52464ce5ecf7f89b5e591f0da34a15fac70212ab44ab883f64b7dff45b67ba865c153c2ebd3dd9
|
7
|
+
data.tar.gz: 8cb363ef8d2f7988dee963d3baef608b076484afa46268f1d08a7b21fe5ef12c736d09cb05ba3e6efaa01ce8b6acbf85f0748aef6d39a407338b499aa94f22c4
|
@@ -0,0 +1,47 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
runs-on: ubuntu-18.04
|
8
|
+
strategy:
|
9
|
+
fail-fast: false
|
10
|
+
matrix:
|
11
|
+
ruby: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0]
|
12
|
+
gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61']
|
13
|
+
exclude:
|
14
|
+
- ruby: 2.3
|
15
|
+
gemfile: rails60
|
16
|
+
- ruby: 2.3
|
17
|
+
gemfile: rails61
|
18
|
+
- ruby: 2.4
|
19
|
+
gemfile: rails60
|
20
|
+
- ruby: 2.4
|
21
|
+
gemfile: rails61
|
22
|
+
- ruby: 3.0
|
23
|
+
gemfile: rails50
|
24
|
+
- ruby: 3.0
|
25
|
+
gemfile: rails51
|
26
|
+
- ruby: 3.0
|
27
|
+
gemfile: rails52
|
28
|
+
|
29
|
+
name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}
|
30
|
+
|
31
|
+
env:
|
32
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
|
33
|
+
|
34
|
+
steps:
|
35
|
+
- uses: actions/checkout@v2
|
36
|
+
- uses: ruby/setup-ruby@v1
|
37
|
+
with:
|
38
|
+
ruby-version: ${{ matrix.ruby }}
|
39
|
+
bundler-cache: true
|
40
|
+
- name: Prepare test
|
41
|
+
run: |
|
42
|
+
cd spec/dummy
|
43
|
+
BUNDLE_GEMFILE=../../${{ env.BUNDLE_GEMFILE }} RAILS_ENV=test bundle exec rake db:create db:migrate db:seed
|
44
|
+
cd ../..
|
45
|
+
- name: Run test
|
46
|
+
run: |
|
47
|
+
bundle exec rspec
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,30 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 1.4.1
|
4
|
+
|
5
|
+
* Avoid overwrite of superclass's `_enum_ish_enums`.
|
6
|
+
* Filter ancestors for dictionary lookup.
|
7
|
+
|
8
|
+
## 1.4.0
|
9
|
+
|
10
|
+
* Generate `not` scope for activerecord class.
|
11
|
+
* Add prefix option to define short predicate method.
|
12
|
+
* Search translation including ancestor classes.
|
13
|
+
* Support multiple arguments for scope.
|
14
|
+
* Fix predicate and validation bugs for field with `accessor: true`.
|
15
|
+
|
16
|
+
## 1.3.3
|
17
|
+
|
18
|
+
* Support rails 6.1.
|
19
|
+
|
20
|
+
## 1.3.2
|
21
|
+
|
22
|
+
* Fix deprecation warning for ruby 2.7.
|
23
|
+
|
24
|
+
## 1.3.1
|
25
|
+
|
26
|
+
* Fix default value for model included ActiveModel.
|
27
|
+
|
3
28
|
## 1.3.0
|
4
29
|
|
5
30
|
* Support array field.
|
data/README.md
CHANGED
@@ -121,6 +121,21 @@ user.status_enable? #=> true
|
|
121
121
|
user.status_disable? #=> false
|
122
122
|
```
|
123
123
|
|
124
|
+
Without prefix:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
class User
|
128
|
+
extend EnumIsh
|
129
|
+
attr_accessor :status
|
130
|
+
enum_ish :status, ['enable', 'disable'], predicate: { prefix: false }
|
131
|
+
end
|
132
|
+
|
133
|
+
user = User.new
|
134
|
+
user.status = 'enable'
|
135
|
+
user.enable? #=> true
|
136
|
+
user.disable? #=> false
|
137
|
+
```
|
138
|
+
|
124
139
|
### Accessor
|
125
140
|
|
126
141
|
Generate getter and setter for aliased symbols instead of raw values:
|
@@ -164,6 +179,7 @@ class User < ActiveRecord::Base
|
|
164
179
|
end
|
165
180
|
|
166
181
|
User.with_status(:enable) #=> SELECT "users".* FROM "users" WHERE "users"."status" = "enable"
|
182
|
+
User.with_status_not(:enable) #=> SELECT "users".* FROM "users" WHERE "users"."status" != 'enable'
|
167
183
|
```
|
168
184
|
|
169
185
|
#### Validation
|
data/lib/enum_ish.rb
CHANGED
@@ -22,6 +22,15 @@ module EnumIsh
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def self.extended(klass)
|
25
|
+
super
|
25
26
|
klass.class_attribute :_enum_ish_enums
|
27
|
+
klass.extend ClassMethods
|
28
|
+
end
|
29
|
+
|
30
|
+
module ClassMethods
|
31
|
+
def inherited(subclass)
|
32
|
+
super
|
33
|
+
subclass._enum_ish_enums = self._enum_ish_enums.dup
|
34
|
+
end
|
26
35
|
end
|
27
36
|
end
|
@@ -24,7 +24,13 @@ module EnumIsh
|
|
24
24
|
enum.mapping.fetch(value, value)
|
25
25
|
end
|
26
26
|
|
27
|
-
|
27
|
+
args =
|
28
|
+
if ActiveRecord.version > Gem::Version.new('6.1.0.a')
|
29
|
+
[enum.name]
|
30
|
+
else
|
31
|
+
[enum.name, :enum]
|
32
|
+
end
|
33
|
+
decorate_attribute_type(*args) do |subtype|
|
28
34
|
EnumIsh::ActiveRecordEnumType.new(enum.name, enum.mapping, subtype)
|
29
35
|
end
|
30
36
|
end
|
@@ -32,9 +38,12 @@ module EnumIsh
|
|
32
38
|
|
33
39
|
def define_scope(enum)
|
34
40
|
@klass.class_eval do
|
35
|
-
scope "#{Config.scope_prefix}#{enum.name}#{Config.scope_suffix}", ->(value) {
|
41
|
+
scope "#{Config.scope_prefix}#{enum.name}#{Config.scope_suffix}", ->(*value) {
|
36
42
|
where(enum.name => enum.mapping.fetch(value, value))
|
37
43
|
}
|
44
|
+
scope "#{Config.scope_prefix}#{enum.name}_not#{Config.scope_suffix}", ->(*value) {
|
45
|
+
where.not(enum.name => enum.mapping.fetch(value, value))
|
46
|
+
}
|
38
47
|
end
|
39
48
|
end
|
40
49
|
end
|
data/lib/enum_ish/definer.rb
CHANGED
@@ -32,13 +32,23 @@ module EnumIsh
|
|
32
32
|
|
33
33
|
def define_predicate(enum)
|
34
34
|
enum.mapping.each do |enum_key, enum_value|
|
35
|
+
method_name = if enum.setting[:predicate].is_a?(Hash) && enum.setting[:predicate][:prefix] == false
|
36
|
+
"#{enum_key}?"
|
37
|
+
else
|
38
|
+
"#{enum.name}_#{enum_key}?"
|
39
|
+
end
|
40
|
+
target_value = if enum.setting[:accessor]
|
41
|
+
enum_key
|
42
|
+
else
|
43
|
+
enum_value
|
44
|
+
end
|
35
45
|
@klass.class_eval do
|
36
|
-
define_method
|
46
|
+
define_method method_name.tr('.', '_') do
|
37
47
|
value = public_send(enum.name)
|
38
48
|
if value.is_a?(Array)
|
39
|
-
value == [
|
49
|
+
value == [target_value]
|
40
50
|
else
|
41
|
-
value ==
|
51
|
+
value == target_value
|
42
52
|
end
|
43
53
|
end
|
44
54
|
end
|
@@ -49,15 +59,15 @@ module EnumIsh
|
|
49
59
|
mod = Module.new
|
50
60
|
mod.module_eval do
|
51
61
|
define_method :initialize do |*args|
|
62
|
+
super(*args)
|
52
63
|
if respond_to?(enum.name) && public_send(enum.name).nil?
|
53
64
|
default = enum.setting[:default]
|
54
65
|
default = instance_exec(&default) if default.kind_of?(Proc)
|
55
66
|
public_send("#{enum.name}=", default)
|
56
67
|
end
|
57
|
-
super(*args)
|
58
68
|
end
|
59
69
|
end
|
60
|
-
@klass.
|
70
|
+
@klass.send(:include, mod)
|
61
71
|
end
|
62
72
|
|
63
73
|
def define_accessor(enum)
|
@@ -75,8 +85,13 @@ module EnumIsh
|
|
75
85
|
end
|
76
86
|
|
77
87
|
def define_validate(enum)
|
88
|
+
targets = if enum.setting[:accessor]
|
89
|
+
enum.mapping.keys
|
90
|
+
else
|
91
|
+
enum.mapping.values
|
92
|
+
end
|
78
93
|
@klass.class_eval do
|
79
|
-
validates enum.name, inclusion: { in:
|
94
|
+
validates enum.name, inclusion: { in: targets }, allow_nil: true
|
80
95
|
end
|
81
96
|
end
|
82
97
|
|
data/lib/enum_ish/dictionary.rb
CHANGED
@@ -2,7 +2,9 @@ module EnumIsh
|
|
2
2
|
class Dictionary
|
3
3
|
def initialize(klass, enum, options = {})
|
4
4
|
@klass = klass
|
5
|
-
@
|
5
|
+
@enum = enum
|
6
|
+
@options = options
|
7
|
+
@dict = load_dict
|
6
8
|
end
|
7
9
|
|
8
10
|
def translate_value(value)
|
@@ -11,7 +13,7 @@ module EnumIsh
|
|
11
13
|
else
|
12
14
|
@dict[value] || value
|
13
15
|
end
|
14
|
-
|
16
|
+
end
|
15
17
|
|
16
18
|
def translate_options
|
17
19
|
@dict.to_a.map { |value, label| [label, value] }
|
@@ -19,44 +21,52 @@ module EnumIsh
|
|
19
21
|
|
20
22
|
private
|
21
23
|
|
22
|
-
def
|
23
|
-
|
24
|
-
filter(dict, options)
|
25
|
-
end
|
24
|
+
def load_dict
|
25
|
+
i18n = load_from_i18n.transform_keys { |k| k.to_s.to_sym }
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
dict = {}
|
28
|
+
if @enum.setting[:accessor]
|
29
|
+
@enum.mapping.each { |k, v| dict[k] = i18n[k].to_s }
|
30
|
+
else
|
31
|
+
@enum.mapping.each { |k, v| dict[v] = i18n[k].to_s }
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
key = i18n_key(enum, options)
|
36
|
-
dict = I18n.t("enum_ish.#{@klass.name.underscore}.#{key}", i18n_options(enum, options))
|
37
|
-
dict.map { |k, v| [k.to_s.to_sym, v.to_s] }.to_h
|
34
|
+
filter(dict)
|
38
35
|
end
|
39
36
|
|
40
|
-
def
|
41
|
-
[enum.name, options[:format]].compact.join('/')
|
37
|
+
def load_from_i18n
|
38
|
+
key = [@enum.name, @options[:format]].compact.join('/')
|
39
|
+
options = (@options[:i18n_options] || {}).merge(default: nil)
|
40
|
+
dict = I18n.t(:"enum_ish.#{@klass.name.underscore}.#{key}", **options)
|
41
|
+
return dict if dict
|
42
|
+
|
43
|
+
i18n_ancestors.each do |ancestor|
|
44
|
+
dict = I18n.t(:"enum_ish.#{ancestor.name.underscore}.#{key}", **options)
|
45
|
+
return dict if dict
|
46
|
+
end
|
47
|
+
|
48
|
+
dict = I18n.t(:"enum_ish.defaults.#{key}", **options)
|
49
|
+
return dict if dict
|
50
|
+
|
51
|
+
@enum.mapping.invert
|
42
52
|
end
|
43
53
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
54
|
+
def i18n_ancestors
|
55
|
+
@klass.ancestors.drop(1)
|
56
|
+
.take_while { |a| a.name != 'ActiveRecord::Base' && a.name != 'Object' }
|
57
|
+
.select { |a| a.is_a?(Class) && !a.name.empty? }
|
48
58
|
end
|
49
59
|
|
50
|
-
def filter(
|
51
|
-
if options[:except]
|
52
|
-
except = Array(options[:except])
|
53
|
-
|
60
|
+
def filter(dict)
|
61
|
+
if @options[:except]
|
62
|
+
except = Array(@options[:except])
|
63
|
+
dict.reject! { |k, v| except.include?(k) }
|
54
64
|
end
|
55
|
-
if options[:only]
|
56
|
-
only = Array(options[:only])
|
57
|
-
|
65
|
+
if @options[:only]
|
66
|
+
only = Array(@options[:only])
|
67
|
+
dict.select! { |k, v| only.include?(k) }
|
58
68
|
end
|
59
|
-
|
69
|
+
dict
|
60
70
|
end
|
61
71
|
end
|
62
72
|
end
|
data/lib/enum_ish/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_ish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshikazu Kaneta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -101,9 +101,9 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
+
- ".github/workflows/ci.yml"
|
104
105
|
- ".gitignore"
|
105
106
|
- ".rspec"
|
106
|
-
- ".travis.yml"
|
107
107
|
- CHANGELOG.md
|
108
108
|
- Gemfile
|
109
109
|
- LICENSE
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- gemfiles/rails51.gemfile
|
115
115
|
- gemfiles/rails52.gemfile
|
116
116
|
- gemfiles/rails60.gemfile
|
117
|
+
- gemfiles/rails61.gemfile
|
117
118
|
- lib/enum_ish.rb
|
118
119
|
- lib/enum_ish/active_record_definer.rb
|
119
120
|
- lib/enum_ish/active_record_enum_type.rb
|
@@ -141,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
142
|
- !ruby/object:Gem::Version
|
142
143
|
version: '0'
|
143
144
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
145
|
+
rubygems_version: 3.1.2
|
145
146
|
signing_key:
|
146
147
|
specification_version: 4
|
147
148
|
summary: A ruby and rails extension to generate enum-like methods
|
data/.travis.yml
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
rvm:
|
3
|
-
- 2.3
|
4
|
-
- 2.4
|
5
|
-
- 2.5
|
6
|
-
- 2.6
|
7
|
-
gemfile:
|
8
|
-
- gemfiles/rails50.gemfile
|
9
|
-
- gemfiles/rails51.gemfile
|
10
|
-
- gemfiles/rails52.gemfile
|
11
|
-
- gemfiles/rails60.gemfile
|
12
|
-
matrix:
|
13
|
-
exclude:
|
14
|
-
- rvm: 2.3
|
15
|
-
gemfile: gemfiles/rails60.gemfile
|
16
|
-
- rvm: 2.4
|
17
|
-
gemfile: gemfiles/rails60.gemfile
|
18
|
-
before_script:
|
19
|
-
- cd spec/dummy
|
20
|
-
- bundle exec rake db:create db:migrate db:seed RAILS_ENV=test
|
21
|
-
- cd ../..
|
22
|
-
script:
|
23
|
-
- bundle exec rspec
|