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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63543cd5b47a63a6870368d2d16c9570e7cd0cebb53cfd296d717884c37fb4c3
4
- data.tar.gz: b4417f8e33ff2d24a71a9ec436a26b66c38cab59395ed7fbb1182c818ff48439
3
+ metadata.gz: 332984f03395d4725f162027692324d66fa591f19820d7690201677c37ab8d0c
4
+ data.tar.gz: 49017319a8f0cc0ff6bbf498ec32fc68fc51cf062821d683e58b1340b317e7f3
5
5
  SHA512:
6
- metadata.gz: e053310353bf54fcfbef35b32b5de0015f9b28903cb6dc04631bc04cfbaeec274359c0dc34d687eca7a9813eee36590f2c745e90d3c9587e381fceb9bb38886d
7
- data.tar.gz: dbf905a1c5a7c7c10154ad5cc9eb73b67d81cd65513e5ac7a1ab33086dbfaee92a885b511942ecc37db4b8528a75283d270ec807db9c4982defb7ba7f2c814a8
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
@@ -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
@@ -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.1"
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.
@@ -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.0
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-16 00:00:00.000000000 Z
11
+ date: 2022-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord