simple_uuid 0.0.2 → 0.1.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.
- data/CHANGELOG +2 -0
- data/lib/simple_uuid.rb +106 -104
- data/simple_uuid.gemspec +2 -2
- data/test/test_uuid.rb +1 -0
- data.tar.gz.sig +0 -0
- metadata +2 -2
- metadata.gz.sig +0 -0
data/CHANGELOG
CHANGED
data/lib/simple_uuid.rb
CHANGED
@@ -8,127 +8,129 @@ class Time
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
module SimpleUUID
|
12
|
+
# UUID format version 1, as specified in RFC 4122, with jitter in place of the mac address and sequence counter.
|
13
|
+
class UUID
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
class InvalidVersion < StandardError #:nodoc:
|
16
|
+
end
|
17
|
+
|
18
|
+
GREGORIAN_EPOCH_OFFSET = 0x01B2_1DD2_1381_4000 # Oct 15, 1582
|
19
|
+
|
20
|
+
VARIANT = 0b1000_0000_0000_0000
|
21
|
+
|
22
|
+
def initialize(bytes = nil)
|
23
|
+
case bytes
|
24
|
+
when self.class # UUID
|
25
|
+
@bytes = bytes.to_s
|
26
|
+
when String
|
27
|
+
case bytes.size
|
28
|
+
when 16 # Raw byte array
|
29
|
+
@bytes = bytes
|
30
|
+
when 36 # Human-readable UUID representation; inverse of #to_guid
|
31
|
+
elements = bytes.split("-")
|
32
|
+
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (malformed UUID representation)" if elements.size != 5
|
33
|
+
@bytes = elements.join.to_a.pack('H32')
|
34
|
+
else
|
35
|
+
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (invalid bytecount)"
|
36
|
+
end
|
37
|
+
|
38
|
+
when Integer
|
39
|
+
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (integer out of range)" if bytes < 0 or bytes > 2**128
|
40
|
+
@bytes = [
|
41
|
+
(bytes >> 96) & 0xFFFF_FFFF,
|
42
|
+
(bytes >> 64) & 0xFFFF_FFFF,
|
43
|
+
(bytes >> 32) & 0xFFFF_FFFF,
|
44
|
+
bytes & 0xFFFF_FFFF
|
45
|
+
].pack("NNNN")
|
46
|
+
|
47
|
+
when NilClass, Time
|
48
|
+
time = (bytes || Time).stamp * 10 + GREGORIAN_EPOCH_OFFSET
|
49
|
+
# See http://github.com/spectra/ruby-uuid/
|
50
|
+
@bytes = [
|
51
|
+
time & 0xFFFF_FFFF,
|
52
|
+
time >> 32,
|
53
|
+
((time >> 48) & 0x0FFF) | 0x1000,
|
54
|
+
# Top 3 bytes reserved
|
55
|
+
rand(2**13) | VARIANT,
|
56
|
+
rand(2**16),
|
57
|
+
rand(2**32)
|
58
|
+
].pack("NnnnnN")
|
16
59
|
|
17
|
-
GREGORIAN_EPOCH_OFFSET = 0x01B2_1DD2_1381_4000 # Oct 15, 1582
|
18
|
-
|
19
|
-
VARIANT = 0b1000_0000_0000_0000
|
20
|
-
|
21
|
-
def initialize(bytes = nil)
|
22
|
-
case bytes
|
23
|
-
when self.class # UUID
|
24
|
-
@bytes = bytes.to_s
|
25
|
-
when String
|
26
|
-
case bytes.size
|
27
|
-
when 16 # Raw byte array
|
28
|
-
@bytes = bytes
|
29
|
-
when 36 # Human-readable UUID representation; inverse of #to_guid
|
30
|
-
elements = bytes.split("-")
|
31
|
-
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (malformed UUID representation)" if elements.size != 5
|
32
|
-
@bytes = elements.join.to_a.pack('H32')
|
33
60
|
else
|
34
|
-
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (
|
61
|
+
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (unknown source class)"
|
35
62
|
end
|
36
|
-
|
37
|
-
when Integer
|
38
|
-
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (integer out of range)" if bytes < 0 or bytes > 2**128
|
39
|
-
@bytes = [
|
40
|
-
(bytes >> 96) & 0xFFFF_FFFF,
|
41
|
-
(bytes >> 64) & 0xFFFF_FFFF,
|
42
|
-
(bytes >> 32) & 0xFFFF_FFFF,
|
43
|
-
bytes & 0xFFFF_FFFF
|
44
|
-
].pack("NNNN")
|
45
|
-
|
46
|
-
when NilClass, Time
|
47
|
-
time = (bytes || Time).stamp * 10 + GREGORIAN_EPOCH_OFFSET
|
48
|
-
# See http://github.com/spectra/ruby-uuid/
|
49
|
-
@bytes = [
|
50
|
-
time & 0xFFFF_FFFF,
|
51
|
-
time >> 32,
|
52
|
-
((time >> 48) & 0x0FFF) | 0x1000,
|
53
|
-
# Top 3 bytes reserved
|
54
|
-
rand(2**13) | VARIANT,
|
55
|
-
rand(2**16),
|
56
|
-
rand(2**32)
|
57
|
-
].pack("NnnnnN")
|
58
|
-
|
59
|
-
else
|
60
|
-
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (unknown source class)"
|
61
63
|
end
|
62
|
-
end
|
63
64
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
65
|
+
def to_i
|
66
|
+
ints = @bytes.unpack("NNNN")
|
67
|
+
(ints[0] << 96) +
|
68
|
+
(ints[1] << 64) +
|
69
|
+
(ints[2] << 32) +
|
70
|
+
ints[3]
|
71
|
+
end
|
71
72
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
73
|
+
def version
|
74
|
+
time_high = @bytes.unpack("NnnQ")[2]
|
75
|
+
version = (time_high & 0xF000).to_s(16)[0].chr.to_i
|
76
|
+
version > 0 and version < 6 ? version : -1
|
77
|
+
end
|
77
78
|
|
78
|
-
|
79
|
-
|
80
|
-
|
79
|
+
def variant
|
80
|
+
@bytes.unpack('QnnN')[1] >> 13
|
81
|
+
end
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
83
|
+
def to_guid
|
84
|
+
elements = @bytes.unpack("NnnCCa6")
|
85
|
+
node = elements[-1].unpack('C*')
|
86
|
+
elements[-1] = '%02x%02x%02x%02x%02x%02x' % node
|
87
|
+
"%08x-%04x-%04x-%02x%02x-%s" % elements
|
88
|
+
end
|
88
89
|
|
89
|
-
|
90
|
-
|
91
|
-
|
90
|
+
def seconds
|
91
|
+
total_usecs / 1_000_000
|
92
|
+
end
|
92
93
|
|
93
|
-
|
94
|
-
|
95
|
-
|
94
|
+
def usecs
|
95
|
+
total_usecs % 1_000_000
|
96
|
+
end
|
96
97
|
|
97
|
-
|
98
|
-
|
99
|
-
|
98
|
+
def <=>(other)
|
99
|
+
total_usecs <=> other.send(:total_usecs)
|
100
|
+
end
|
100
101
|
|
101
|
-
|
102
|
-
|
103
|
-
|
102
|
+
def ==(other)
|
103
|
+
to_s == other.to_s
|
104
|
+
end
|
104
105
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
106
|
+
def inspect(long = false)
|
107
|
+
"<UUID##{object_id} time: #{
|
108
|
+
Time.at(seconds).inspect
|
109
|
+
}, usecs: #{
|
110
|
+
usecs
|
111
|
+
} jitter: #{
|
112
|
+
@bytes.unpack('QQ')[1]
|
113
|
+
}" + (long ? ", version: #{version}, variant: #{variant}, guid: #{to_guid}>" : ">")
|
114
|
+
end
|
114
115
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
116
|
+
def to_s
|
117
|
+
@bytes
|
118
|
+
end
|
119
|
+
alias bytes to_s
|
119
120
|
|
120
|
-
|
121
|
-
|
122
|
-
|
121
|
+
def hash
|
122
|
+
@bytes.hash
|
123
|
+
end
|
123
124
|
|
124
|
-
|
125
|
-
|
126
|
-
|
125
|
+
def eql?(other)
|
126
|
+
other.respond_to?(:bytes) && bytes == other.bytes
|
127
|
+
end
|
127
128
|
|
128
|
-
|
129
|
+
private
|
129
130
|
|
130
|
-
|
131
|
-
|
132
|
-
|
131
|
+
def total_usecs
|
132
|
+
elements = @bytes.unpack("NnnQ")
|
133
|
+
(elements[0] + (elements[1] << 32) + ((elements[2] & 0x0FFF) << 48) - GREGORIAN_EPOCH_OFFSET) / 10
|
134
|
+
end
|
133
135
|
end
|
134
|
-
end
|
136
|
+
end
|
data/simple_uuid.gemspec
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{simple_uuid}
|
5
|
-
s.version = "0.0
|
5
|
+
s.version = "0.1.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Ryan King"]
|
9
9
|
s.cert_chain = ["/Users/ryan/.gemkeys/gem-public_cert.pem"]
|
10
|
-
s.date = %q{2010-
|
10
|
+
s.date = %q{2010-02-19}
|
11
11
|
s.description = %q{Simple UUID generation.}
|
12
12
|
s.email = %q{ryan@twitter.com}
|
13
13
|
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib/simple_uuid.rb"]
|
data/test/test_uuid.rb
CHANGED
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_uuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan King
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
zyKMYVRO0z/58g==
|
31
31
|
-----END CERTIFICATE-----
|
32
32
|
|
33
|
-
date: 2010-
|
33
|
+
date: 2010-02-19 00:00:00 -08:00
|
34
34
|
default_executable:
|
35
35
|
dependencies: []
|
36
36
|
|
metadata.gz.sig
CHANGED
Binary file
|