enum_ish 1.4.0 → 1.6.0
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 +1 -1
- data/CHANGELOG.md +20 -0
- data/README.md +20 -9
- data/lib/enum_ish.rb +10 -18
- data/lib/enum_ish/active_record_definer.rb +6 -4
- data/lib/enum_ish/active_record_enum_type.rb +2 -0
- data/lib/enum_ish/base.rb +29 -0
- data/lib/enum_ish/config.rb +9 -10
- data/lib/enum_ish/definer.rb +13 -26
- data/lib/enum_ish/dictionary.rb +17 -45
- data/lib/enum_ish/dictionary_cache.rb +31 -0
- data/lib/enum_ish/dictionary_lookup.rb +51 -0
- data/lib/enum_ish/enum.rb +65 -8
- data/lib/enum_ish/errors.rb +2 -0
- data/lib/enum_ish/version.rb +3 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9578ab7ba6e720321c8706fabc463af41bcd7244e24d65ff736943a197abdc9b
|
4
|
+
data.tar.gz: 69b73002c3b790e6a737db306fadfb411e418c1ed5e25faf53a96c57f9f44582
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 955eb532c9bc238d8721589240bf600412e679f97b6d0c782f2b34916db04dbd757b389f551fa32605516216a5031730ac37c9778df9ff947cd81825c74e7cf7
|
7
|
+
data.tar.gz: ba5eabfa271493a71ea0d08950b674662535df79a37386b4b59b46000e365ed95f8175946edbd2236310de21b16fb6de3af827766b61db35f3d16820e56cdaf9
|
data/.github/workflows/ci.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,25 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 1.6.0
|
4
|
+
|
5
|
+
* Add `EnumIsh.config.defaults` which allows to set default options for `enum_ish`.
|
6
|
+
* Don't convert dictionary value to string.
|
7
|
+
|
8
|
+
## 1.5.1
|
9
|
+
|
10
|
+
* Fix wrong cache lookup.
|
11
|
+
|
12
|
+
## 1.5.0
|
13
|
+
|
14
|
+
* Add dictionary cache feature.
|
15
|
+
* Change module inclusion. Use 'include EnumIsh::Base' instead of 'extend EnumIsh'.
|
16
|
+
* Move configuration method to `EnumIsh`. Use `EnumIsh.configure` instead of 'EnumIsh::Config.configure'.
|
17
|
+
|
18
|
+
## 1.4.1
|
19
|
+
|
20
|
+
* Avoid overwrite of superclass's `_enum_ish_enums`.
|
21
|
+
* Filter ancestors for dictionary lookup.
|
22
|
+
|
3
23
|
## 1.4.0
|
4
24
|
|
5
25
|
* Generate `not` scope for activerecord class.
|
data/README.md
CHANGED
@@ -26,7 +26,7 @@ Extend your class using EnumIsh and define an enum-like field:
|
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
class User
|
29
|
-
|
29
|
+
include EnumIsh::Base
|
30
30
|
attr_accessor :status # status is a string field
|
31
31
|
enum_ish :status, ['enable', 'disable'] # status has 'enable' or 'disable'
|
32
32
|
end
|
@@ -82,7 +82,7 @@ Set default value:
|
|
82
82
|
|
83
83
|
```ruby
|
84
84
|
class User
|
85
|
-
|
85
|
+
include EnumIsh::Base
|
86
86
|
attr_accessor :status
|
87
87
|
enum_ish :status, ['enable', 'disable'], default: 'enable'
|
88
88
|
end
|
@@ -95,7 +95,7 @@ Use default value with block:
|
|
95
95
|
|
96
96
|
```ruby
|
97
97
|
class User
|
98
|
-
|
98
|
+
include EnumIsh::Base
|
99
99
|
attr_accessor :status, :flag
|
100
100
|
enum_ish :status, ['enable', 'disable'], default: -> { flag ? 'enable' : 'disable' }
|
101
101
|
end
|
@@ -110,7 +110,7 @@ Generate predicate methods:
|
|
110
110
|
|
111
111
|
```ruby
|
112
112
|
class User
|
113
|
-
|
113
|
+
include EnumIsh::Base
|
114
114
|
attr_accessor :status
|
115
115
|
enum_ish :status, ['enable', 'disable'], predicate: true
|
116
116
|
end
|
@@ -125,7 +125,7 @@ Without prefix:
|
|
125
125
|
|
126
126
|
```ruby
|
127
127
|
class User
|
128
|
-
|
128
|
+
include EnumIsh::Base
|
129
129
|
attr_accessor :status
|
130
130
|
enum_ish :status, ['enable', 'disable'], predicate: { prefix: false }
|
131
131
|
end
|
@@ -142,7 +142,7 @@ Generate getter and setter for aliased symbols instead of raw values:
|
|
142
142
|
|
143
143
|
```ruby
|
144
144
|
class User
|
145
|
-
|
145
|
+
include EnumIsh::Base
|
146
146
|
attr_accessor :status
|
147
147
|
enum_ish :status, { _enable: 'enable', _disable: 'disable' }, accessor: true
|
148
148
|
end
|
@@ -161,7 +161,7 @@ Generate accessor:
|
|
161
161
|
|
162
162
|
```ruby
|
163
163
|
class User < ActiveRecord::Base
|
164
|
-
|
164
|
+
include EnumIsh::Base
|
165
165
|
enum_ish :status, { _enable: 'enable', _disable: 'disable' }, accessor: true
|
166
166
|
end
|
167
167
|
|
@@ -174,7 +174,7 @@ Generate scope:
|
|
174
174
|
|
175
175
|
```ruby
|
176
176
|
class User < ActiveRecord::Base
|
177
|
-
|
177
|
+
include EnumIsh::Base
|
178
178
|
enum_ish :status, ['enable', 'disable'], scope: true
|
179
179
|
end
|
180
180
|
|
@@ -188,7 +188,7 @@ Generate validation:
|
|
188
188
|
|
189
189
|
```ruby
|
190
190
|
class User < ActiveRecord::Base
|
191
|
-
|
191
|
+
include EnumIsh::Base
|
192
192
|
enum_ish :status, ['enable', 'disable'], validate: true
|
193
193
|
end
|
194
194
|
|
@@ -197,6 +197,17 @@ user.status = 'INVALID'
|
|
197
197
|
user.valid? #=> false
|
198
198
|
```
|
199
199
|
|
200
|
+
### Dictionary cache
|
201
|
+
|
202
|
+
You can enable dictionary cache using rack middleware:
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
Rails.application.config.middleware.use EnumIsh::DictionaryCache
|
206
|
+
```
|
207
|
+
|
208
|
+
This middleware enables dictionary cache for each request.
|
209
|
+
Performance of dictionary lookup will be improved in case one dictionary is used repeatedly in the same request.
|
210
|
+
|
200
211
|
## Contributing
|
201
212
|
|
202
213
|
Bug reports and pull requests are welcome at https://github.com/kanety/enum_ish.
|
data/lib/enum_ish.rb
CHANGED
@@ -1,27 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support'
|
2
4
|
require 'enum_ish/version'
|
3
|
-
require 'enum_ish/errors'
|
4
5
|
require 'enum_ish/config'
|
5
|
-
require 'enum_ish/
|
6
|
-
require 'enum_ish/
|
7
|
-
require 'enum_ish/definer'
|
8
|
-
require 'enum_ish/active_record_definer' if defined?(ActiveRecord::Base)
|
6
|
+
require 'enum_ish/base'
|
7
|
+
require 'enum_ish/dictionary_cache'
|
9
8
|
|
10
9
|
module EnumIsh
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
self._enum_ish_enums ||= {}
|
15
|
-
self._enum_ish_enums[name.to_sym] = enum
|
16
|
-
|
17
|
-
if defined?(ActiveRecord::Base) && self.ancestors.include?(ActiveRecord::Base)
|
18
|
-
ActiveRecordDefiner.new(self).define(enum)
|
19
|
-
else
|
20
|
-
Definer.new(self).define(enum)
|
10
|
+
class << self
|
11
|
+
def configure
|
12
|
+
yield Config
|
21
13
|
end
|
22
|
-
end
|
23
14
|
|
24
|
-
|
25
|
-
|
15
|
+
def config
|
16
|
+
Config
|
17
|
+
end
|
26
18
|
end
|
27
19
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'active_record_enum_type'
|
2
4
|
|
3
5
|
module EnumIsh
|
@@ -9,7 +11,7 @@ module EnumIsh
|
|
9
11
|
after_initialize method
|
10
12
|
define_method method do
|
11
13
|
if respond_to?(enum.name) && public_send(enum.name).nil?
|
12
|
-
default = enum.
|
14
|
+
default = enum.default
|
13
15
|
default = instance_exec(&default) if default.kind_of?(Proc)
|
14
16
|
public_send("#{enum.name}=", default)
|
15
17
|
end
|
@@ -19,7 +21,7 @@ module EnumIsh
|
|
19
21
|
|
20
22
|
def define_accessor(enum)
|
21
23
|
@klass.class_eval do
|
22
|
-
define_method
|
24
|
+
define_method enum.raw_method do
|
23
25
|
value = read_attribute(enum.name)
|
24
26
|
enum.mapping.fetch(value, value)
|
25
27
|
end
|
@@ -38,10 +40,10 @@ module EnumIsh
|
|
38
40
|
|
39
41
|
def define_scope(enum)
|
40
42
|
@klass.class_eval do
|
41
|
-
scope
|
43
|
+
scope enum.scope_method, ->(*value) {
|
42
44
|
where(enum.name => enum.mapping.fetch(value, value))
|
43
45
|
}
|
44
|
-
scope
|
46
|
+
scope enum.scope_method(:negative), ->(*value) {
|
45
47
|
where.not(enum.name => enum.mapping.fetch(value, value))
|
46
48
|
}
|
47
49
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'enum'
|
2
|
+
require_relative 'dictionary'
|
3
|
+
require_relative 'errors'
|
4
|
+
require_relative 'definer'
|
5
|
+
require_relative 'active_record_definer' if defined?(ActiveRecord::Base)
|
6
|
+
|
7
|
+
module EnumIsh
|
8
|
+
module Base
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
class_attribute :_enum_ish_enums
|
13
|
+
self._enum_ish_enums = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
class_methods do
|
17
|
+
def enum_ish(name, map, setting = {})
|
18
|
+
enum = Enum.new(name, map, Config.defaults.merge(setting))
|
19
|
+
self._enum_ish_enums = _enum_ish_enums.merge(name.to_sym => enum)
|
20
|
+
|
21
|
+
if defined?(ActiveRecord::Base) && ancestors.include?(ActiveRecord::Base)
|
22
|
+
ActiveRecordDefiner.new(self).define(enum)
|
23
|
+
else
|
24
|
+
Definer.new(self).define(enum)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/enum_ish/config.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module EnumIsh
|
2
4
|
class Config
|
3
|
-
|
5
|
+
class_attribute :data
|
6
|
+
|
7
|
+
self.data = {
|
8
|
+
defaults: { text: true, options: true },
|
4
9
|
text_prefix: '',
|
5
10
|
text_suffix: '_text',
|
6
11
|
options_prefix: '',
|
@@ -11,19 +16,13 @@ module EnumIsh
|
|
11
16
|
scope_suffix: ''
|
12
17
|
}
|
13
18
|
|
14
|
-
|
19
|
+
data.keys.each do |key|
|
15
20
|
define_singleton_method "#{key}" do
|
16
|
-
|
21
|
+
data[key]
|
17
22
|
end
|
18
23
|
|
19
24
|
define_singleton_method "#{key}=" do |val|
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class << self
|
25
|
-
def configure
|
26
|
-
yield self
|
25
|
+
data[key] = val
|
27
26
|
end
|
28
27
|
end
|
29
28
|
end
|
data/lib/enum_ish/definer.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module EnumIsh
|
2
4
|
class Definer
|
3
5
|
def initialize(klass)
|
@@ -5,8 +7,8 @@ module EnumIsh
|
|
5
7
|
end
|
6
8
|
|
7
9
|
def define(enum)
|
8
|
-
|
9
|
-
send("define_#{
|
10
|
+
enum.features.each do |feature|
|
11
|
+
send("define_#{feature}", enum)
|
10
12
|
end
|
11
13
|
end
|
12
14
|
|
@@ -14,7 +16,7 @@ module EnumIsh
|
|
14
16
|
|
15
17
|
def define_text(enum)
|
16
18
|
@klass.class_eval do
|
17
|
-
define_method
|
19
|
+
define_method enum.text_method do |options = {}|
|
18
20
|
dict = Dictionary.new(self.class, enum, options)
|
19
21
|
dict.translate_value(public_send(enum.name))
|
20
22
|
end
|
@@ -23,7 +25,7 @@ module EnumIsh
|
|
23
25
|
|
24
26
|
def define_options(enum)
|
25
27
|
@klass.class_eval do
|
26
|
-
define_singleton_method
|
28
|
+
define_singleton_method enum.options_method do |options = {}|
|
27
29
|
dict = Dictionary.new(self, enum, options)
|
28
30
|
dict.translate_options
|
29
31
|
end
|
@@ -31,24 +33,14 @@ module EnumIsh
|
|
31
33
|
end
|
32
34
|
|
33
35
|
def define_predicate(enum)
|
34
|
-
enum.mapping.each do |
|
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
|
36
|
+
enum.mapping.keys.each do |key|
|
45
37
|
@klass.class_eval do
|
46
|
-
define_method
|
38
|
+
define_method enum.predicate_method(key) do
|
47
39
|
value = public_send(enum.name)
|
48
40
|
if value.is_a?(Array)
|
49
|
-
value == [
|
41
|
+
value == [enum.value(key)]
|
50
42
|
else
|
51
|
-
value ==
|
43
|
+
value == enum.value(key)
|
52
44
|
end
|
53
45
|
end
|
54
46
|
end
|
@@ -61,7 +53,7 @@ module EnumIsh
|
|
61
53
|
define_method :initialize do |*args|
|
62
54
|
super(*args)
|
63
55
|
if respond_to?(enum.name) && public_send(enum.name).nil?
|
64
|
-
default = enum.
|
56
|
+
default = enum.default
|
65
57
|
default = instance_exec(&default) if default.kind_of?(Proc)
|
66
58
|
public_send("#{enum.name}=", default)
|
67
59
|
end
|
@@ -72,7 +64,7 @@ module EnumIsh
|
|
72
64
|
|
73
65
|
def define_accessor(enum)
|
74
66
|
@klass.class_eval do
|
75
|
-
define_method
|
67
|
+
define_method enum.raw_method do
|
76
68
|
instance_variable_get("@#{enum.name}")
|
77
69
|
end
|
78
70
|
define_method "#{enum.name}" do
|
@@ -85,13 +77,8 @@ module EnumIsh
|
|
85
77
|
end
|
86
78
|
|
87
79
|
def define_validate(enum)
|
88
|
-
targets = if enum.setting[:accessor]
|
89
|
-
enum.mapping.keys
|
90
|
-
else
|
91
|
-
enum.mapping.values
|
92
|
-
end
|
93
80
|
@klass.class_eval do
|
94
|
-
validates enum.name, inclusion: { in:
|
81
|
+
validates enum.name, inclusion: { in: enum.values }, allow_nil: true
|
95
82
|
end
|
96
83
|
end
|
97
84
|
|
data/lib/enum_ish/dictionary.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'dictionary_lookup'
|
4
|
+
|
1
5
|
module EnumIsh
|
2
6
|
class Dictionary
|
3
7
|
def initialize(klass, enum, options = {})
|
4
8
|
@klass = klass
|
5
|
-
@
|
9
|
+
@enum = enum
|
10
|
+
@options = options
|
11
|
+
@dict = cache { DictionaryLookup.new(@klass, @enum, @options).call }
|
6
12
|
end
|
7
13
|
|
8
14
|
def translate_value(value)
|
@@ -11,57 +17,23 @@ module EnumIsh
|
|
11
17
|
else
|
12
18
|
@dict[value] || value
|
13
19
|
end
|
14
|
-
|
20
|
+
end
|
15
21
|
|
16
22
|
def translate_options
|
17
|
-
@dict.to_a.map { |value,
|
23
|
+
@dict.to_a.map { |value, text| [text, value] }
|
18
24
|
end
|
19
25
|
|
20
26
|
private
|
21
27
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
translated = translated.map { |k, v| [enum.mapping[k], v] }.to_h unless enum.setting[:accessor]
|
31
|
-
translated
|
32
|
-
end
|
33
|
-
|
34
|
-
def load_dict(enum, options)
|
35
|
-
key = i18n_key(enum, options)
|
36
|
-
dict = I18n.t("enum_ish.#{@klass.name.to_s.underscore}.#{key}", **i18n_options(enum, options))
|
37
|
-
dict.map { |k, v| [k.to_s.to_sym, v.to_s] }.to_h
|
38
|
-
end
|
39
|
-
|
40
|
-
def i18n_key(enum, options)
|
41
|
-
[enum.name, options[:format]].compact.join('/')
|
42
|
-
end
|
43
|
-
|
44
|
-
def i18n_options(enum, options)
|
45
|
-
key = i18n_key(enum, options)
|
46
|
-
opts = options[:i18n_options] || {}
|
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}" }
|
53
|
-
end
|
54
|
-
|
55
|
-
def filter(translated, options)
|
56
|
-
if options[:except]
|
57
|
-
except = Array(options[:except])
|
58
|
-
translated.reject! { |k, v| except.include?(k) }
|
59
|
-
end
|
60
|
-
if options[:only]
|
61
|
-
only = Array(options[:only])
|
62
|
-
translated.select! { |k, v| only.include?(k) }
|
28
|
+
def cache
|
29
|
+
if (cache = EnumIsh::DictionaryCache.cache) != nil
|
30
|
+
cache[I18n.locale] ||= {}
|
31
|
+
cache[I18n.locale][@klass] ||= {}
|
32
|
+
cache[I18n.locale][@klass][@enum] ||= {}
|
33
|
+
cache[I18n.locale][@klass][@enum][@options] ||= yield
|
34
|
+
else
|
35
|
+
yield
|
63
36
|
end
|
64
|
-
translated
|
65
37
|
end
|
66
38
|
end
|
67
39
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EnumIsh
|
4
|
+
class DictionaryCache
|
5
|
+
def initialize(app)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
EnumIsh::DictionaryCache.enable do
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
class_attribute :cache_key
|
17
|
+
self.cache_key = :_enum_ish_dictionary_cache
|
18
|
+
|
19
|
+
def cache
|
20
|
+
Thread.current[cache_key]
|
21
|
+
end
|
22
|
+
|
23
|
+
def enable
|
24
|
+
Thread.current[cache_key] = {}
|
25
|
+
yield
|
26
|
+
ensure
|
27
|
+
Thread.current[cache_key] = nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module EnumIsh
|
2
|
+
class DictionaryLookup
|
3
|
+
def initialize(klass, enum, options = {})
|
4
|
+
@klass = klass
|
5
|
+
@enum = enum
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def call
|
10
|
+
i18n = lookup_for(@klass).transform_keys { |k| k.to_s.to_sym }
|
11
|
+
|
12
|
+
dict = {}
|
13
|
+
if @enum.use?(:accessor)
|
14
|
+
@enum.mapping.each { |k, v| dict[k] = i18n[k] }
|
15
|
+
else
|
16
|
+
@enum.mapping.each { |k, v| dict[v] = i18n[k] }
|
17
|
+
end
|
18
|
+
|
19
|
+
filter(dict)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def lookup_for(klass)
|
25
|
+
if klass.name.blank? || !klass.is_a?(Class)
|
26
|
+
return lookup_for(klass.superclass)
|
27
|
+
end
|
28
|
+
|
29
|
+
key = [@enum.name, @options[:format]].compact.join('/')
|
30
|
+
options = (@options[:i18n_options] || {}).merge(default: nil)
|
31
|
+
|
32
|
+
if klass.name.to_s.in?(['ActiveRecord::Base', 'Object'])
|
33
|
+
I18n.t(:"enum_ish.defaults.#{key}", **options) || @enum.mapping
|
34
|
+
else
|
35
|
+
I18n.t(:"enum_ish.#{klass.name.underscore}.#{key}", **options) || lookup_for(klass.superclass)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def filter(dict)
|
40
|
+
if @options[:except]
|
41
|
+
except = Array(@options[:except])
|
42
|
+
dict.reject! { |k, v| except.include?(k) }
|
43
|
+
end
|
44
|
+
if @options[:only]
|
45
|
+
only = Array(@options[:only])
|
46
|
+
dict.select! { |k, v| only.include?(k) }
|
47
|
+
end
|
48
|
+
dict
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/enum_ish/enum.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module EnumIsh
|
2
4
|
class Enum
|
5
|
+
class_attribute :features
|
6
|
+
self.features = [:text, :options, :predicate, :default, :accessor, :validate, :scope]
|
7
|
+
|
3
8
|
attr_accessor :name
|
4
9
|
attr_accessor :mapping
|
5
10
|
attr_accessor :setting
|
@@ -7,7 +12,37 @@ module EnumIsh
|
|
7
12
|
def initialize(name, mapping, setting)
|
8
13
|
@name = name
|
9
14
|
@mapping = init_mapping(mapping)
|
10
|
-
@setting =
|
15
|
+
@setting = setting
|
16
|
+
end
|
17
|
+
|
18
|
+
features.each do |feature|
|
19
|
+
define_method feature do
|
20
|
+
setting.fetch(feature)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def use?(feature)
|
25
|
+
@setting.key?(feature)
|
26
|
+
end
|
27
|
+
|
28
|
+
def features
|
29
|
+
self.class.features.select { |feature| use?(feature) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def values
|
33
|
+
if use?(:accessor)
|
34
|
+
@mapping.keys
|
35
|
+
else
|
36
|
+
@mapping.values
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def value(key)
|
41
|
+
if use?(:accessor)
|
42
|
+
key
|
43
|
+
else
|
44
|
+
@mapping[key]
|
45
|
+
end
|
11
46
|
end
|
12
47
|
|
13
48
|
def to_raw(value)
|
@@ -27,6 +62,35 @@ module EnumIsh
|
|
27
62
|
end
|
28
63
|
end
|
29
64
|
|
65
|
+
def text_method
|
66
|
+
"#{Config.text_prefix}#{@name}#{Config.text_suffix}"
|
67
|
+
end
|
68
|
+
|
69
|
+
def options_method
|
70
|
+
"#{Config.options_prefix}#{@name}#{Config.options_suffix}"
|
71
|
+
end
|
72
|
+
|
73
|
+
def predicate_method(key)
|
74
|
+
key = key.to_s.tr('.', '_')
|
75
|
+
if predicate.is_a?(Hash) && predicate[:prefix] == false
|
76
|
+
"#{key}?"
|
77
|
+
else
|
78
|
+
"#{@name}_#{key}?"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def raw_method
|
83
|
+
"#{Config.raw_prefix}#{@name}#{Config.raw_suffix}"
|
84
|
+
end
|
85
|
+
|
86
|
+
def scope_method(type = nil)
|
87
|
+
if type == :negative
|
88
|
+
"#{Config.scope_prefix}#{@name}_not#{Config.scope_suffix}"
|
89
|
+
else
|
90
|
+
"#{Config.scope_prefix}#{@name}#{Config.scope_suffix}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
30
94
|
private
|
31
95
|
|
32
96
|
def init_mapping(mapping)
|
@@ -40,12 +104,5 @@ module EnumIsh
|
|
40
104
|
mapping
|
41
105
|
end
|
42
106
|
end
|
43
|
-
|
44
|
-
def init_setting(setting)
|
45
|
-
[:text, :options].each do |key|
|
46
|
-
setting[key] = true unless setting.key?(key)
|
47
|
-
end
|
48
|
-
setting
|
49
|
-
end
|
50
107
|
end
|
51
108
|
end
|
data/lib/enum_ish/errors.rb
CHANGED
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.6.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-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -118,9 +118,12 @@ files:
|
|
118
118
|
- lib/enum_ish.rb
|
119
119
|
- lib/enum_ish/active_record_definer.rb
|
120
120
|
- lib/enum_ish/active_record_enum_type.rb
|
121
|
+
- lib/enum_ish/base.rb
|
121
122
|
- lib/enum_ish/config.rb
|
122
123
|
- lib/enum_ish/definer.rb
|
123
124
|
- lib/enum_ish/dictionary.rb
|
125
|
+
- lib/enum_ish/dictionary_cache.rb
|
126
|
+
- lib/enum_ish/dictionary_lookup.rb
|
124
127
|
- lib/enum_ish/enum.rb
|
125
128
|
- lib/enum_ish/errors.rb
|
126
129
|
- lib/enum_ish/version.rb
|
@@ -142,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
145
|
- !ruby/object:Gem::Version
|
143
146
|
version: '0'
|
144
147
|
requirements: []
|
145
|
-
rubygems_version: 3.
|
148
|
+
rubygems_version: 3.1.6
|
146
149
|
signing_key:
|
147
150
|
specification_version: 4
|
148
151
|
summary: A ruby and rails extension to generate enum-like methods
|