uuid_attribute 0.9.11 → 0.9.12

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: 72d04de25e1451d8d20da7c3c9bdc6615761cfb2f900fe01c03e487c6f1bac12
4
- data.tar.gz: e9f76cc58730887fbc69e7a680b0ac6453e6ed09869c0d76707a0d005dcae24d
3
+ metadata.gz: 1685c722a23743951d2632a893fd5829db06e9f1d2fdecc324e84c3eb5f31a9c
4
+ data.tar.gz: b2ddf6083cf44a1c011845d08065fad900b6ad993677e98e75fcb18c894e48fa
5
5
  SHA512:
6
- metadata.gz: fc4b31bb6e0ee8306ea6a8413e3f815c5a2ddb87964339183c944f921b7ba605c8f266672efea8210c2851d84198f18b3c5b1db94617745d4023ddf7062cf9a8
7
- data.tar.gz: b3e18587ba0e177a66629dbe6a794f33497d33b2eba80701ecf4f668fa91860286a0d19523012fccfb38009dccc3a0b6f2738e733a9a385259957904df3524be
6
+ metadata.gz: addce17ecc827d604fbf2eafe349f1e4ddab3528a2405bc08b8ed028c744a585633ed67daa374c1c8917c28b13619961e38ce125d0aa688e673ede674e11e5b5
7
+ data.tar.gz: 2987018970032d0709a07c2bb791ff65deb90406672e5caa12233299f9b09b8c8a215cc38207f6da5469cd545152fd8ba29d746b0925cc7b66a6a1d386ded358
@@ -1,6 +1,99 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rails/railtie"
4
+ require "active_record/associations/association_scope"
5
+ require "active_record/fixture_set/table_row"
6
+
7
+ module ActiveRecord
8
+ module Associations
9
+ class AssociationScope # :nodoc:
10
+ def last_chain_scope(scope, reflection, owner)
11
+ primary_key = reflection.join_primary_key
12
+ foreign_key = reflection.join_foreign_key
13
+
14
+ table = reflection.aliased_table
15
+
16
+ prevalue = owner[foreign_key]
17
+ if owner.class.attribute_types[foreign_key].class.name == "UuidAttribute::Type"
18
+ prevalue = UuidAttribute::Utils.raw_bytes(UuidAttribute::Utils.normalize(UuidAttribute::Utils.parse(prevalue)))
19
+ end
20
+
21
+ value = transform_value(prevalue)
22
+ scope = apply_scope(scope, table, primary_key, value)
23
+
24
+ if reflection.type
25
+ polymorphic_type = transform_value(owner.class.polymorphic_name)
26
+ scope = apply_scope(scope, table, reflection.type, polymorphic_type)
27
+ end
28
+
29
+ scope
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ module ActiveStorage
36
+ class FixtureSet
37
+ def self.blob(filename:, **attributes)
38
+ generated_uuid = Digest::UUID.uuid_v5(Digest::UUID::OID_NAMESPACE, filename)
39
+ raw_uuid = UuidAttribute::Utils.raw_bytes(
40
+ UuidAttribute::Utils.normalize(UuidAttribute::Utils.parse(generated_uuid))
41
+ )
42
+ new.prepare Blob.new(filename: filename, id: raw_uuid, key: generated_uuid), **attributes
43
+ end
44
+ end
45
+ end
46
+
47
+ module ActiveRecord
48
+ class FixtureSet
49
+ class << self
50
+ def identify(label, column_type = :integer)
51
+ if column_type == :uuid
52
+ generated_uuid = Digest::UUID.uuid_v5(Digest::UUID::OID_NAMESPACE, label.to_s)
53
+ UuidAttribute::Utils.raw_bytes(UuidAttribute::Utils.normalize(UuidAttribute::Utils.parse(generated_uuid)))
54
+ else
55
+ Zlib.crc32(label.to_s) % MAX_ID
56
+ end
57
+ end
58
+ end
59
+
60
+ class TableRow
61
+ private
62
+
63
+ alias org_fill_row_model_attributes fill_row_model_attributes
64
+ def fill_row_model_attributes
65
+ org_fill_row_model_attributes
66
+ return unless model_class
67
+
68
+ resolve_string_uuids
69
+ end
70
+
71
+ def resolve_string_uuids
72
+ @row.each do |key, value|
73
+ next if model_class.to_s.include?("ActiveStorage") && key == "record_id"
74
+
75
+ transform_uuid(key, value)
76
+ end
77
+ end
78
+
79
+ def transform_uuid(key, value)
80
+ if test_uuid36?(key, value)
81
+ @row[key] = [value&.gsub("-", "")&.upcase].pack("H*")
82
+ elsif test_uuid22?(key, value)
83
+ @row[key] = [UuidAttribute::Utils.unshort(value)&.gsub("-", "")&.upcase].pack("H*")
84
+ end
85
+ end
86
+
87
+ def test_uuid36?(key, value)
88
+ (key == "id" || key.ends_with?("_id")) && value.instance_of?(String) && value.length == 36
89
+ end
90
+
91
+ def test_uuid22?(key, value)
92
+ (key == "id" || key.ends_with?("_id")) && value.instance_of?(String) && value.length == 22
93
+ end
94
+ end
95
+ end
96
+ end
4
97
 
