discriminable 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/Rakefile +1 -3
- data/lib/discriminable/version.rb +1 -1
- data/lib/discriminable.rb +12 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 332984f03395d4725f162027692324d66fa591f19820d7690201677c37ab8d0c
|
4
|
+
data.tar.gz: 49017319a8f0cc0ff6bbf498ec32fc68fc51cf062821d683e58b1340b317e7f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23b33e6a0ebe2009ec62ef57747fbb585cd400837476a3199f28ff8733f607f3974b667713fd788457824adaa9a09ee93e0093e071010016d389b2eab2ec415a
|
7
|
+
data.tar.gz: fabeb7320aa926567261756a3d2c9863feb193abfd4124da42a47588d84ab3b6a4736bd197d850aa74fbd8c6841c7077bbeb41020549f0e962ca54f0d132e494
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
### [2.2.1](https://github.com/gregorw/discriminable/compare/v2.2.0...v2.2.1) (2022-04-17)
|
4
|
+
|
5
|
+
|
6
|
+
### Bug Fixes
|
7
|
+
|
8
|
+
* Simplify STI using OCP ([68f49c2](https://github.com/gregorw/discriminable/commit/68f49c2d0b7a9437525fe0bc9ebef1927fccb2e8))
|
9
|
+
|
3
10
|
## [2.2.0](https://github.com/gregorw/discriminable/compare/v2.1.1...v2.2.0) (2022-04-16)
|
4
11
|
|
5
12
|
|
data/Rakefile
CHANGED
data/lib/discriminable.rb
CHANGED
@@ -27,7 +27,6 @@ module Discriminable
|
|
27
27
|
class_attribute :discriminable_map, instance_writer: false
|
28
28
|
class_attribute :discriminable_inverse_map, instance_writer: false
|
29
29
|
class_attribute :discriminable_values, instance_writer: false
|
30
|
-
class_attribute :discriminable_as_descendant_value, instance_writer: false
|
31
30
|
end
|
32
31
|
|
33
32
|
# Specify the column to use for discrimination.
|
@@ -46,12 +45,19 @@ module Discriminable
|
|
46
45
|
self.inheritance_column = attribute.to_s
|
47
46
|
end
|
48
47
|
|
48
|
+
# rubocop:disable Metrics/AbcSize
|
49
49
|
def discriminable_by(attribute)
|
50
50
|
raise "Subclasses should not override .discriminable_by" unless base_class?
|
51
51
|
|
52
|
-
self.discriminable_as_descendant_value = true
|
53
52
|
self.inheritance_column = attribute.to_s
|
53
|
+
self.discriminable_map ||= Hash.new do |map, value|
|
54
|
+
map[value] = discriminable_descendants(value)&.name
|
55
|
+
end
|
56
|
+
self.discriminable_inverse_map ||= Hash.new do |map, value|
|
57
|
+
map[value] = value.constantize.discriminable_values&.first
|
58
|
+
end
|
54
59
|
end
|
60
|
+
# rubocop:enable Metrics/AbcSize
|
55
61
|
|
56
62
|
def discriminable_as(*values)
|
57
63
|
raise "Only subclasses should specify .discriminable_as" if base_class?
|
@@ -62,22 +68,10 @@ module Discriminable
|
|
62
68
|
end
|
63
69
|
|
64
70
|
def sti_name
|
65
|
-
if discriminable_as_descendant_value
|
66
|
-
self.discriminable_inverse_map ||= Hash.new do |map, value|
|
67
|
-
map[value] = name.constantize.discriminable_values&.first
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
71
|
discriminable_inverse_map[name]
|
72
72
|
end
|
73
73
|
|
74
74
|
def sti_class_for(value)
|
75
|
-
if discriminable_as_descendant_value
|
76
|
-
self.discriminable_map ||= Hash.new do |map, v|
|
77
|
-
map[v] = descendants.detect { |d| d.discriminable_values.include? v }&.name
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
75
|
return self unless (type_name = discriminable_map[value])
|
82
76
|
|
83
77
|
super type_name
|
@@ -94,5 +88,9 @@ module Discriminable
|
|
94
88
|
value = base_class.type_for_attribute(inheritance_column).cast(value)
|
95
89
|
sti_class_for(value)
|
96
90
|
end
|
91
|
+
|
92
|
+
def discriminable_descendants(value)
|
93
|
+
descendants.detect { |d| d.discriminable_values.include? value }
|
94
|
+
end
|
97
95
|
end
|
98
96
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: discriminable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gregor Wassmann
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-04-
|
11
|
+
date: 2022-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|