discriminable 2.1.1 → 2.2.2
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 +22 -6
- data/Rakefile +3 -0
- data/TODO.md +5 -4
- data/lib/discriminable/version.rb +1 -1
- data/lib/discriminable.rb +29 -2
- 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: 4a89c1ad10593a4aa548c3792343092ef153d6a9a5abd0a65ac253005dcb1b69
|
4
|
+
data.tar.gz: 67db220cf69fa783068f8eb9b2d4852f4a44066926da4703ac41233ff62f88d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbdd8c37b95997ccf9daf4556a79d7248e9d20165ecf8f51022961ed3ae884cb3a16316ff7e8aa7669da53af38e6fba5f9e1a61fcbe13b370c536cdc5bd5fbb1
|
7
|
+
data.tar.gz: 94af960d7e56c3b913faf4d2c5ef721d0400c8e3235884b03cc77707f4015372c12cd108370de41016ffabb7e202217eab9cb14657d566b161b82567b307479f
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
### [2.2.2](https://github.com/gregorw/discriminable/compare/v2.2.1...v2.2.2) (2022-04-17)
|
4
|
+
|
5
|
+
|
6
|
+
### Bug Fixes
|
7
|
+
|
8
|
+
* Typo in gemspec uris ([f77253f](https://github.com/gregorw/discriminable/commit/f77253f2cb2f400e04baf3c7bd6083f42ae3aa6b))
|
9
|
+
|
10
|
+
### [2.2.1](https://github.com/gregorw/discriminable/compare/v2.2.0...v2.2.1) (2022-04-17)
|
11
|
+
|
12
|
+
|
13
|
+
### Bug Fixes
|
14
|
+
|
15
|
+
* Simplify STI using OCP ([68f49c2](https://github.com/gregorw/discriminable/commit/68f49c2d0b7a9437525fe0bc9ebef1927fccb2e8))
|
16
|
+
|
17
|
+
## [2.2.0](https://github.com/gregorw/discriminable/compare/v2.1.1...v2.2.0) (2022-04-16)
|
18
|
+
|
19
|
+
|
20
|
+
### Features
|
21
|
+
|
22
|
+
* Alternative syntax respecting open-closed principle ([8a8f885](https://github.com/gregorw/discriminable/commit/8a8f8857dc24753dc08f21b2bfbb7b4317b7ed5c))
|
23
|
+
|
3
24
|
### [2.1.1](https://github.com/gregorw/discriminable/compare/v2.1.0...v2.1.1) (2022-04-15)
|
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
|
|
@@ -29,20 +29,36 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
29
29
|
## Usage
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
ActiveRecord::
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
class Order < ActiveRecord::Base
|
33
|
+
include Discriminable
|
34
|
+
|
35
|
+
enum state: { open: 0, completed: 1 }
|
36
|
+
discriminable state: { open: "Cart" }
|
36
37
|
end
|
37
38
|
|
39
|
+
class Cart < Order
|
40
|
+
end
|
41
|
+
|
42
|
+
Cart.create
|
43
|
+
=> #<Cart id: 1, state: "open">
|
44
|
+
Order.all
|
45
|
+
=> #<ActiveRecord::Relation [#<Cart id: 1, state: "open">]>
|
46
|
+
```
|
47
|
+
|
48
|
+
### Alternative syntax
|
49
|
+
|
50
|
+
Instead of defining subclass names within the parent you may prefer to be open for extension but closed for modification (open-closed principle) using the alternative syntax.
|
51
|
+
|
52
|
+
```ruby
|
38
53
|
class Order < ActiveRecord::Base
|
39
54
|
include Discriminable
|
40
55
|
|
41
56
|
enum state: { open: 0, completed: 1 }
|
42
|
-
|
57
|
+
discriminable_by :state
|
43
58
|
end
|
44
59
|
|
45
60
|
class Cart < Order
|
61
|
+
discriminable_as :open
|
46
62
|
end
|
47
63
|
|
48
64
|
Cart.create
|
data/Rakefile
CHANGED
data/TODO.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
- [
|
2
|
-
- [
|
3
|
-
- [
|
1
|
+
- [x] multiple values per subclass
|
2
|
+
- [x] default to first value when using hash syntax
|
3
|
+
- [x] open-closed principle
|
4
|
+
- [ ] use `type` column with enum, int, string, etc.
|
4
5
|
- [ ] test permitted attributes
|
5
|
-
- [ ] `self.abstract_class = true`
|
6
|
+
- [ ] `self.abstract_class = true`
|
data/lib/discriminable.rb
CHANGED
@@ -26,6 +26,7 @@ module Discriminable
|
|
26
26
|
included do
|
27
27
|
class_attribute :discriminable_map, instance_writer: false
|
28
28
|
class_attribute :discriminable_inverse_map, instance_writer: false
|
29
|
+
class_attribute :discriminable_values, instance_writer: false
|
29
30
|
end
|
30
31
|
|
31
32
|
# Specify the column to use for discrimination.
|
@@ -33,7 +34,7 @@ module Discriminable
|
|
33
34
|
def discriminable(**options)
|
34
35
|
raise "Subclasses should not override .discriminable" unless base_class?
|
35
36
|
|
36
|
-
|
37
|
+
attribute, map = options.first
|
37
38
|
|
38
39
|
self.discriminable_map = map.with_indifferent_access
|
39
40
|
|
@@ -41,7 +42,29 @@ module Discriminable
|
|
41
42
|
# { a: "C", b: "C" }.invert => { "C" => :b }
|
42
43
|
# { a: "C", b: "C" }.to_a.reverse.to_h.invert => { "C" => :a }
|
43
44
|
self.discriminable_inverse_map = map.to_a.reverse.to_h.invert
|
44
|
-
self.inheritance_column =
|
45
|
+
self.inheritance_column = attribute.to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
# rubocop:disable Metrics/AbcSize
|
49
|
+
def discriminable_by(attribute)
|
50
|
+
raise "Subclasses should not override .discriminable_by" unless base_class?
|
51
|
+
|
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
|
59
|
+
end
|
60
|
+
# rubocop:enable Metrics/AbcSize
|
61
|
+
|
62
|
+
def discriminable_as(*values)
|
63
|
+
raise "Only subclasses should specify .discriminable_as" if base_class?
|
64
|
+
|
65
|
+
self.discriminable_values = values.map do |value|
|
66
|
+
value.instance_of?(Symbol) ? value.to_s : value
|
67
|
+
end
|
45
68
|
end
|
46
69
|
|
47
70
|
def sti_name
|
@@ -65,5 +88,9 @@ module Discriminable
|
|
65
88
|
value = base_class.type_for_attribute(inheritance_column).cast(value)
|
66
89
|
sti_class_for(value)
|
67
90
|
end
|
91
|
+
|
92
|
+
def discriminable_descendants(value)
|
93
|
+
descendants.detect { |d| d.discriminable_values.include? value }
|
94
|
+
end
|
68
95
|
end
|
69
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.
|
4
|
+
version: 2.2.2
|
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
|
@@ -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: []
|