enum_ish 1.2.0 → 1.2.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: c81de716639aaef492be612f2f9d412889513785e61163a2d0e409dad03f883e
4
- data.tar.gz: 8163a1339902881f9300cf582b852ca8e505c0cbe27b031b0e9e680cef5a1547
3
+ metadata.gz: 52aa581f25b17fc1890f71cba2669c5cd12c1507f7282ab8f1c1dbb2c1680d7e
4
+ data.tar.gz: 7993d724365d6a906874c8cf0786ab25a3b551b1ec9790971585d37b71a90845
5
5
  SHA512:
6
- metadata.gz: 689437f57eb521fe7b5458fde6ea43d2bfa86fba8b7cca2873c18e7d5ece8cbc5da72ea300ed8ac6ed373c113bc1e3e1ff48e1ecb8abc04e0712e7b7e4b28a88
7
- data.tar.gz: 8269c8f1268008af6bedb3bec2eff776254430d440d9c71bd68243d1661f137392bfbfbe74d1ec5aaef78ce293637a080a3c713e2bcaaafcb4d0eff72887e213
6
+ metadata.gz: a6d1ac67ef5a55663ddd0c784225b3c3c0f4f5322ad71d066ed2260c305997ae1a384254ace06dea5d0bd076440d315733520ac4afc070e3c2bd19f7debc1f86
7
+ data.tar.gz: da632eb455b2915e5c7116544fd3c94033c2eabf6b866c938bb2c869090f026c27d8f564964fe0ae2b279203a69f0419b6083ca0b026e40da3d4fcc3ad5fc19a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.2.1
4
+
5
+ * Fix internal module name.
6
+
3
7
  ## 1.2.0
4
8
 
5
9
  * Register enum definitions in the class.
@@ -0,0 +1,41 @@
1
+ require_relative 'active_record_enum_type'
2
+
3
+ module EnumIsh
4
+ class ActiveRecordDefiner < EnumIsh::Definer
5
+ def define_default(enum)
6
+ method = "_enum_ish_init_#{enum.name}".to_sym
7
+
8
+ @klass.class_eval do
9
+ after_initialize method
10
+ define_method method do
11
+ if respond_to?(enum.name) && public_send(enum.name).nil?
12
+ default = enum.setting[:default]
13
+ default = instance_exec(&default) if default.kind_of?(Proc)
14
+ public_send("#{enum.name}=", default)
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ def define_accessor(enum)
21
+ @klass.class_eval do
22
+ define_method "#{Config.raw_prefix}#{enum.name}#{Config.raw_suffix}" do
23
+ value = read_attribute(enum.name)
24
+ enum.mapping.fetch(value, value)
25
+ end
26
+
27
+ decorate_attribute_type(enum.name, :enum) do |subtype|
28
+ EnumIsh::ActiveRecordEnumType.new(enum.name, enum.mapping, subtype)
29
+ end
30
+ end
31
+ end
32
+
33
+ def define_scope(enum)
34
+ @klass.class_eval do
35
+ scope "#{Config.scope_prefix}#{enum.name}#{Config.scope_suffix}", ->(value) {
36
+ where(enum.name => enum.mapping.fetch(value, value))
37
+ }
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ module EnumIsh
2
+ class ActiveRecordEnumType < ActiveRecord::Type::Value
3
+ def initialize(name, mapping, subtype)
4
+ @name = name
5
+ @mapping = mapping
6
+ @subtype = subtype
7
+ end
8
+
9
+ def cast(value)
10
+ if @mapping.has_key?(value)
11
+ value
12
+ elsif @mapping.has_value?(value)
13
+ @mapping.key(value)
14
+ else
15
+ value
16
+ end
17
+ end
18
+
19
+ def deserialize(value)
20
+ @mapping.key(@subtype.deserialize(value))
21
+ end
22
+
23
+ def serialize(value)
24
+ @mapping.fetch(value, value)
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module EnumIsh
2
- VERSION = '1.2.0'
2
+ VERSION = '1.2.1'
3
3
  end
