enum_ish 1.4.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 +1 -1
- data/CHANGELOG.md +5 -0
- data/lib/enum_ish.rb +9 -0
- data/lib/enum_ish/dictionary.rb +39 -34
- data/lib/enum_ish/version.rb +1 -1
- metadata +3 -3
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
|
data/.github/workflows/ci.yml
CHANGED
data/CHANGELOG.md
CHANGED
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
|
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,49 +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.to_s.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('/')
|
42
|
-
|
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
|
43
50
|
|
44
|
-
|
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])
|
51
|
+
@enum.mapping.invert
|
48
52
|
end
|
49
53
|
|
50
|
-
def i18n_ancestors
|
51
|
-
|
52
|
-
|
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? }
|
53
58
|
end
|
54
59
|
|
55
|
-
def filter(
|
56
|
-
if options[:except]
|
57
|
-
except = Array(options[:except])
|
58
|
-
|
60
|
+
def filter(dict)
|
61
|
+
if @options[:except]
|
62
|
+
except = Array(@options[:except])
|
63
|
+
dict.reject! { |k, v| except.include?(k) }
|
59
64
|
end
|
60
|
-
if options[:only]
|
61
|
-
only = Array(options[:only])
|
62
|
-
|
65
|
+
if @options[:only]
|
66
|
+
only = Array(@options[:only])
|
67
|
+
dict.select! { |k, v| only.include?(k) }
|
63
68
|
end
|
64
|
-
|
69
|
+
dict
|
65
70
|
end
|
66
71
|
end
|
67
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.
|
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: 2021-05-
|
11
|
+
date: 2021-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
142
|
- !ruby/object:Gem::Version
|
143
143
|
version: '0'
|
144
144
|
requirements: []
|
145
|
-
rubygems_version: 3.2
|
145
|
+
rubygems_version: 3.1.2
|
146
146
|
signing_key:
|
147
147
|
specification_version: 4
|
148
148
|
summary: A ruby and rails extension to generate enum-like methods
|