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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53a6b89fe82d91b0ae54a440f3ba3a4908af751b3b22064f3ddd085257f975d9
4
- data.tar.gz: 033b3df71e3286482f86ad0cce38084cc7c745cf13c5c06ad3cec146c3a577ac
3
+ metadata.gz: 34d21d01c11cb32143d1ce656228165fc93059f820b60f2cc109668a61e70680
4
+ data.tar.gz: a26baa4da8d0ae6e5ad1dce8cdf169f24fbe79ec8ddc88acf5fb2180db817b6f
5
5
  SHA512:
6
- metadata.gz: f55c27359c26b39c6500357594e318da8027198b45073bfd772d9ae9047f295d53e2c23ffabf54118cb308c399b8dce546e4db0a23e6564534a7a28c9f18bdfc
7
- data.tar.gz: ca8144b132543120f9fb810398df2252ad8e1051c76e32242fef1d6029ab9a458b988902271c779580c6673fe1f6ac2cada24dd4b8ce7edf499669991721d21a
6
+ metadata.gz: cc05a7517f723a1df0f521e031d2c171ed6a3ad0614974326c52464ce5ecf7f89b5e591f0da34a15fac70212ab44ab883f64b7dff45b67ba865c153c2ebd3dd9
7
+ data.tar.gz: 8cb363ef8d2f7988dee963d3baef608b076484afa46268f1d08a7b21fe5ef12c736d09cb05ba3e6efaa01ce8b6acbf85f0748aef6d39a407338b499aa94f22c4
@@ -4,7 +4,7 @@ on: [push, pull_request]
4
4
 
5
5
  jobs:
6
6
  test:
7
- runs-on: ubuntu-16.04
7
+ runs-on: ubuntu-18.04
8
8
  strategy:
9
9
  fail-fast: false
10
10
  matrix:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.4.1
4
+
5
+ * Avoid overwrite of superclass's `_enum_ish_enums`.
6
+ * Filter ancestors for dictionary lookup.
7
+
3
8
  ## 1.4.0
4
9
 
5
10
  * Generate `not` scope for activerecord class.
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
@@ -2,7 +2,9 @@ module EnumIsh
2
2
  class Dictionary
3
3
  def initialize(klass, enum, options = {})
4
4
  @klass = klass
5
- @dict = load(enum, options)
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
- end
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 load(enum, options)
23
- dict = translate_dict(enum, options)
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
- def translate_dict(enum, options)
28
- dict = load_dict(enum, options)
29
- translated = enum.mapping.map { |k, v| dict[k] ? [k, dict[k]] : [k, v.to_s] }.to_h
30
- translated = translated.map { |k, v| [enum.mapping[k], v] }.to_h unless enum.setting[:accessor]
31
- translated
32
- end
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
- 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
34
+ filter(dict)
38
35
  end
39
36
 
40
- def i18n_key(enum, options)
41
- [enum.name, options[:format]].compact.join('/')
42
- end
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
- 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])
51
+ @enum.mapping.invert
48
52
  end
49
53
 
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}" }
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(translated, options)
56
- if options[:except]
57
- except = Array(options[:except])
58
- translated.reject! { |k, v| except.include?(k) }
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
- translated.select! { |k, v| only.include?(k) }
65
+ if @options[:only]
66
+ only = Array(@options[:only])
67
+ dict.select! { |k, v| only.include?(k) }
63
68
  end
64
- translated
69
+ dict
65
70
  end
66
71
  end
67
72
  end
@@ -1,3 +1,3 @@
1
1
  module EnumIsh
2
- VERSION = '1.4.0'
2
+ VERSION = '1.4.1'
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.4.0
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-05 00:00:00.000000000 Z
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.3
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