discriminable 2.2.0 → 2.2.3
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 +21 -0
- data/README.md +1 -1
- data/Rakefile +1 -3
- data/TODO.md +5 -1
- data/lib/discriminable/version.rb +1 -1
- data/lib/discriminable.rb +31 -18
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77f7e596256a4d211a4437b0a52d9e0f40d03a038e48c5ce6332083f7f910da9
|
4
|
+
data.tar.gz: cdaf98658eea3b2faf0bd6193c38fd83d010141c08d7c8cff643f331b51c0986
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eabc2a7dd9bbbce30010f777ffc782e9f875f4c9b5c67daa2d762c224a4493b6f48d74a52b43696de29f0ef2de57128c52e2e83612362e78f896645594e5ef95
|
7
|
+
data.tar.gz: da4b9c0a03acca87af217b97d996077d8d60e79fe43bf1dc0baaacde5c465b3911ce0259422382c0c9ec0a3aaeea70adaf738aa62d36f1401c0763d3f6f4fa44
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
### [2.2.3](https://github.com/gregorw/discriminable/compare/v2.2.2...v2.2.3) (2022-04-20)
|
4
|
+
|
5
|
+
|
6
|
+
### Bug Fixes
|
7
|
+
|
8
|
+
* Allow to use attribute aliases ([448e4e8](https://github.com/gregorw/discriminable/commit/448e4e81925543f1ee4949898f0ac6628eb2d3fd))
|
9
|
+
|
10
|
+
### [2.2.2](https://github.com/gregorw/discriminable/compare/v2.2.1...v2.2.2) (2022-04-17)
|
11
|
+
|
12
|
+
|
13
|
+
### Bug Fixes
|
14
|
+
|
15
|
+
* Typo in gemspec uris ([f77253f](https://github.com/gregorw/discriminable/commit/f77253f2cb2f400e04baf3c7bd6083f42ae3aa6b))
|
16
|
+
|
17
|
+
### [2.2.1](https://github.com/gregorw/discriminable/compare/v2.2.0...v2.2.1) (2022-04-17)
|
18
|
+
|
19
|
+
|
20
|
+
### Bug Fixes
|
21
|
+
|
22
|
+
* Simplify STI using OCP ([68f49c2](https://github.com/gregorw/discriminable/commit/68f49c2d0b7a9437525fe0bc9ebef1927fccb2e8))
|
23
|
+
|
3
24
|
## [2.2.0](https://github.com/gregorw/discriminable/compare/v2.1.1...v2.2.0) (2022-04-16)
|
4
25
|
|
5
26
|
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
# Discriminable
|
4
4
|
|
5
5
|
[](https://rubygems.org/gems/discriminable)
|
6
|
-
[](https://github.com/gregorw/discriminable/actions/workflows/main.yml)
|
6
|
+
[](https://github.com/gregorw/discriminable/actions/workflows/main.yml)
|
7
7
|
[](https://codeclimate.com/github/gregorw/discriminable/maintainability)
|
8
8
|
[](https://codeclimate.com/github/gregorw/discriminable/test_coverage)
|
9
9
|
|
data/Rakefile
CHANGED
data/TODO.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
- [x] multiple values per subclass
|
2
2
|
- [x] default to first value when using hash syntax
|
3
3
|
- [x] open-closed principle
|
4
|
-
- [ ]
|
4
|
+
- [ ] Bug: multiple values: Child.all query
|
5
|
+
- [ ] more tests / examples
|
6
|
+
- [ ] Rails 5 support
|
5
7
|
- [ ] test permitted attributes
|
6
8
|
- [ ] `self.abstract_class = true`
|
9
|
+
- [ ] scoping…
|
10
|
+
- [ ] ~~use `type` column with enum, int, string, etc.~~
|
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.
|
@@ -37,20 +36,27 @@ module Discriminable
|
|
37
36
|
|
38
37
|
attribute, map = options.first
|
39
38
|
|
39
|
+
# E.g. { value: "ClassName" }
|
40
40
|
self.discriminable_map = map.with_indifferent_access
|
41
41
|
|
42
42
|
# Use first key as default discriminator
|
43
43
|
# { a: "C", b: "C" }.invert => { "C" => :b }
|
44
44
|
# { a: "C", b: "C" }.to_a.reverse.to_h.invert => { "C" => :a }
|
45
|
+
# E.g. { "ClassName" => :value }
|
45
46
|
self.discriminable_inverse_map = map.to_a.reverse.to_h.invert
|
46
|
-
|
47
|
+
|
48
|
+
attribute = attribute.to_s
|
49
|
+
self.inheritance_column = attribute_aliases[attribute] || attribute
|
47
50
|
end
|
48
51
|
|
49
52
|
def discriminable_by(attribute)
|
50
53
|
raise "Subclasses should not override .discriminable_by" unless base_class?
|
51
54
|
|
52
|
-
self.
|
53
|
-
self.
|
55
|
+
self.discriminable_map ||= discriminable_map_memoized
|
56
|
+
self.discriminable_inverse_map ||= discriminable_inverse_map_memoized
|
57
|
+
|
58
|
+
attribute = attribute.to_s
|
59
|
+
self.inheritance_column = attribute_aliases[attribute] || attribute
|
54
60
|
end
|
55
61
|
|
56
62
|
def discriminable_as(*values)
|
@@ -61,23 +67,12 @@ module Discriminable
|
|
61
67
|
end
|
62
68
|
end
|
63
69
|
|
70
|
+
# This is the value of the discriminable attribute
|
64
71
|
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
72
|
discriminable_inverse_map[name]
|
72
73
|
end
|
73
74
|
|
74
75
|
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
76
|
return self unless (type_name = discriminable_map[value])
|
82
77
|
|
83
78
|
super type_name
|
@@ -90,9 +85,27 @@ module Discriminable
|
|
90
85
|
attrs = attrs.to_h if attrs.respond_to?(:permitted?)
|
91
86
|
return unless attrs.is_a?(Hash)
|
92
87
|
|
93
|
-
value = attrs
|
94
|
-
value = base_class.type_for_attribute(inheritance_column).cast(value)
|
88
|
+
value = discriminable_value(attrs)
|
95
89
|
sti_class_for(value)
|
96
90
|
end
|
91
|
+
|
92
|
+
def discriminable_map_memoized
|
93
|
+
Hash.new do |map, value|
|
94
|
+
map[value] = descendants.detect { |d| d.discriminable_values.include? value }&.name
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def discriminable_inverse_map_memoized
|
99
|
+
Hash.new do |map, value|
|
100
|
+
map[value] = value.constantize.discriminable_values&.first
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def discriminable_value(attrs)
|
105
|
+
attrs = attrs.with_indifferent_access
|
106
|
+
value = attrs[inheritance_column]
|
107
|
+
value ||= attrs[attribute_aliases.invert[inheritance_column]]
|
108
|
+
base_class.type_for_attribute(inheritance_column).cast(value)
|
109
|
+
end
|
97
110
|
end
|
98
111
|
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.3
|
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-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -131,13 +131,13 @@ files:
|
|
131
131
|
- lib/discriminable.rb
|
132
132
|
- lib/discriminable/version.rb
|
133
133
|
- sig/discriminable.rbs
|
134
|
-
homepage: https://github.com/gregorw/
|
134
|
+
homepage: https://github.com/gregorw/discriminable
|
135
135
|
licenses:
|
136
136
|
- MIT
|
137
137
|
metadata:
|
138
|
-
homepage_uri: https://github.com/gregorw/
|
139
|
-
source_code_uri: https://github.com/gregorw/
|
140
|
-
changelog_uri: https://github.com/gregorw/
|
138
|
+
homepage_uri: https://github.com/gregorw/discriminable
|
139
|
+
source_code_uri: https://github.com/gregorw/discriminable
|
140
|
+
changelog_uri: https://github.com/gregorw/discriminable/CHANGELOG.md
|
141
141
|
rubygems_mfa_required: 'false'
|
142
142
|
post_install_message:
|
143
143
|
rdoc_options: []
|