data/lib/enum_ish.rb CHANGED
@@ -5,7 +5,7 @@ require 'enum_ish/config'
5
5
  require 'enum_ish/enum'
6
6
  require 'enum_ish/dictionary'
7
7
  require 'enum_ish/definer'
8
- require 'enum_ish/active_record/definer' if defined?(::ActiveRecord::Base)
8
+ require 'enum_ish/active_record_definer' if defined?(ActiveRecord::Base)
9
9
 
10
10
  module EnumIsh
11
11
  def enum_ish(name, map, config = {})
@@ -15,8 +15,8 @@ module EnumIsh
15
15
  self._enum_ish_enums ||= []
16
16
  self._enum_ish_enums << enum
17
17
 
18
- if defined?(::ActiveRecord::Base) && self.ancestors.include?(::ActiveRecord::Base)
19
- ActiveRecord::Definer.new(self).define(enum)
18
+ if defined?(ActiveRecord::Base) && self.ancestors.include?(ActiveRecord::Base)
19
+ ActiveRecordDefiner.new(self).define(enum)
20
20
  else
21
21
  Definer.new(self).define(enum)
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_ish
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshikazu Kaneta
@@ -115,8 +115,8 @@ files:
115
115
  - gemfiles/rails52.gemfile
116
116
  - gemfiles/rails60.gemfile
117
117
  - lib/enum_ish.rb
118
- - lib/enum_ish/active_record/definer.rb
119
- - lib/enum_ish/active_record/enum_type.rb
118
+ - lib/enum_ish/active_record_definer.rb
119
+ - lib/enum_ish/active_record_enum_type.rb
120
120
  - lib/enum_ish/config.rb
121
121
  - lib/enum_ish/definer.rb
122
122
  - lib/enum_ish/dictionary.rb
@@ -1,43 +0,0 @@
1
- require_relative 'enum_type'
2
-
3
- module EnumIsh
4
- module ActiveRecord
5
- class Definer < EnumIsh::Definer
6
- def define_default(enum)
7
- method = "_enum_ish_init_#{enum.name}".to_sym
8
-
9
- @klass.class_eval do
10
- after_initialize method
11
- define_method method do
12
- if respond_to?(enum.name) && public_send(enum.name).nil?
13
- default = enum.setting[:default]
14
- default = instance_exec(&default) if default.kind_of?(Proc)
15
- public_send("#{enum.name}=", default)
16
- end
17
- end
18
- end
19
- end
20
-
21
- def define_accessor(enum)
22
- @klass.class_eval do
23
- define_method "#{Config.raw_prefix}#{enum.name}#{Config.raw_suffix}" do
24
- value = read_attribute(enum.name)
25
- enum.mapping.fetch(value, value)
26
- end
27
-
28
- decorate_attribute_type(enum.name, :enum) do |subtype|
29
- EnumIsh::ActiveRecord::EnumType.new(enum.name, enum.mapping, subtype)
30
- end
31
- end
32
- end
33
-
34
- def define_scope(enum)
35
- @klass.class_eval do
36
- scope "#{Config.scope_prefix}#{enum.name}#{Config.scope_suffix}", ->(value) {
37
- where(enum.name => enum.mapping.fetch(value, value))
38
- }
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,29 +0,0 @@
1
- module EnumIsh
2
- module ActiveRecord
3
- class EnumType < ::ActiveRecord::Type::Value
4
- def initialize(name, mapping, subtype)
5
- @name = name
6
- @mapping = mapping
7
- @subtype = subtype
8
- end
9
-
10
- def cast(value)
11
- if @mapping.has_key?(value)
12
- value
13
- elsif @mapping.has_value?(value)
14
- @mapping.key(value)
15
- else
16
- value
17
- end
18
- end
19
-
20
- def deserialize(value)
21
- @mapping.key(@subtype.deserialize(value))
22
- end
23
-
24
- def serialize(value)
25
- @mapping.fetch(value, value)
26
- end
27
- end
28
- end
29
- end