rails_type_id 0.2.0 → 0.3.0

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: c0781fbf1b175f2edfe7b66fc82d03d09f08762d084cfc4e0681831d39d6f90b
4
- data.tar.gz: 162be97b603d6b6480dcf27461bcf19541f43b30a5c65fcab5b1b46de1c04bd4
3
+ metadata.gz: 0c9dca219a90d4889fbfe7e3efcfc0654d6b25dfb6b92c3ce26a7c9e0e66db51
4
+ data.tar.gz: bff2e4c5d830371922b696c14675739f985bfe37f1add64a9f8fa28552940ad2
5
5
  SHA512:
6
- metadata.gz: 7a2d68e71437f6c62f2c183aaf749a4fddfba9ea61c6708f66c433dfa708ebfd2218ea2bfc74d05b1349cf2f65bbe1cc55b6434688cb5a6d2cb4c7defe823b94
7
- data.tar.gz: bbd6f63fd939e57f0dbb46280e249f1d2ed8a929aeb9f1541e49c732bd742b7f81f624a4bf6ed51ca399fe733064a2d58e1a8fbc4778796e1f89915fdeb4c59f
6
+ metadata.gz: a376381db8db1c2a571f4f43248e6da57867890410dee87af7e5bb72ea8caa84b5484a72f3fc51c7e8acd64fe1991d948e3b35c5cab51328e4277a2cf2d82389
7
+ data.tar.gz: 2f5ab23d07f0c1e1914bf391673519e29f684f6da2894802e039452ac5c7286965407e52a4609d2d4ee0d5e1b5fc58695afe2fe7bad710511c2d2293df19e309
data/README.md CHANGED
@@ -59,7 +59,7 @@ class MigrateUserToUUID < ActiveRecord::Migration[8.0]
59
59
  add_column :sessions, :user_uuid, :text, null: true
60
60
 
61
61
  Users.find_each do |u|
62
- u.update(uuid: SecureRandom.uuid_v7)
62
+ u.update(uuid: RailsTypeId::Concern::Helpers.generate_type_id("user"))
63
63
  end
64
64
  Session.find_each do |s|
65
65
  s.update(user_uuid: s.user.uuid)
@@ -13,8 +13,6 @@ module RailsTypeId
13
13
  extend ::ActiveSupport::Concern
14
14
  include ::ActiveModel::Validations::Callbacks
15
15
 
16
- class InvalidTypeIdPrefix < StandardError; end
17
-
18
16
  # Checks validity of the ActiveRecord model instances
19
17
  class Validator < ActiveModel::Validator
20
18
  def validate(record)
@@ -56,57 +54,7 @@ module RailsTypeId
56
54
  # @return [void]
57
55
  define_method :generate_type_id do
58
56
  Helpers.validate_type_id_prefix!(self.class.type_id_prefix)
59
- self.id ||= TypeID.from_uuid(self.class.type_id_prefix, SecureRandom.uuid_v7).to_s
60
- end
61
- end
62
-
63
- # Internal helper methods
64
- class Helpers
65
- extend T::Sig
66
-
67
- class << self
68
- def validate_type_id_prefix(prefix)
69
- return "type_id_prefix cannot be nil" if prefix.nil?
70
-
71
- return nil if prefix.match(/\A[a-z]{1,10}\z/)
72
-
73
- "type_id_prefix must be lowercase alphabetic (a-z) with length >= 1, <= 10"
74
- end
75
-
76
- def validate_type_id_prefix!(prefix)
77
- result = validate_type_id_prefix(prefix)
78
- raise InvalidTypeIdPrefix, result if result.present?
79
- end
80
-
81
- def lookup_type_id(type_id)
82
- klasses = lookup_model(type_id)
83
- return if klasses.nil?
84
-
85
- klasses.each do |klass|
86
- result = klass.find_by(id: type_id)
87
- return result unless result.nil?
88
- end
89
-
90
- nil
91
- end
92
-
93
- def lookup_model(type_id)
94
- prefix = TypeID.from_string(type_id).prefix
95
- return if prefix.nil?
96
-
97
- prefix_map[prefix]
98
- end
99
-
100
- private
101
-
102
- def prefix_map
103
- Rails.application.eager_load!
104
- # This has to be group_by and not index_by because we can have multiple
105
- # models that use the same prefix (like Settings)
106
- @prefix_map ||= ActiveRecord::Base.descendants
107
- .select { |klass| klass.respond_to?(:type_id_prefix) }
108
- .group_by(&:type_id_prefix)
109
- end
57
+ self.id ||= Helpers.generate_type_id(self.class.type_id_prefix).to_s
110
58
  end
