enum_ish 1.3.3 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +16 -0
- data/lib/enum_ish/active_record_definer.rb +4 -1
- data/lib/enum_ish/definer.rb +19 -4
- data/lib/enum_ish/dictionary.rb +7 -2
- data/lib/enum_ish/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53a6b89fe82d91b0ae54a440f3ba3a4908af751b3b22064f3ddd085257f975d9
|
4
|
+
data.tar.gz: 033b3df71e3286482f86ad0cce38084cc7c745cf13c5c06ad3cec146c3a577ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f55c27359c26b39c6500357594e318da8027198b45073bfd772d9ae9047f295d53e2c23ffabf54118cb308c399b8dce546e4db0a23e6564534a7a28c9f18bdfc
|
7
|
+
data.tar.gz: ca8144b132543120f9fb810398df2252ad8e1051c76e32242fef1d6029ab9a458b988902271c779580c6673fe1f6ac2cada24dd4b8ce7edf499669991721d21a
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 1.4.0
|
4
|
+
|
5
|
+
* Generate `not` scope for activerecord class.
|
6
|
+
* Add prefix option to define short predicate method.
|
7
|
+
* Search translation including ancestor classes.
|
8
|
+
* Support multiple arguments for scope.
|
9
|
+
* Fix predicate and validation bugs for field with `accessor: true`.
|
10
|
+
|
3
11
|
## 1.3.3
|
4
12
|
|
5
13
|
* Support rails 6.1.
|
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
|
@@ -38,9 +38,12 @@ module EnumIsh
|
|
38
38
|
|
39
39
|
def define_scope(enum)
|
40
40
|
@klass.class_eval do
|
41
|
-
scope "#{Config.scope_prefix}#{enum.name}#{Config.scope_suffix}", ->(value) {
|
41
|
+
scope "#{Config.scope_prefix}#{enum.name}#{Config.scope_suffix}", ->(*value) {
|
42
42
|
where(enum.name => enum.mapping.fetch(value, value))
|
43
43
|
}
|
44
|
+
scope "#{Config.scope_prefix}#{enum.name}_not#{Config.scope_suffix}", ->(*value) {
|
45
|
+
where.not(enum.name => enum.mapping.fetch(value, value))
|
46
|
+
}
|
44
47
|
end
|
45
48
|
end
|
46
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
|
@@ -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
@@ -33,7 +33,7 @@ module EnumIsh
|
|
33
33
|
|
34
34
|
def load_dict(enum, options)
|
35
35
|
key = i18n_key(enum, options)
|
36
|
-
dict = I18n.t("enum_ish.#{@klass.name.underscore}.#{key}", **i18n_options(enum, options))
|
36
|
+
dict = I18n.t("enum_ish.#{@klass.name.to_s.underscore}.#{key}", **i18n_options(enum, options))
|
37
37
|
dict.map { |k, v| [k.to_s.to_sym, v.to_s] }.to_h
|
38
38
|
end
|
39
39
|
|
@@ -44,7 +44,12 @@ module EnumIsh
|
|
44
44
|
def i18n_options(enum, options)
|
45
45
|
key = i18n_key(enum, options)
|
46
46
|
opts = options[:i18n_options] || {}
|
47
|
-
opts.merge(default: [:"enum_ish.defaults.#{key}", enum.mapping.invert])
|
47
|
+
opts.merge(default: i18n_ancestors(key) + [:"enum_ish.defaults.#{key}", enum.mapping.invert])
|
48
|
+
end
|
49
|
+
|
50
|
+
def i18n_ancestors(key)
|
51
|
+
ancestors = @klass.ancestors.drop(1).select { |a| a.is_a?(Class) && !a.name.empty? }
|
52
|
+
ancestors.map { |a| :"enum_ish.#{a.name.underscore}.#{key}" }
|
48
53
|
end
|
49
54
|
|
50
55
|
def filter(translated, options)
|
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.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshikazu Kaneta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|