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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63543cd5b47a63a6870368d2d16c9570e7cd0cebb53cfd296d717884c37fb4c3
4
- data.tar.gz: b4417f8e33ff2d24a71a9ec436a26b66c38cab59395ed7fbb1182c818ff48439
3
+ metadata.gz: 77f7e596256a4d211a4437b0a52d9e0f40d03a038e48c5ce6332083f7f910da9
4
+ data.tar.gz: cdaf98658eea3b2faf0bd6193c38fd83d010141c08d7c8cff643f331b51c0986
5
5
  SHA512:
6
- metadata.gz: e053310353bf54fcfbef35b32b5de0015f9b28903cb6dc04631bc04cfbaeec274359c0dc34d687eca7a9813eee36590f2c745e90d3c9587e381fceb9bb38886d
7
- data.tar.gz: dbf905a1c5a7c7c10154ad5cc9eb73b67d81cd65513e5ac7a1ab33086dbfaee92a885b511942ecc37db4b8528a75283d270ec807db9c4982defb7ba7f2c814a8
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
  [![Gem Version](https://badge.fury.io/rb/discriminable.svg)](https://rubygems.org/gems/discriminable)
6
- [![CI](https://github.com/gregorw/discriminable/actions/workflows/main.yml/badge.svg)](https://github.com/gregorw/discriminable/actions/workflows/main.yml)
6
+ [![CI](https://github.com/gregorw/discriminable/actions/workflows/main.yml/badge.svg?event=push)](https://github.com/gregorw/discriminable/actions/workflows/main.yml)
7
7
  [![Maintainability](https://api.codeclimate.com/v1/badges/94041c5f946b64040368/maintainability)](https://codeclimate.com/github/gregorw/discriminable/maintainability)
8
8
  [![Test Coverage](https://api.codeclimate.com/v1/badges/94041c5f946b64040368/test_coverage)](https://codeclimate.com/github/gregorw/discriminable/test_coverage)
9
9
 
data/Rakefile CHANGED
@@ -16,6 +16,4 @@ RuboCop::RakeTask.new
16
16
  task default: %i[test rubocop]
17
17
 
18
18
  # Release task
19
- # gem build *.gemspec
20
- # gem push *.gem
21
- # rm *.gem
19
+ # gem build *.gemspec && gem push *.gem && rm *.gem
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
- - [ ] use `type` column with enum, int, string, etc.
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.~~
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Discriminable
4
- VERSION = "2.2.0"
4
+ VERSION = "2.2.3"
5
5
  end
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
- self.inheritance_column = attribute.to_s
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.discriminable_as_descendant_value = true
53
- self.inheritance_column = attribute.to_s
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.with_indifferent_access[inheritance_column]
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.0
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-16 00:00:00.000000000 Z
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/discrimainable
134
+ homepage: https://github.com/gregorw/discriminable
135
135
  licenses:
136
136
  - MIT
137
137
  metadata:
138
- homepage_uri: https://github.com/gregorw/discrimainable
139
- source_code_uri: https://github.com/gregorw/discrimainable
140
- changelog_uri: https://github.com/gregorw/discrimainable/CHANGELOG.md
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: []