gn0m30-uuid 2.2.1 → 2.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/gn0m30-uuid.gemspec +1 -1
- data/lib/uuid.rb +54 -16
- data/test/test-translate.rb +40 -0
- metadata +3 -2
data/gn0m30-uuid.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
spec = Gem::Specification.new do |spec|
|
2
2
|
spec.name = 'gn0m30-uuid'
|
3
|
-
spec.version = '2.2.
|
3
|
+
spec.version = '2.2.2'
|
4
4
|
spec.summary = "UUID generator with teenie format"
|
5
5
|
spec.description = <<-EOF
|
6
6
|
UUID generator for producing universally unique identifiers based on RFC 4122
|
data/lib/uuid.rb
CHANGED
@@ -13,7 +13,7 @@ require 'tmpdir'
|
|
13
13
|
require 'rubygems'
|
14
14
|
require 'macaddr'
|
15
15
|
require 'base62'
|
16
|
-
|
16
|
+
require 'scanf'
|
17
17
|
|
18
18
|
##
|
19
19
|
# = Generating UUIDs
|
@@ -186,6 +186,36 @@ class UUID
|
|
186
186
|
def self.validate_teenie(uuid)
|
187
187
|
return true if uuid =~ /\A[\da-fA-f]{24}\z/i
|
188
188
|
end
|
189
|
+
|
190
|
+
##
|
191
|
+
# translate one format uuid to another
|
192
|
+
def self.translate(uuid, source_format, target_format)
|
193
|
+
if source_format == :teenie
|
194
|
+
raise "invalid uuid passed in for translation" unless UUID.validate_teenie(uuid)
|
195
|
+
else
|
196
|
+
raise "invalid uuid passed in for translation" unless UUID.validate(uuid)
|
197
|
+
end
|
198
|
+
|
199
|
+
raise "invalid source format passed in for tranlation" unless FORMATS[source_format]
|
200
|
+
raise "invalid target format passed in for tranlation" unless FORMATS[target_format]
|
201
|
+
raise "it's a waste of time to translate one format to another" unless source_format != target_format
|
202
|
+
|
203
|
+
parts = Array.new
|
204
|
+
|
205
|
+
if source_format != :teenie
|
206
|
+
parts = uuid.scanf(FORMATS[source_format])
|
207
|
+
else
|
208
|
+
# this has to stay in synch with the :teenie format
|
209
|
+
parts[0] = uuid[0..5].base62_decode
|
210
|
+
parts[1] = uuid[6..8].base62_decode
|
211
|
+
parts[2] = uuid[9..11].base62_decode
|
212
|
+
parts[3] = uuid[12..14].base62_decode
|
213
|
+
parts[4] = uuid[15..23].base62_decode
|
214
|
+
end
|
215
|
+
|
216
|
+
@uuid ||= new
|
217
|
+
@uuid.generate_from_parts(target_format, parts[0], parts[1], parts[2], parts[3], parts[4])
|
218
|
+
end
|
189
219
|
|
190
220
|
##
|
191
221
|
# Create a new UUID generator. You really only need to do this once.
|
@@ -214,9 +244,6 @@ class UUID
|
|
214
244
|
# Generates a new UUID string using +format+. See FORMATS for a list of
|
215
245
|
# supported formats.
|
216
246
|
def generate(format = :default)
|
217
|
-
template = FORMATS[format]
|
218
|
-
|
219
|
-
raise ArgumentError, "invalid UUID format #{format.inspect}" unless template
|
220
247
|
|
221
248
|
# The clock must be monotonically increasing. The clock resolution is at
|
222
249
|
# best 100 ns (UUID spec), but practically may be lower (on my setup,
|
@@ -255,18 +282,7 @@ class UUID
|
|
255
282
|
part4 = @sequence & 0xFFFF
|
256
283
|
part5 = @mac & 0xFFFFFFFFFFFF
|
257
284
|
|
258
|
-
|
259
|
-
if format == :teenie
|
260
|
-
part1 = part1.base62_encode
|
261
|
-
part2 = part2.base62_encode
|
262
|
-
part3 = part3.base62_encode
|
263
|
-
part4 = part4.base62_encode
|
264
|
-
part5 = part5.base62_encode
|
265
|
-
|
266
|
-
(template % [part1, part2, part3, part4, part5]).gsub(' ', '0')
|
267
|
-
else
|
268
|
-
template % [part1, part2, part3, part4, part5]
|
269
|
-
end
|
285
|
+
generate_from_parts(format, part1, part2, part3, part4, part5)
|
270
286
|
end
|
271
287
|
|
272
288
|
##
|
@@ -300,6 +316,28 @@ class UUID
|
|
300
316
|
"MAC: #{mac} Sequence: #{@sequence}"
|
301
317
|
end
|
302
318
|
|
319
|
+
##
|
320
|
+
# this is used both for generation and translation
|
321
|
+
def generate_from_parts(format, part1, part2, part3, part4, part5)
|
322
|
+
|
323
|
+
template = FORMATS[format]
|
324
|
+
|
325
|
+
raise ArgumentError, "invalid UUID format #{format.inspect}" unless template
|
326
|
+
|
327
|
+
# for this special case, the parts are going to be strings which we will 0 pad
|
328
|
+
if format == :teenie
|
329
|
+
part1 = part1.base62_encode
|
330
|
+
part2 = part2.base62_encode
|
331
|
+
part3 = part3.base62_encode
|
332
|
+
part4 = part4.base62_encode
|
333
|
+
part5 = part5.base62_encode
|
334
|
+
|
335
|
+
(template % [part1, part2, part3, part4, part5]).gsub(' ', '0')
|
336
|
+
else
|
337
|
+
template % [part1, part2, part3, part4, part5]
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
303
341
|
protected
|
304
342
|
|
305
343
|
##
|
@@ -0,0 +1,40 @@
|
|
1
|
+
###
|
2
|
+
# test the translate method
|
3
|
+
#
|
4
|
+
require "test/unit"
|
5
|
+
require "uuid"
|
6
|
+
|
7
|
+
class TestTranslate < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def test_default_to_teenie_and_back
|
10
|
+
|
11
|
+
11.times do
|
12
|
+
uuid = UUID.new
|
13
|
+
original_uuid = uuid.generate()
|
14
|
+
|
15
|
+
teenie_uuid = UUID.translate(original_uuid, :default, :teenie)
|
16
|
+
assert UUID.validate_teenie(teenie_uuid), 'teenie'
|
17
|
+
|
18
|
+
default_uuid = UUID.translate(teenie_uuid, :teenie, :default)
|
19
|
+
assert UUID.validate(default_uuid), 'default'
|
20
|
+
|
21
|
+
assert default_uuid == original_uuid, 'got back what we started with'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_teenie_to_compact_and_back
|
26
|
+
|
27
|
+
7.times do
|
28
|
+
uuid = UUID.new
|
29
|
+
original_uuid = uuid.generate(:teenie)
|
30
|
+
|
31
|
+
compact_uuid = UUID.translate(original_uuid, :teenie, :compact)
|
32
|
+
assert UUID.validate(compact_uuid), 'compact'
|
33
|
+
|
34
|
+
teenie_uuid = UUID.translate(compact_uuid, :compact, :teenie)
|
35
|
+
assert UUID.validate_teenie(teenie_uuid), 'teenie'
|
36
|
+
|
37
|
+
assert teenie_uuid == original_uuid, 'got back what we started with'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gn0m30-uuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Assaf Arkin
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2010-
|
14
|
+
date: 2010-04-05 00:00:00 -04:00
|
15
15
|
default_executable:
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
@@ -47,6 +47,7 @@ extra_rdoc_files:
|
|
47
47
|
- README.rdoc
|
48
48
|
- MIT-LICENSE
|
49
49
|
files:
|
50
|
+
- test/test-translate.rb
|
50
51
|
- test/test-uuid.rb
|
51
52
|
- lib/uuid.rb
|
52
53
|
- README.rdoc
|