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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58df4091edebc5965ce060bd1342f7088f4749aaa18977c76655b44fa823245d
4
- data.tar.gz: 3e97c949be6dc3a88bc03aa20fd5317e681cc68681857581bc9d24abc0fcb38c
3
+ metadata.gz: b366f9ef0d71e73ff92c214008d0459135a03cc5aa6b32d944c1bdc5c5042d8e
4
+ data.tar.gz: a428a5f900c77c1caf3c6c9344cbc30e4cee6f10e99868d5b1b18d481bc5ed5c
5
5
  SHA512:
6
- metadata.gz: 2a08c825b4b4e77c904f77cf5088026d34dd66439c9553a47cce1abecc4dd7d52cc9514c3b48f9fab81d7bef4263bd0eb5e3cea34afdbbc446513e672a5f0276
7
- data.tar.gz: 62bd16fd0c155a7ca5144be68fb12d3b413996ce1a316caf70ede933842ce757d315054f47641d6a9de5b2dd1b1cc4744745c735a845c9cece3550d465e53622
6
+ metadata.gz: d0cfa988dc99c79277ae238f9235743655ec7b51e1ca6e8a3df22951b91e2091193926ca76c5e449663dc2ed64734652303dec847ee377709fba1f9f013a3efc
7
+ data.tar.gz: 48b90411d86d74ef8a4c62d0676deb93d0a784376acbb5127fdfd08b9778d46cef8d8c38b85ae754e609ae496f817568d16289f759069934f8e7188a502d9663
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.3.0
4
+
5
+ * Support array field.
6
+
3
7
  ## 1.2.3
4
8
 
5
9
  * Keep enum definitions as a hash internally.
@@ -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
- value = public_send(enum.name)
19
- dic = Dictionary.new(self.class).load(enum, options)
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
- dic = Dictionary.new(self).load(enum, options)
29
- dic.to_a.map { |value, label| [label, value] }
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 |key, value|
34
+ enum.mapping.each do |enum_key, enum_value|
36
35
  @klass.class_eval do
37
- define_method "#{enum.name}_#{key}?".tr('.', '_') do
38
- public_send(enum.name) == value
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.mapping.invert[instance_variable_get("@#{enum.name}")]
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.mapping.fetch(value, value))
72
+ instance_variable_set("@#{enum.name}", enum.to_raw(value))
69
73
  end
70
74
  end
71
75
  end
@@ -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 load(enum, options = {})
8
- translated = translate(enum, options)
9
- filter(translated, options)
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 translate(enum, options)
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)
@@ -1,3 +1,3 @@
1
1
  module EnumIsh
2
- VERSION = '1.2.3'
2
+ VERSION = '1.3.0'
3
3
  end
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.2.3
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-10-05 00:00:00.000000000 Z
11
+ date: 2019-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport