enum_ish 1.2.3 → 1.3.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 +4 -0
- data/lib/enum_ish/definer.rb +15 -11
- data/lib/enum_ish/dictionary.rb +18 -5
- data/lib/enum_ish/enum.rb +17 -0
- 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: b366f9ef0d71e73ff92c214008d0459135a03cc5aa6b32d944c1bdc5c5042d8e
|
4
|
+
data.tar.gz: a428a5f900c77c1caf3c6c9344cbc30e4cee6f10e99868d5b1b18d481bc5ed5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0cfa988dc99c79277ae238f9235743655ec7b51e1ca6e8a3df22951b91e2091193926ca76c5e449663dc2ed64734652303dec847ee377709fba1f9f013a3efc
|
7
|
+
data.tar.gz: 48b90411d86d74ef8a4c62d0676deb93d0a784376acbb5127fdfd08b9778d46cef8d8c38b85ae754e609ae496f817568d16289f759069934f8e7188a502d9663
|
data/CHANGELOG.md
CHANGED
data/lib/enum_ish/definer.rb
CHANGED
@@ -15,27 +15,31 @@ module EnumIsh
|
|
15
15
|
def define_text(enum)
|
16
16
|
@klass.class_eval do
|
17
17
|
define_method "#{Config.text_prefix}#{enum.name}#{Config.text_suffix}" do |options = {}|
|
18
|
-
|
19
|
-
|
20
|
-
dic[value] || value
|
18
|
+
dict = Dictionary.new(self.class, enum, options)
|
19
|
+
dict.translate_value(public_send(enum.name))
|
21
20
|
end
|
22
|
-
end
|
21
|
+
end
|
23
22
|
end
|
24
23
|
|
25
24
|
def define_options(enum)
|
26
25
|
@klass.class_eval do
|
27
26
|
define_singleton_method "#{Config.options_prefix}#{enum.name}#{Config.options_suffix}" do |options = {}|
|
28
|
-
|
29
|
-
|
27
|
+
dict = Dictionary.new(self, enum, options)
|
28
|
+
dict.translate_options
|
30
29
|
end
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
34
33
|
def define_predicate(enum)
|
35
|
-
enum.mapping.each do |
|
34
|
+
enum.mapping.each do |enum_key, enum_value|
|
36
35
|
@klass.class_eval do
|
37
|
-
define_method "#{enum.name}_#{
|
38
|
-
public_send(enum.name)
|
36
|
+
define_method "#{enum.name}_#{enum_key}?".tr('.', '_') do
|
37
|
+
value = public_send(enum.name)
|
38
|
+
if value.is_a?(Array)
|
39
|
+
value == [enum_value]
|
40
|
+
else
|
41
|
+
value == enum_value
|
42
|
+
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
end
|
@@ -62,10 +66,10 @@ module EnumIsh
|
|
62
66
|
instance_variable_get("@#{enum.name}")
|
63
67
|
end
|
64
68
|
define_method "#{enum.name}" do
|
65
|
-
enum.
|
69
|
+
enum.to_sym(instance_variable_get("@#{enum.name}"))
|
66
70
|
end
|
67
71
|
define_method "#{enum.name}=" do |value|
|
68
|
-
instance_variable_set("@#{enum.name}", enum.
|
72
|
+
instance_variable_set("@#{enum.name}", enum.to_raw(value))
|
69
73
|
end
|
70
74
|
end
|
71
75
|
end
|
data/lib/enum_ish/dictionary.rb
CHANGED
@@ -1,17 +1,30 @@
|
|
1
1
|
module EnumIsh
|
2
2
|
class Dictionary
|
3
|
-
def initialize(klass)
|
3
|
+
def initialize(klass, enum, options = {})
|
4
4
|
@klass = klass
|
5
|
+
@dict = load(enum, options)
|
5
6
|
end
|
6
7
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
8
|
+
def translate_value(value)
|
9
|
+
if value.is_a?(Array)
|
10
|
+
value.map { |v| @dict[v] || v }
|
11
|
+
else
|
12
|
+
@dict[value] || value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def translate_options
|
17
|
+
@dict.to_a.map { |value, label| [label, value] }
|
10
18
|
end
|
11
19
|
|
12
20
|
private
|
13
21
|
|
14
|
-
def
|
22
|
+
def load(enum, options)
|
23
|
+
dict = translate_dict(enum, options)
|
24
|
+
filter(dict, options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def translate_dict(enum, options)
|
15
28
|
dict = load_dict(enum, options)
|
16
29
|
translated = enum.mapping.map { |k, v| dict[k] ? [k, dict[k]] : [k, v.to_s] }.to_h
|
17
30
|
translated = translated.map { |k, v| [enum.mapping[k], v] }.to_h unless enum.setting[:accessor]
|
data/lib/enum_ish/enum.rb
CHANGED
@@ -10,6 +10,23 @@ module EnumIsh
|
|
10
10
|
@setting = init_setting(setting)
|
11
11
|
end
|
12
12
|
|
13
|
+
def to_raw(value)
|
14
|
+
if value.is_a?(Array)
|
15
|
+
value.map { |v| @mapping.fetch(v, v) }
|
16
|
+
else
|
17
|
+
@mapping.fetch(value, value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_sym(value)
|
22
|
+
inverted = @mapping.invert
|
23
|
+
if value.is_a?(Array)
|
24
|
+
value.map { |v| inverted[v] }
|
25
|
+
else
|
26
|
+
inverted[value]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
13
30
|
private
|
14
31
|
|
15
32
|
def init_mapping(mapping)
|
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.3.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: 2019-
|
11
|
+
date: 2019-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|