enum_ish 1.5.1 → 1.6.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: cb60bdd7ea33e3c85bee0ed814e651bd00736970de81cc3c6e0b47b384f42017
4
- data.tar.gz: 1a206653955294e0bd6784f7e34098df04ff588fc7c16f0417d3da9c3425ee32
3
+ metadata.gz: 9578ab7ba6e720321c8706fabc463af41bcd7244e24d65ff736943a197abdc9b
4
+ data.tar.gz: 69b73002c3b790e6a737db306fadfb411e418c1ed5e25faf53a96c57f9f44582
5
5
  SHA512:
6
- metadata.gz: 825669f678a2d0eb6dd23a656bf00d929920f41bd6b44daeda81a1a257c544219b0e57c0c0e9ef392f53c7b79ec4980a6e976b40f533d71d34496e7546302cef
7
- data.tar.gz: 737648557aa906080bf70f1f3f2eb9cbf007490853a1c0f61a604d15b8201178083eeb9c503aec297d347f87555131f8765e7b0b9b9bd262dad6f9db3942ca27
6
+ metadata.gz: 955eb532c9bc238d8721589240bf600412e679f97b6d0c782f2b34916db04dbd757b389f551fa32605516216a5031730ac37c9778df9ff947cd81825c74e7cf7
7
+ data.tar.gz: ba5eabfa271493a71ea0d08950b674662535df79a37386b4b59b46000e365ed95f8175946edbd2236310de21b16fb6de3af827766b61db35f3d16820e56cdaf9
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
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
+
3
8
  ## 1.5.1
4
9
 
5
10
  * Fix wrong cache lookup.
