sti_type_customizable 0.4.0 → 0.5.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
- SHA1:
3
- metadata.gz: 35e3700e16d39906db475a0ef2adaf33f4fc3f7e
4
- data.tar.gz: f3303e2f8751c15c05a6244ebe05da77f0b63ba3
2
+ SHA256:
3
+ metadata.gz: 7dcc03b41655bdb470806fe3cda60da630378b01d8f3b27505a6d870799bba09
4
+ data.tar.gz: 04f067e28c901941bb27330be3e0b5ee8f00296118e015bd82da61647dfdc722
5
5
  SHA512:
6
- metadata.gz: 69b91dc39393b64629d644450956ee7a6bb019054aa9b02b6446db2fbf86e73f802df5fec81cc97dceba283402526fc5149280b45c89434f04ab43d736580a7e
7
- data.tar.gz: 67e5d2fc2ebc65d80448a9ad86614d695475dc08df30478decec479f9cf3f2121972f866b89277806469844958553b461b026d7bdf28cf7f1de3456798bf22a2
6
+ metadata.gz: 7122771fdfe9cda41926b851415859bf8b2998773302491a04193d3eff78b1ddd4f88ccd8070404b022b534e44acd5474feacc660c5dcd8583e1cea67e5cd7c9
7
+ data.tar.gz: b64e57752fdacd5619843da4cb2ba6b37032c0620b7393a00d8f8c3c1ef81ed90481c281b69a72922d95b2fc7102db79c681df98870bd9da6d879298f4e7cda4
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ vendor
data/README.md CHANGED
@@ -36,26 +36,32 @@ class ErrorMessage < Message
36
36
  end
37
37
  ```
38
38
 
39
-
40
39
  - type to integer sample
41
40
 
42
41
  ```ruby
43
- # type: 0
44
42
  class Message < ActiveRecord::Base
45
43
  include StiTypeCustomizable
46
- self.sti_type_value = 0
47
- sti_child_classes [WarningMessage, ErrorMessage]
44
+
45
+ self.sti_resolver = Resolver.new
48
46
  end
49
47
 
50
- # type: 1
51
48
  class WarningMessage < Message
52
- self.sti_type_value = 1
53
49
  end
54
50
 
55
- # type: 2
56
51
  class ErrorMessage < Message
57
- self.sti_type_value = 2
58
52
  end
53
+
54
+ class Resolver
55
+ include StiTypeCustomizable::Resolver
56
+
57
+ def definitions
58
+ @definitions ||= {
59
+ warning_message: 1,
60
+ error_message: 2
61
+ }
62
+ end
63
+ end
64
+
59
65
  ```
60
66
 
61
67
  ## Development
@@ -1,34 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
  require "sti_type_customizable/version"
3
- require "active_support/concern"
3
+ require "sti_type_customizable/definition"
4
+
5
+ require "active_support"
6
+ require "active_support/core_ext"
4
7
 
5
8
  module StiTypeCustomizable
6
9
  extend ActiveSupport::Concern
7
10
 
8
11
  included do
9
- class_attribute :sti_type_value
10
- class_attribute :sti_entities
12
+ cattr_accessor :_sti_definition, instance_accessor: false
11
13
  end
12
14
 
13
15
  class_methods do
14
- def sti_child_classes(classes)
15
- self::sti_entities ||= begin
16
- {}.tap do |entities|
17
- classes.each do |klass|
18
- raise NotImplementedError unless klass.include? StiTypeCustomizable
19
-
20
- entities[klass.sti_type_value] = klass
21
- end
22
- end
23
- end
16
+ def sti_definition(definition)
17
+ self._sti_definition = definition
24
18
  end
25
19
 
26
20
  def find_sti_class(type)
27
- self::sti_entities[type] || self
21
+ self._sti_definition.resolve_class(type) || self
28
22
  end
29
23
 
30
24
  def sti_name
31
- self::sti_entities.invert[self] || self::sti_type_value
25
+ self._sti_definition.resolve_type_value(self)
32
26
  end
33
27
  end
34
28
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StiTypeCustomizable
4
+ module Definition
5
+ def initialize(default_type_value)
6
+ @definitions = load_definitions
7
+ @inverted_definitions = @definitions.invert
8
+ @module_name = self.class.name.deconstantize
9
+ @default_type_value = default_type_value
10
+ end
11
+
12
+ def resolve_class(type_value)
13
+ key = @inverted_definitions.dig(type_value)
14
+ return nil if key.blank?
15
+
16
+ "#{@module_name}::#{key.to_s.classify}".safe_constantize
17
+ end
18
+
19
+ def resolve_type_value(klass)
20
+ @definitions.dig(klass.name.demodulize.underscore.to_sym) || @default_type_value
21
+ end
22
+
23
+ private
24
+
25
+ def load_definitions
26
+ raise NotImplementedError
27
+ end
28
+ end
29
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module StiTypeCustomizable
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency "rails", ">= 4.2"
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.12"
24
+ spec.add_development_dependency "bundler", "~> 2.0"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sti_type_customizable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Ooishi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-18 00:00:00.000000000 Z
11
+ date: 2019-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.12'
33
+ version: '2.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.12'
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -83,6 +83,7 @@ files:
83
83
  - bin/console
84
84
  - bin/setup
85
85
  - lib/sti_type_customizable.rb
86
+ - lib/sti_type_customizable/definition.rb
86
87
  - lib/sti_type_customizable/version.rb
87
88
  - sti_type_customizable.gemspec
88
89
  homepage: https://github.com/sti_type_customizable
@@ -104,8 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
105
  - !ruby/object:Gem::Version
105
106
  version: '0'
106
107
  requirements: []
107
- rubyforge_project:
108
- rubygems_version: 2.5.2
108
+ rubygems_version: 3.0.3
109
109
  signing_key:
110
110
  specification_version: 4
111
111
  summary: Customize STI type column