typed_uuid 4.0 → 4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb375ca568f121a2a996b9afd41123e18b820621e4417b2bce05024364eef414
4
- data.tar.gz: 15d885d11a1dbcb19ca60733312ad0b21bcf77a0f9ce165ce8b25fa24f43cecf
3
+ metadata.gz: 9dd0a928d842b001570b023cace562666b6f391623fd27ca1c6b88cebd58ed3a
4
+ data.tar.gz: '09ea1aee57794d976b79c8bd221e1894519994603f8112bd4ffb55966b3082c6'
5
5
  SHA512:
6
- metadata.gz: 38c295658ce41fac5b51c27fdb4df1922ef426e71c2bb234e7af0e0b87abdb11e8110c3e4604e84909f30aa4a3fd2782b740b443aae9ee926fc7ad567a31a8f6
7
- data.tar.gz: 42139374aa96e9a43b5f0f7d749a410617fa14c1affd0fa334e1fe59661189fb58d911b26056aa9e5cc6092709db6ab62541c88f788be54cfe946f45bca5868c
6
+ metadata.gz: 5123b6d3291005d87ae8ed4fd1fefe9b55f10d83cde474367d9e136ff625a80a7bae221584a92c297f3a32e2b33e5a0c81bb3124b7acacb4bd82a28df94e4e82
7
+ data.tar.gz: 7e96e3a5376fdcd87ba3a0c89e0e1511707000d280aa17c52d618d6f7c150eb3ff33bf23102c6dfdbfe04b6b91dccf94f984d94d66e38b3830c89c23516ce805
data/README.md CHANGED
@@ -70,7 +70,7 @@ ActiveRecord::Base.register_uuid_types({
70
70
  ActiveRecord::Base.register_uuid_types({
71
71
  0 => :Listing,
72
72
  512 => :Building,
73
- 8_191 => 'Building::SkyScrpaer'
73
+ 8_191 => 'Building::SkyScrpaer'
74
74
  })
75
75
  ```
76
76
 
@@ -83,7 +83,7 @@ a table.
83
83
  ```ruby
84
84
  class CreateProperties < ActiveRecord::Migration[5.2]
85
85
  def change
86
- create_table :properties, id: :typed_uuid do |t|
86
+ create_table :properties, id: :typed_uuid do |t|
87
87
  t.string "name", limit: 255
88
88
  end
89
89
  end
@@ -116,6 +116,12 @@ class UpdateProperties < ActiveRecord::Migration[6.1]
116
116
  end
117
117
  ```
118
118
 
119
+ When you need to lookup they class of a TypedUUID:
120
+
121
+ ```ruby
122
+ ActiveRecord::Base.class_from_uuid(my_uuid) #=> MyClass
123
+ ```
124
+
119
125
  ## STI Models
120
126
  When using STI Model Rails will generate the UUID to be inserted. This UUID will
121
127
  be calculated of the STI Model class and not the base class.
@@ -1,3 +1,3 @@
1
1
  module TypedUUID
2
- VERSION = '4.0'
2
+ VERSION = '4.1'
3
3
  end
data/lib/typed_uuid.rb CHANGED
@@ -35,7 +35,22 @@ module TypedUUID
35
35
  timestamp ||= Time.now
36
36
 
37
37
  uuid = [timestamp.to_i * 1_000_000 + timestamp.usec].pack('Q>')[1..-1]
38
- uuid << (sequence&.pack('Q>') || SecureRandom.random_bytes(10))
38
+ uuid << if sequence.nil?
39
+ SecureRandom.random_bytes(8)
40
+ elsif sequence.is_a?(Integer)
41
+ sequence = [sequence].pack("Q>")
42
+ if sequence.bytesize == 8 && sequence[0..1] == "\x00\x00"
43
+ sequence[2..]
44
+ else
45
+ raise ArgumentError, 'Sequence is more than 6 bytes'
46
+ end
47
+ elsif sequence.is_a?(String)
48
+ raise ArgumentError, 'Sequence is more than 6 bytes' if sequence.bytesize > 6
49
+ sequence.b
50
+ else
51
+ raise ArgumentError, 'Unable to convert sequence to binary'
52
+ end
53
+ uuid << SecureRandom.random_bytes(4)
39
54
 
40
55
  uuid = uuid.unpack("nnnnnnnn")
41
56
  uuid[7] = (uuid[2] ^ uuid[6]) ^ ((enum << 3) | 1)
@@ -63,6 +78,15 @@ module TypedUUID
63
78
  Time.at(*uuid[0..13].to_i(16).divmod(1_000_000), :usec)
64
79
  end
65
80
 
81
+ def sequence_b(uuid)
82
+ uuid = uuid.gsub('-', '')
83
+ uuid[14..25].scan(/.{4}/).map{|i| i.to_i(16) }.pack('n*').b
84
+ end
85
+
86
+ def sequence(uuid)
87
+ ("\x00\x00" + sequence_b(uuid)).unpack1('Q>')
88
+ end
89
+
66
90
  end
67
91
  end
68
92
 
@@ -102,6 +102,42 @@ class UUIDTest < ActiveSupport::TestCase
102
102
 
103
103
  assert_equal Listing, ::ActiveRecord::Base.class_from_uuid(Listing.typed_uuid)
104
104
  end
105
+
106
+ test 'typed_uuid v1 with a timestamp' do
107
+ time = Time.at(1606612254, 370979, :usec)
108
+ uuid = TypedUUID.uuid(592, 1, timestamp: time)
109
+
110
+ assert_equal 1, TypedUUID.version(uuid)
111
+ assert_equal 592, TypedUUID.enum(uuid)
112
+
113
+ assert_equal time, TypedUUID.timestamp(uuid)
114
+ assert_equal time.to_i, TypedUUID.timestamp(uuid).to_i
115
+ assert_equal time.nsec, TypedUUID.timestamp(uuid).nsec
116
+
117
+ assert_equal Listing, ::ActiveRecord::Base.class_from_uuid(Listing.typed_uuid)
118
+ end
119
+
120
+ test 'typed_uuid v1 with sequence as a string' do
121
+ uuid = TypedUUID.uuid(592, 1, sequence: '123456')
122
+
123
+ assert_equal 1, TypedUUID.version(uuid)
124
+ assert_equal 592, TypedUUID.enum(uuid)
125
+ assert_equal 54091677185334, TypedUUID.sequence(uuid)
126
+ assert_equal '123456', TypedUUID.sequence_b(uuid)
127
+
128
+ assert_equal Listing, ::ActiveRecord::Base.class_from_uuid(Listing.typed_uuid)
129
+ end
130
+
131
+ test 'typed_uuid v1 with sequence as a integer' do
132
+ uuid = TypedUUID.uuid(592, 1, sequence: 123456)
133
+
134
+ assert_equal 1, TypedUUID.version(uuid)
135
+ assert_equal 592, TypedUUID.enum(uuid)
136
+ assert_equal 123456, TypedUUID.sequence(uuid)
137
+ assert_equal "\x00\x00\x00\x01\xE2@".b, TypedUUID.sequence_b(uuid)
138
+
139
+ assert_equal Listing, ::ActiveRecord::Base.class_from_uuid(Listing.typed_uuid)
140
+ end
105
141
 
106
142
  test 'class_from uuid generated bye PostgresQL' do
107
143
  listing = Listing.create
data/typed_uuid.gemspec CHANGED
@@ -23,9 +23,9 @@ Gem::Specification.new do |s|
23
23
  s.add_development_dependency 'mocha'
24
24
  s.add_development_dependency 'byebug'
25
25
  s.add_development_dependency 'activesupport', '>= 6.0.0'
26
- s.add_development_dependency 'rails', '>= 6.0.0'
26
+ s.add_development_dependency 'rails', '>= 6.1.0'
27
27
  s.add_development_dependency 'pg'
28
28
 
29
29
  # Runtime
30
- s.add_runtime_dependency 'activerecord', '>= 6.0.0'
30
+ s.add_runtime_dependency 'activerecord', '>= 6.1.0'
31
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_uuid
3
3
  version: !ruby/object:Gem::Version
4
- version: '4.0'
4
+ version: '4.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-02 00:00:00.000000000 Z
11
+ date: 2021-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -128,14 +128,14 @@ dependencies:
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: 6.0.0
131
+ version: 6.1.0
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: 6.0.0
138
+ version: 6.1.0
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: pg
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -156,14 +156,14 @@ dependencies:
156
156
  requirements:
157
157
  - - ">="
158
158
  - !ruby/object:Gem::Version
159
- version: 6.0.0
159
+ version: 6.1.0
160
160
  type: :runtime
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
- version: 6.0.0
166
+ version: 6.1.0
167
167
  description: Typed UUIDs 2 bytes are reserved in the UUID for the class enum
168
168
  email:
169
169
  - jonbracy@gmail.com