data/README.md CHANGED
@@ -206,7 +206,7 @@ Rails.application.config.middleware.use EnumIsh::DictionaryCache
206
206
  ```
207
207
 
208
208
  This middleware enables dictionary cache for each request.
209
- Prformance of dictionary lookup will be improved in case one dictionary is used repeatedly in the same request.
209
+ Performance of dictionary lookup will be improved in case one dictionary is used repeatedly in the same request.
210
210
 
211
211
  ## Contributing
212
212
 
@@ -11,7 +11,7 @@ module EnumIsh
11
11
  after_initialize method
12
12
  define_method method do
13
13
  if respond_to?(enum.name) && public_send(enum.name).nil?
14
- default = enum.setting[:default]
14
+ default = enum.default
15
15
  default = instance_exec(&default) if default.kind_of?(Proc)
16
16
  public_send("#{enum.name}=", default)
17
17
  end
@@ -21,7 +21,7 @@ module EnumIsh
21
21
 
22
22
  def define_accessor(enum)
23
23
  @klass.class_eval do
24
- define_method "#{Config.raw_prefix}#{enum.name}#{Config.raw_suffix}" do
24
+ define_method enum.raw_method do
25
25
  value = read_attribute(enum.name)
26
26
  enum.mapping.fetch(value, value)
27
27
  end
@@ -40,10 +40,10 @@ module EnumIsh
40
40
 
41
41
  def define_scope(enum)
42
42
  @klass.class_eval do
43
- scope "#{Config.scope_prefix}#{enum.name}#{Config.scope_suffix}", ->(*value) {
43
+ scope enum.scope_method, ->(*value) {
44
44
  where(enum.name => enum.mapping.fetch(value, value))
45
45
  }
46
- scope "#{Config.scope_prefix}#{enum.name}_not#{Config.scope_suffix}", ->(*value) {
46
+ scope enum.scope_method(:negative), ->(*value) {
47
47
  where.not(enum.name => enum.mapping.fetch(value, value))
48
48
  }
49
49
  end
data/lib/enum_ish/base.rb CHANGED
@@ -14,11 +14,11 @@ module EnumIsh
14
14
  end
15
15
 
16
16
  class_methods do
17
- def enum_ish(name, map, config = {})
18
- enum = Enum.new(name, map, config)
17
+ def enum_ish(name, map, setting = {})
18
+ enum = Enum.new(name, map, Config.defaults.merge(setting))
19
19
  self._enum_ish_enums = _enum_ish_enums.merge(name.to_sym => enum)
20
20
 
21
- if defined?(ActiveRecord::Base) && self.ancestors.include?(ActiveRecord::Base)
21
+ if defined?(ActiveRecord::Base) && ancestors.include?(ActiveRecord::Base)
22
22
  ActiveRecordDefiner.new(self).define(enum)
23
23
  else
24
24
  Definer.new(self).define(enum)
@@ -5,6 +5,7 @@ module EnumIsh
5
5
  class_attribute :data
6
6
 
7
7
  self.data = {
8
+ defaults: { text: true, options: true },
8
9
  text_prefix: '',
9
10
  text_suffix: '_text',
10
11
  options_prefix: '',
@@ -7,8 +7,8 @@ module EnumIsh
7
7
  end
8
8
 
9
9
  def define(enum)
10
- [:text, :options, :predicate, :default, :accessor, :validate, :scope].each do |type|
11
- send("define_#{type}", enum) if enum.setting[type]
10
+ enum.features.each do |feature|
11
+ send("define_#{feature}", enum)
12
12
  end
13
13
  end
14
14
 
@@ -16,7 +16,7 @@ module EnumIsh
16
16
 
17
17
  def define_text(enum)
18
18
  @klass.class_eval do
19
- define_method "#{Config.text_prefix}#{enum.name}#{Config.text_suffix}" do |options = {}|
19
+ define_method enum.text_method do |options = {}|
20
20
  dict = Dictionary.new(self.class, enum, options)
21
21
  dict.translate_value(public_send(enum.name))
22
22
  end
@@ -25,7 +25,7 @@ module EnumIsh
25
25
 
26
26
  def define_options(enum)
27
27
  @klass.class_eval do
28
- define_singleton_method "#{Config.options_prefix}#{enum.name}#{Config.options_suffix}" do |options = {}|
28
+ define_singleton_method enum.options_method do |options = {}|
29
29
  dict = Dictionary.new(self, enum, options)
30
30
  dict.translate_options
31
31
  end
@@ -33,24 +33,14 @@ module EnumIsh
33
33
  end
34
34
 
35
35
  def define_predicate(enum)
36
- enum.mapping.each do |enum_key, enum_value|
37
- method_name = if enum.setting[:predicate].is_a?(Hash) && enum.setting[:predicate][:prefix] == false
38
- "#{enum_key}?"
39
- else
40
- "#{enum.name}_#{enum_key}?"
41
- end
42
- target_value = if enum.setting[:accessor]
43
- enum_key
44
- else
45
- enum_value
46
- end
36
+ enum.mapping.keys.each do |key|
47
37
  @klass.class_eval do
48
- define_method method_name.tr('.', '_') do
38
+ define_method enum.predicate_method(key) do
49
39
  value = public_send(enum.name)
50
40
  if value.is_a?(Array)
51
- value == [target_value]
41
+ value == [enum.value(key)]
52
42
  else
53
- value == target_value
43
+ value == enum.value(key)
54
44
  end
55
45
  end
56
46
  end
@@ -63,7 +53,7 @@ module EnumIsh
63
53
  define_method :initialize do |*args|
64
54
  super(*args)
65
55
  if respond_to?(enum.name) && public_send(enum.name).nil?
66
- default = enum.setting[:default]
56
+ default = enum.default
67
57
  default = instance_exec(&default) if default.kind_of?(Proc)
68
58
  public_send("#{enum.name}=", default)
69
59
  end
@@ -74,7 +64,7 @@ module EnumIsh
74
64
 
75
65
  def define_accessor(enum)
76
66
  @klass.class_eval do
77
- define_method "#{Config.raw_prefix}#{enum.name}#{Config.raw_suffix}" do
67
+ define_method enum.raw_method do
78
68
  instance_variable_get("@#{enum.name}")
79
69
  end
80
70
  define_method "#{enum.name}" do
@@ -87,13 +77,8 @@ module EnumIsh
87
77
  end
88
78
 
89
79
  def define_validate(enum)
90
- targets = if enum.setting[:accessor]
91
- enum.mapping.keys
92
- else
93
- enum.mapping.values
94
- end
95
80
  @klass.class_eval do
96
- validates enum.name, inclusion: { in: targets }, allow_nil: true
81
+ validates enum.name, inclusion: { in: enum.values }, allow_nil: true
97
82
  end
98
83
  end
99
84
 
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'dictionary_lookup'
4
+
3
5
  module EnumIsh
4
6
  class Dictionary
5
- CACHE_KEY = :_enum_ish_dictionary_cache
6
-
7
7
  def initialize(klass, enum, options = {})
8
8
  @klass = klass
9
9
  @enum = enum
10
10
  @options = options
11
- @dict = cache { Lookup.new(@klass, @enum, @options).call }
11
+ @dict = cache { DictionaryLookup.new(@klass, @enum, @options).call }
12
12
  end
13
13
 
14
14
  def translate_value(value)
@@ -20,13 +20,13 @@ module EnumIsh
20
20
  end
21
21
 
22
22
  def translate_options
23
- @dict.to_a.map { |value, label| [label, value] }
23
+ @dict.to_a.map { |value, text| [text, value] }
24
24
  end
25
25
 
26
26
  private
27
27
 
28
28
  def cache
29
- if (cache = Thread.current[CACHE_KEY]) != nil
29
+ if (cache = EnumIsh::DictionaryCache.cache) != nil
30
30
  cache[I18n.locale] ||= {}
31
31
  cache[I18n.locale][@klass] ||= {}
32
32
  cache[I18n.locale][@klass][@enum] ||= {}
@@ -35,62 +35,5 @@ module EnumIsh
35
35
  yield
36
36
  end
37
37
  end
38
-
39
- class << self
40
- def cache
41
- Thread.current[CACHE_KEY] = {}
42
- yield
43
- ensure
44
- Thread.current[CACHE_KEY] = nil
45
- end
46
- end
47
- end
48
-
49
- class Lookup
50
- def initialize(klass, enum, options = {})
51
- @klass = klass
52
- @enum = enum
53
- @options = options
54
- end
55
-
56
- def call
57
- i18n = lookup_for(@klass).transform_keys { |k| k.to_s.to_sym }
58
-
59
- dict = {}
60
- if @enum.setting[:accessor]
61
- @enum.mapping.each { |k, v| dict[k] = i18n[k].to_s }
62
- else
63
- @enum.mapping.each { |k, v| dict[v] = i18n[k].to_s }
64
- end
65
-
66
- filter(dict)
67
- end
68
-
69
- private
70
-
71
- def lookup_for(klass)
72
- key = [@enum.name, @options[:format]].compact.join('/')
73
- options = (@options[:i18n_options] || {}).merge(default: nil)
74
-
75
- if klass.name.to_s.in?(['ActiveRecord::Base', 'Object'])
76
- I18n.t(:"enum_ish.defaults.#{key}", **options) || @enum.mapping.invert
77
- elsif klass.name.blank? || !klass.is_a?(Class)
78
- resolve(klass.superclass)
79
- else
80
- I18n.t(:"enum_ish.#{klass.name.underscore}.#{key}", **options) || lookup_for(klass.superclass)
81
- end
82
- end
83
-
84
- def filter(dict)
85
- if @options[:except]
86
- except = Array(@options[:except])
87
- dict.reject! { |k, v| except.include?(k) }
88
- end
89
- if @options[:only]
90
- only = Array(@options[:only])
91
- dict.select! { |k, v| only.include?(k) }
92
- end
93
- dict
94
- end
95
38
  end
96
39
  end
@@ -7,9 +7,25 @@ module EnumIsh
7
7
  end
8
8
 
9
9
  def call(env)
10
- EnumIsh::Dictionary.cache do
10
+ EnumIsh::DictionaryCache.enable do
11
11
  @app.call(env)
12
12
  end
13
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
14
30
  end
15
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
@@ -2,6 +2,9 @@
2
2
 
3
3
  module EnumIsh
4
4
  class Enum
5
+ class_attribute :features
6
+ self.features = [:text, :options, :predicate, :default, :accessor, :validate, :scope]
7
+
5
8
  attr_accessor :name
6
9
  attr_accessor :mapping
7
10
  attr_accessor :setting
@@ -9,7 +12,37 @@ module EnumIsh
9
12
  def initialize(name, mapping, setting)
10
13
  @name = name
11
14
  @mapping = init_mapping(mapping)
12
- @setting = init_setting(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
13
46
  end
14
47
 
15
48
  def to_raw(value)
@@ -29,6 +62,35 @@ module EnumIsh
29
62
  end
30
63
  end
31
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
+
32
94
  private
33
95
 
34
96
  def init_mapping(mapping)
@@ -42,12 +104,5 @@ module EnumIsh
42
104
  mapping
43
105
  end
44
106
  end
45
-
46
- def init_setting(setting)
47
- [:text, :options].each do |key|
48
- setting[key] = true unless setting.key?(key)
49
- end
50
- setting
51
- end
52
107
  end
53
108
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EnumIsh
4
- VERSION = '1.5.1'
4
+ VERSION = '1.6.0'
5
5
  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.5.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-07-05 00:00:00.000000000 Z
11
+ date: 2021-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -123,6 +123,7 @@ files:
123
123
  - lib/enum_ish/definer.rb
124
124
  - lib/enum_ish/dictionary.rb
125
125
  - lib/enum_ish/dictionary_cache.rb
126
+ - lib/enum_ish/dictionary_lookup.rb
126
127
  - lib/enum_ish/enum.rb
127
128
  - lib/enum_ish/errors.rb
128
129
  - lib/enum_ish/version.rb
@@ -144,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
145
  - !ruby/object:Gem::Version
145
146
  version: '0'
146
147
  requirements: []
147
- rubygems_version: 3.1.2
148
+ rubygems_version: 3.1.6
148
149
  signing_key:
149
150
  specification_version: 4
150
151
  summary: A ruby and rails extension to generate enum-like methods