111
59
  end
112
60
  end
@@ -0,0 +1,71 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "sorbet-runtime"
5
+
6
+ module RailsTypeId
7
+ class InvalidTypeIdPrefix < StandardError; end
8
+
9
+ # Helper methods for interacting with type IDs
10
+ class Helpers
11
+ class << self
12
+ extend T::Sig
13
+
14
+ sig { params(prefix: String).returns(TypeID) }
15
+ def generate_type_id(prefix)
16
+ TypeID.from_uuid(prefix, SecureRandom.uuid_v7)
17
+ end
18
+
19
+ sig { params(prefix: T.nilable(String)).returns(T.nilable(String)) }
20
+ def validate_type_id_prefix(prefix)
21
+ return "type_id_prefix cannot be nil" if prefix.nil?
22
+
23
+ return nil if prefix.match(/\A[a-z]{1,10}\z/)
24
+
25
+ "type_id_prefix must be lowercase alphabetic (a-z) with length >= 1, <= 10"
26
+ end
27
+
28
+ sig { params(prefix: T.nilable(String)).void }
29
+ def validate_type_id_prefix!(prefix)
30
+ result = validate_type_id_prefix(prefix)
31
+ raise InvalidTypeIdPrefix, result if result.present?
32
+ end
33
+
34
+ sig { params(type_id: String).returns(T.nilable(ActiveRecord::Base)) }
35
+ def lookup_type_id(type_id)
36
+ klasses = lookup_model(type_id)
37
+ return if klasses.nil?
38
+
39
+ klasses.each do |klass|
40
+ result = klass.find_by(id: type_id)
41
+ return result unless result.nil?
42
+ end
43
+
44
+ nil
45
+ end
46
+
47
+ sig { params(type_id: String).returns(T.nilable(T::Array[T.untyped])) }
48
+ def lookup_model(type_id)
49
+ prefix = TypeID.from_string(type_id).prefix
50
+ return if prefix.nil?
51
+
52
+ prefix_map[prefix]
53
+ end
54
+
55
+ private
56
+
57
+ sig { returns(T::Hash[String, T::Array[T.untyped]]) }
58
+ def prefix_map
59
+ Rails.application.eager_load!
60
+ # This has to be group_by and not index_by because we can have multiple
61
+ # models that use the same prefix (like Settings)
62
+ @prefix_map ||= T.let(
63
+ ActiveRecord::Base.descendants
64
+ .select { |klass| klass.respond_to?(:type_id_prefix) }
65
+ .group_by(&:type_id_prefix),
66
+ T.nilable(T::Hash[String, T::Array[T.untyped]])
67
+ )
68
+ end
69
+ end
70
+ end
71
+ end
@@ -2,5 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative "version"
5
+ require_relative "helpers"
5
6
  require_relative "concern"
6
7
  require_relative "test_helper"
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module RailsTypeId
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_type_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franklin Hu
@@ -66,6 +66,7 @@ files:
66
66
  - Rakefile
67
67
  - lib/rails_type_id.rb
68
68
  - lib/rails_type_id/concern.rb
69
+ - lib/rails_type_id/helpers.rb
69
70
  - lib/rails_type_id/require.rb
70
71
  - lib/rails_type_id/test_helper.rb
71
72
  - lib/rails_type_id/version.rb