enum_ish 1.4.1 → 1.6.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: 34d21d01c11cb32143d1ce656228165fc93059f820b60f2cc109668a61e70680
4
- data.tar.gz: a26baa4da8d0ae6e5ad1dce8cdf169f24fbe79ec8ddc88acf5fb2180db817b6f
3
+ metadata.gz: 8fb31764431d97fa544b90f3d7534f3c96d2fc1acbbfb7d3d44fcaa73da3064e
4
+ data.tar.gz: 84897de0db79b886700f9d061159798aab16dbc120ec52d87d94677a8417de49
5
5
  SHA512:
6
- metadata.gz: cc05a7517f723a1df0f521e031d2c171ed6a3ad0614974326c52464ce5ecf7f89b5e591f0da34a15fac70212ab44ab883f64b7dff45b67ba865c153c2ebd3dd9
7
- data.tar.gz: 8cb363ef8d2f7988dee963d3baef608b076484afa46268f1d08a7b21fe5ef12c736d09cb05ba3e6efaa01ce8b6acbf85f0748aef6d39a407338b499aa94f22c4
6
+ metadata.gz: 9130d50fa059e0a1a0e9237e07b73ad7564a55e500a712ea7c15efdd232cfe69ef67cc09e222b124a819a44b57b2e9da078d9b9bb590c08b8f21e6980392cfac
7
+ data.tar.gz: 204ae64789c49108e1e9faa137aa6737c5f1d7a1c5f79cf55e354e8a80ae4fb11c89235f4dfb5f018ec89a7a7b9c50141f9a2777235a90bced4d946444e9038f
@@ -4,27 +4,41 @@ on: [push, pull_request]
4
4
 
5
5
  jobs:
6
6
  test:
7
- runs-on: ubuntu-18.04
7
+ runs-on: ubuntu-20.04
8
8
  strategy:
9
9
  fail-fast: false
10
10
  matrix:
11
- ruby: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0]
12
- gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61']
11
+ ruby: [2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1]
12
+ gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61', 'rails70']
13
13
  exclude:
14
14
  - ruby: 2.3
15
15
  gemfile: rails60
16
16
  - ruby: 2.3
17
17
  gemfile: rails61
18
+ - ruby: 2.3
19
+ gemfile: rails70
18
20
  - ruby: 2.4
19
21
  gemfile: rails60
20
22
  - ruby: 2.4
21
23
  gemfile: rails61
24
+ - ruby: 2.4
25
+ gemfile: rails70
26
+ - ruby: 2.5
27
+ gemfile: rails70
28
+ - ruby: 2.6
29
+ gemfile: rails70
22
30
  - ruby: 3.0
23
31
  gemfile: rails50
24
32
  - ruby: 3.0
25
33
  gemfile: rails51
26
34
  - ruby: 3.0
27
35
  gemfile: rails52
36
+ - ruby: 3.1
37
+ gemfile: rails50
38
+ - ruby: 3.1
39
+ gemfile: rails51
40
+ - ruby: 3.1
41
+ gemfile: rails52
28
42
 
29
43
  name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}
30
44
 
data/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.6.1
4
+
5
+ * Support activerecord 7.0.
6
+
7
+ ## 1.6.0
8
+
9
+ * Add `EnumIsh.config.defaults` which allows to set default options for `enum_ish`.
10
+ * Don't convert dictionary value to string.
11
+
12
+ ## 1.5.1
13
+
14
+ * Fix wrong cache lookup.
15
+
16
+ ## 1.5.0
17
+
18
+ * Add dictionary cache feature.
19
+ * Change module inclusion. Use 'include EnumIsh::Base' instead of 'extend EnumIsh'.
20
+ * Move configuration method to `EnumIsh`. Use `EnumIsh.configure` instead of 'EnumIsh::Config.configure'.
21
+
3
22
  ## 1.4.1
4
23
 
5
24
  * Avoid overwrite of superclass's `_enum_ish_enums`.
data/README.md CHANGED
@@ -26,7 +26,7 @@ Extend your class using EnumIsh and define an enum-like field:
26
26
 
27
27
  ```ruby
28
28
  class User
29
- extend EnumIsh
29
+ include EnumIsh::Base
30
30
  attr_accessor :status # status is a string field
31
31
  enum_ish :status, ['enable', 'disable'] # status has 'enable' or 'disable'
32
32
  end
@@ -82,7 +82,7 @@ Set default value:
82
82
 
83
83
  ```ruby
84
84
  class User
85
- extend EnumIsh
85
+ include EnumIsh::Base
86
86
  attr_accessor :status
87
87
  enum_ish :status, ['enable', 'disable'], default: 'enable'
88
88
  end
@@ -95,7 +95,7 @@ Use default value with block:
95
95
 
96
96
  ```ruby
97
97
  class User
98
- extend EnumIsh
98
+ include EnumIsh::Base
99
99
  attr_accessor :status, :flag
100
100
  enum_ish :status, ['enable', 'disable'], default: -> { flag ? 'enable' : 'disable' }
101
101
  end
@@ -110,7 +110,7 @@ Generate predicate methods:
110
110
 
111
111
  ```ruby
112
112
  class User
113
- extend EnumIsh
113
+ include EnumIsh::Base
114
114
  attr_accessor :status
115
115
  enum_ish :status, ['enable', 'disable'], predicate: true
116
116
  end
@@ -125,7 +125,7 @@ Without prefix:
125
125
 
126
126
  ```ruby
127
127
  class User
128
- extend EnumIsh
128
+ include EnumIsh::Base
129
129
  attr_accessor :status
130
130
  enum_ish :status, ['enable', 'disable'], predicate: { prefix: false }
131
131
  end
@@ -142,7 +142,7 @@ Generate getter and setter for aliased symbols instead of raw values:
142
142
 
143
143
  ```ruby
144
144
  class User
145
- extend EnumIsh
145
+ include EnumIsh::Base
146
146
  attr_accessor :status
147
147
  enum_ish :status, { _enable: 'enable', _disable: 'disable' }, accessor: true
148
148
  end
@@ -161,7 +161,7 @@ Generate accessor:
161
161
 
162
162
  ```ruby
163
163
  class User < ActiveRecord::Base
164
- extend EnumIsh
164
+ include EnumIsh::Base
165
165
  enum_ish :status, { _enable: 'enable', _disable: 'disable' }, accessor: true
166
166
  end
167
167
 
@@ -174,7 +174,7 @@ Generate scope:
174
174
 
175
175
  ```ruby
176
176
  class User < ActiveRecord::Base
177
- extend EnumIsh
177
+ include EnumIsh::Base
178
178
  enum_ish :status, ['enable', 'disable'], scope: true
179
179
  end
180
180
 
@@ -188,7 +188,7 @@ Generate validation:
188
188
 
189
189
  ```ruby
190
190
  class User < ActiveRecord::Base
191
- extend EnumIsh
191
+ include EnumIsh::Base
192
192
  enum_ish :status, ['enable', 'disable'], validate: true
193
193
  end
194
194
 
@@ -197,6 +197,17 @@ user.status = 'INVALID'
197
197
  user.valid? #=> false
198
198
  ```
199
199
 
200
+ ### Dictionary cache
201
+
202
+ You can enable dictionary cache using rack middleware:
203
+
204
+ ```ruby
205
+ Rails.application.config.middleware.use EnumIsh::DictionaryCache
206
+ ```
207
+
208
+ This middleware enables dictionary cache for each request.
209
+ Performance of dictionary lookup will be improved in case one dictionary is used repeatedly in the same request.
210
+
200
211
  ## Contributing
201
212
 
202
213
  Bug reports and pull requests are welcome at https://github.com/kanety/enum_ish.
@@ -1,5 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem "rails", "~> 6.0.0"
4
+ gem "psych", "~> 3.3.0"
4
5
 
5
6
  gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 7.0.1"
4
+
5
+ gemspec path: "../"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'active_record_enum_type'
2
4
 
3
5
  module EnumIsh
@@ -9,7 +11,7 @@ module EnumIsh
9
11
  after_initialize method
10
12
  define_method method do
11
13
  if respond_to?(enum.name) && public_send(enum.name).nil?
12
- default = enum.setting[:default]
14
+ default = enum.default
13
15
  default = instance_exec(&default) if default.kind_of?(Proc)
14
16
  public_send("#{enum.name}=", default)
15
17
  end
@@ -19,29 +21,33 @@ module EnumIsh
19
21
 
20
22
  def define_accessor(enum)
21
23
  @klass.class_eval do
22
- define_method "#{Config.raw_prefix}#{enum.name}#{Config.raw_suffix}" do
24
+ define_method enum.raw_method do
23
25
  value = read_attribute(enum.name)
24
26
  enum.mapping.fetch(value, value)
25
27
  end
26
28
 