5
98
  module UuidAttribute
6
99
  # Rails Initializer
@@ -57,7 +150,7 @@ module UuidAttribute
57
150
 
58
151
  default = nil
59
152
  default = -> { SecureRandom.uuid } if att.eql? "id"
60
- model.attribute att, ::UuidAttribute::UUID.new, default: default
153
+ model.attribute att, ::UuidAttribute::Type.new, default: default
61
154
  end
62
155
  end
63
156
  rescue ActiveRecord::NoDatabaseError
@@ -0,0 +1,43 @@
1
+
2
+ module UuidAttribute
3
+ # UUID Attribute
4
+ class Type < ActiveModel::Type::Binary
5
+ def type
6
+ :uuid
7
+ end
8
+
9
+ def serialize(value)
10
+ #puts "SERIALIZE"
11
+ return if value.blank?
12
+ return value if value.is_a?(ActiveRecord::Type::Binary::Data)
13
+
14
+ binary_data = value
15
+ binary_data = Utils.raw_bytes(Utils.normalize(Utils.parse(value))) if value.is_a?(String)
16
+ # binary_data = value.raw if value.is_a?(UUID)
17
+
18
+ ActiveRecord::Type::Binary::Data.new(
19
+ binary_data
20
+ )
21
+ end
22
+
23
+ def deserialize(value)
24
+ # puts "DESERIALIZE"
25
+ return nil if value.nil?
26
+ value = value.to_s if value.is_a?(ActiveModel::Type::Binary::Data)
27
+
28
+ # return UUID.new(Utils.normalize(Utils.parse(value)))
29
+ return Utils.shorten(Utils.normalize(Utils.parse(value)))
30
+ # ActiveRecord::Type::Binary::Data.new(Utils.raw_bytes(Utils.normalize(Utils.parse(value))))
31
+ end
32
+
33
+ def cast(value)
34
+ # puts "CAST"
35
+ return nil if value.nil?
36
+ # return value if value.is_a?(UUID)
37
+
38
+ value = value.to_s if value.is_a?(ActiveModel::Type::Binary::Data)
39
+ return Utils.shorten(Utils.normalize(Utils.parse(value)))
40
+ # super
41
+ end
42
+ end
43
+ end
@@ -17,6 +17,7 @@ module UuidAttribute
17
17
  end
18
18
 
19
19
  def parse(str)
20
+ return format_uuid(str.hex) if str.is_a?(UUID)
20
21
  return nil if str.length.zero?
21
22
  return str if str.length == 36
22
23
 
@@ -1,28 +1,22 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  module UuidAttribute
4
3
  # UUID Attribute
5
- class UUID < ActiveModel::Type::Binary
6
- def type
7
- :uuid
4
+ class UUID
5
+ def initialize(value)
6
+ @hex = Utils.normalize(Utils.parse(value))
7
+ @shorten = Utils.shorten(@hex)
8
8
  end
9
9
 
10
- def serialize(value)
11
- return if value.blank?
12
-
13
- ActiveRecord::Type::Binary::Data.new(
14
- Utils.raw_bytes(Utils.normalize(Utils.parse(value)))
15
- )
10
+ def to_s
11
+ @shorten
16
12
  end
17
13
 
18
- def deserialize(value)
19
- return nil if value.nil?
20
-
21
- Utils.shorten(Utils.parse(value.to_s))
14
+ def hex
15
+ @hex
22
16
  end
23
17
 
24
- def cast(value)
25
- deserialize(value)
18
+ def raw
19
+ UuidAttribute::Utils.raw_bytes(@hex)
26
20
  end
27
21
  end
28
22
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UuidAttribute
4
- VERSION = "0.9.11"
4
+ VERSION = "0.9.12"
5
5
  end
@@ -4,6 +4,7 @@ require "active_model"
4
4
  require "active_model/type"
5
5
  require "rails"
6
6
  require_relative "uuid_attribute/version"
7
+ require_relative "uuid_attribute/type"
7
8
  require_relative "uuid_attribute/uuid"
8
9
  require_relative "uuid_attribute/utils"
9
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uuid_attribute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.11
4
+ version: 0.9.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Negri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-12 00:00:00.000000000 Z
11
+ date: 2023-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -50,6 +50,7 @@ files:
50
50
  - README.md
51
51
  - lib/uuid_attribute.rb
52
52
  - lib/uuid_attribute/railtie.rb
53
+ - lib/uuid_attribute/type.rb
53
54
  - lib/uuid_attribute/utils.rb
54
55
  - lib/uuid_attribute/uuid.rb
55
56
  - lib/uuid_attribute/version.rb