27
- args =
28
- if ActiveRecord.version > Gem::Version.new('6.1.0.a')
29
- [enum.name]
30
- else
31
- [enum.name, :enum]
29
+ if ActiveRecord.version > Gem::Version.new('7.0.0.a')
30
+ attribute(enum.name) do |subtype|
31
+ EnumIsh::ActiveRecordEnumType.new(enum.name, enum.mapping, subtype)
32
+ end
33
+ elsif ActiveRecord.version > Gem::Version.new('6.1.0.a')
34
+ decorate_attribute_type(enum.name) do |subtype|
35
+ EnumIsh::ActiveRecordEnumType.new(enum.name, enum.mapping, subtype)
36
+ end
37
+ else
38
+ decorate_attribute_type(enum.name, :enum) do |subtype|
39
+ EnumIsh::ActiveRecordEnumType.new(enum.name, enum.mapping, subtype)
32
40
  end
33
- decorate_attribute_type(*args) do |subtype|
34
- EnumIsh::ActiveRecordEnumType.new(enum.name, enum.mapping, subtype)
35
41
  end
36
42
  end
37
43
  end
38
44
 
39
45
  def define_scope(enum)
40
46
  @klass.class_eval do
41
- scope "#{Config.scope_prefix}#{enum.name}#{Config.scope_suffix}", ->(*value) {
47
+ scope enum.scope_method, ->(*value) {
42
48
  where(enum.name => enum.mapping.fetch(value, value))
43
49
  }
44
- scope "#{Config.scope_prefix}#{enum.name}_not#{Config.scope_suffix}", ->(*value) {
50
+ scope enum.scope_method(:negative), ->(*value) {
45
51
  where.not(enum.name => enum.mapping.fetch(value, value))
46
52
  }
47
53
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EnumIsh
2
4
  class ActiveRecordEnumType < ActiveRecord::Type::Value
3
5
  def initialize(name, mapping, subtype)
@@ -0,0 +1,29 @@
1
+ require_relative 'enum'
2
+ require_relative 'dictionary'
3
+ require_relative 'errors'
4
+ require_relative 'definer'
5
+ require_relative 'active_record_definer' if defined?(ActiveRecord::Base)
6
+
7
+ module EnumIsh
8
+ module Base
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ class_attribute :_enum_ish_enums
13
+ self._enum_ish_enums = {}
14
+ end
15
+
16
+ class_methods do
17
+ def enum_ish(name, map, setting = {})
18
+ enum = Enum.new(name, map, Config.defaults.merge(setting))
19
+ self._enum_ish_enums = _enum_ish_enums.merge(name.to_sym => enum)
20
+
21
+ if defined?(ActiveRecord::Base) && ancestors.include?(ActiveRecord::Base)
22
+ ActiveRecordDefiner.new(self).define(enum)
23
+ else
24
+ Definer.new(self).define(enum)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,6 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EnumIsh
2
4
  class Config
3
- @@options = {
5
+ class_attribute :data
6
+
7
+ self.data = {
8
+ defaults: { text: true, options: true },
4
9
  text_prefix: '',
5
10
  text_suffix: '_text',
6
11
  options_prefix: '',
@@ -11,19 +16,13 @@ module EnumIsh
11
16
  scope_suffix: ''
12
17
  }
13
18
 
14
- @@options.keys.each do |key|
19
+ data.keys.each do |key|
15
20
  define_singleton_method "#{key}" do
16
- @@options[key]
21
+ data[key]
17
22
  end
18
23
 
19
24
  define_singleton_method "#{key}=" do |val|
20
- @@options[key] = val
21
- end
22
- end
23
-
24
- class << self
25
- def configure
26
- yield self
25
+ data[key] = val
27
26
  end
28
27
  end
29
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EnumIsh
2
4
  class Definer
3
5
  def initialize(klass)
@@ -5,8 +7,8 @@ module EnumIsh
5
7
  end
6
8
 
7
9
  def define(enum)
8
- [:text, :options, :predicate, :default, :accessor, :validate, :scope].each do |type|
9
- send("define_#{type}", enum) if enum.setting[type]
10
+ enum.features.each do |feature|
11
+ send("define_#{feature}", enum)
10
12
  end
11
13
  end
12
14
 
@@ -14,7 +16,7 @@ module EnumIsh
14
16
 
15
17
  def define_text(enum)
16
18
  @klass.class_eval do
17
- define_method "#{Config.text_prefix}#{enum.name}#{Config.text_suffix}" do |options = {}|
19
+ define_method enum.text_method do |options = {}|
18
20
  dict = Dictionary.new(self.class, enum, options)
19
21
  dict.translate_value(public_send(enum.name))
20
22
  end
@@ -23,7 +25,7 @@ module EnumIsh
23
25
 
24
26
  def define_options(enum)
25
27
  @klass.class_eval do
26
- define_singleton_method "#{Config.options_prefix}#{enum.name}#{Config.options_suffix}" do |options = {}|
28
+ define_singleton_method enum.options_method do |options = {}|
27
29
  dict = Dictionary.new(self, enum, options)
28
30
  dict.translate_options
29
31
  end
@@ -31,24 +33,14 @@ module EnumIsh
31
33
  end
32
34
 
33
35
  def define_predicate(enum)
34
- enum.mapping.each do |enum_key, enum_value|
35
- method_name = if enum.setting[:predicate].is_a?(Hash) && enum.setting[:predicate][:prefix] == false
36
- "#{enum_key}?"
37
- else
38
- "#{enum.name}_#{enum_key}?"
39
- end
40
- target_value = if enum.setting[:accessor]
41
- enum_key
42
- else
43
- enum_value
44
- end
36
+ enum.mapping.keys.each do |key|
45
37
  @klass.class_eval do
46
- define_method method_name.tr('.', '_') do
38
+ define_method enum.predicate_method(key) do
47
39
  value = public_send(enum.name)
48
40
  if value.is_a?(Array)
49
- value == [target_value]
41
+ value == [enum.value(key)]
50
42
  else
51
- value == target_value
43
+ value == enum.value(key)
52
44
  end
53
45
  end
54
46
  end
@@ -61,7 +53,7 @@ module EnumIsh
61
53
  define_method :initialize do |*args|
62
54
  super(*args)
63
55
  if respond_to?(enum.name) && public_send(enum.name).nil?
64
- default = enum.setting[:default]
56
+ default = enum.default
65
57
  default = instance_exec(&default) if default.kind_of?(Proc)
66
58
  public_send("#{enum.name}=", default)
67
59
  end
@@ -72,7 +64,7 @@ module EnumIsh
72
64
 
73
65
  def define_accessor(enum)
74
66
  @klass.class_eval do
75
- define_method "#{Config.raw_prefix}#{enum.name}#{Config.raw_suffix}" do
67
+ define_method enum.raw_method do
76
68
  instance_variable_get("@#{enum.name}")
77
69
  end
78
70
  define_method "#{enum.name}" do
@@ -85,13 +77,8 @@ module EnumIsh
85
77
  end
86
78
 
87
79
  def define_validate(enum)
88
- targets = if enum.setting[:accessor]
89
- enum.mapping.keys
90
- else
91
- enum.mapping.values
92
- end
93
80
  @klass.class_eval do
94
- validates enum.name, inclusion: { in: targets }, allow_nil: true
81
+ validates enum.name, inclusion: { in: enum.values }, allow_nil: true
95
82
  end
96
83
  end
97
84
 
@@ -1,10 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'dictionary_lookup'
4
+
1
5
  module EnumIsh
2
6
  class Dictionary
3
7
  def initialize(klass, enum, options = {})
4
8
  @klass = klass
5
9
  @enum = enum
6
10
  @options = options
7
- @dict = load_dict
11
+ @dict = cache { DictionaryLookup.new(@klass, @enum, @options).call }
8
12
  end
9
13
 
10
14
  def translate_value(value)
@@ -16,57 +20,20 @@ module EnumIsh
16
20
  end
17
21
 
18
22
  def translate_options
19
- @dict.to_a.map { |value, label| [label, value] }
23
+ @dict.to_a.map { |value, text| [text, value] }
20
24
  end
21
25
 
22
26
  private
23
27
 
24
- def load_dict
25
- i18n = load_from_i18n.transform_keys { |k| k.to_s.to_sym }
26
-
27
- dict = {}
28
- if @enum.setting[:accessor]
29
- @enum.mapping.each { |k, v| dict[k] = i18n[k].to_s }
28
+ def cache
29
+ if (cache = EnumIsh::DictionaryCache.cache) != nil
30
+ cache[I18n.locale] ||= {}
31
+ cache[I18n.locale][@klass] ||= {}
32
+ cache[I18n.locale][@klass][@enum] ||= {}
33
+ cache[I18n.locale][@klass][@enum][@options] ||= yield
30
34
  else
31
- @enum.mapping.each { |k, v| dict[v] = i18n[k].to_s }
32
- end
33
-
34
- filter(dict)
35
- end
36
-
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
50
-
51
- @enum.mapping.invert
52
- end
53
-
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? }
58
- end
59
-
60
- def filter(dict)
61
- if @options[:except]
62
- except = Array(@options[:except])
63
- dict.reject! { |k, v| except.include?(k) }
64
- end
65
- if @options[:only]
66
- only = Array(@options[:only])
67
- dict.select! { |k, v| only.include?(k) }
35
+ yield
68
36
  end
69
- dict
70
37
  end
71
38
  end
72
39
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnumIsh
4
+ class DictionaryCache
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ EnumIsh::DictionaryCache.enable do
11
+ @app.call(env)
12
+ end
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
30
+ end
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
@@ -1,5 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EnumIsh
2
4
  class Enum
5
+ class_attribute :features
6
+ self.features = [:text, :options, :predicate, :default, :accessor, :validate, :scope]
7
+
3
8
  attr_accessor :name
4
9
  attr_accessor :mapping
5
10
  attr_accessor :setting
@@ -7,7 +12,37 @@ module EnumIsh
7
12
  def initialize(name, mapping, setting)
8
13
  @name = name
9
14
  @mapping = init_mapping(mapping)
10
- @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
11
46
  end
12
47
 
13
48
  def to_raw(value)
@@ -27,6 +62,35 @@ module EnumIsh
27
62
  end
28
63
  end
29
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
+
30
94
  private
31
95
 
32
96
  def init_mapping(mapping)
@@ -40,12 +104,5 @@ module EnumIsh
40
104
  mapping
41
105
  end
42
106
  end
43
-
44
- def init_setting(setting)
45
- [:text, :options].each do |key|
46
- setting[key] = true unless setting.key?(key)
47
- end
48
- setting
49
- end
50
107
  end
51
108
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EnumIsh
2
4
  class Error < StandardError
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EnumIsh
2
- VERSION = '1.4.1'
4
+ VERSION = '1.6.1'
3
5
  end
data/lib/enum_ish.rb CHANGED
@@ -1,36 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support'
2
4
  require 'enum_ish/version'
3
- require 'enum_ish/errors'
4
5
  require 'enum_ish/config'
5
- require 'enum_ish/enum'
6
- require 'enum_ish/dictionary'
7
- require 'enum_ish/definer'
8
- require 'enum_ish/active_record_definer' if defined?(ActiveRecord::Base)
6
+ require 'enum_ish/base'
7
+ require 'enum_ish/dictionary_cache'
9
8
 
10
9
  module EnumIsh
11
- def enum_ish(name, map, config = {})
12
- enum = Enum.new(name, map, config)
13
-
14
- self._enum_ish_enums ||= {}
15
- self._enum_ish_enums[name.to_sym] = enum
16
-
17
- if defined?(ActiveRecord::Base) && self.ancestors.include?(ActiveRecord::Base)
18
- ActiveRecordDefiner.new(self).define(enum)
19
- else
20
- Definer.new(self).define(enum)
10
+ class << self
11
+ def configure
12
+ yield Config
21
13
  end
22
- end
23
-
24
- def self.extended(klass)
25
- super
26
- klass.class_attribute :_enum_ish_enums
27
- klass.extend ClassMethods
28
- end
29
14
 
30
- module ClassMethods
31
- def inherited(subclass)
32
- super
33
- subclass._enum_ish_enums = self._enum_ish_enums.dup
15
+ def config
16
+ Config
34
17
  end
35
18
  end
36
19
  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.1
4
+ version: 1.6.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-25 00:00:00.000000000 Z
11
+ date: 2022-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -115,12 +115,16 @@ files:
115
115
  - gemfiles/rails52.gemfile
116
116
  - gemfiles/rails60.gemfile
117
117
  - gemfiles/rails61.gemfile
118
+ - gemfiles/rails70.gemfile
118
119
  - lib/enum_ish.rb
119
120
  - lib/enum_ish/active_record_definer.rb
120
121
  - lib/enum_ish/active_record_enum_type.rb
122
+ - lib/enum_ish/base.rb
121
123
  - lib/enum_ish/config.rb
122
124
  - lib/enum_ish/definer.rb
123
125
  - lib/enum_ish/dictionary.rb
126
+ - lib/enum_ish/dictionary_cache.rb
127
+ - lib/enum_ish/dictionary_lookup.rb
124
128
  - lib/enum_ish/enum.rb
125
129
  - lib/enum_ish/errors.rb
126
130
  - lib/enum_ish/version.rb