simple_uuid 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +1 -0
- data/LICENSE +0 -0
- data/Manifest +7 -0
- data/README +0 -0
- data/Rakefile +7 -0
- data/lib/simple_uuid.rb +137 -0
- data/simple_uuid.gemspec +33 -0
- data/test/test_uuid.rb +35 -0
- data.tar.gz.sig +0 -0
- metadata +91 -0
- metadata.gz.sig +0 -0
data/CHANGELOG
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
v0.0.1 Initial extraction from the cassandra gem.
|
data/LICENSE
ADDED
File without changes
|
data/Manifest
ADDED
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
data/lib/simple_uuid.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
class Time
|
2
|
+
def self.stamp
|
3
|
+
Time.now.stamp
|
4
|
+
end
|
5
|
+
|
6
|
+
def stamp
|
7
|
+
to_i * 1_000_000 + usec
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# UUID format version 1, as specified in RFC 4122, with jitter in place of the mac address and sequence counter.
|
12
|
+
class UUID
|
13
|
+
|
14
|
+
class InvalidVersion < StandardError #:nodoc:
|
15
|
+
end
|
16
|
+
|
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
|
+
else
|
34
|
+
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (invalid bytecount)"
|
35
|
+
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
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_i
|
65
|
+
ints = @bytes.unpack("NNNN")
|
66
|
+
(ints[0] << 96) +
|
67
|
+
(ints[1] << 64) +
|
68
|
+
(ints[2] << 32) +
|
69
|
+
ints[3]
|
70
|
+
end
|
71
|
+
|
72
|
+
def version
|
73
|
+
time_high = @bytes.unpack("NnnQ")[2]
|
74
|
+
version = (time_high & 0xF000).to_s(16)[0].chr.to_i
|
75
|
+
version > 0 and version < 6 ? version : -1
|
76
|
+
end
|
77
|
+
|
78
|
+
def variant
|
79
|
+
@bytes.unpack('QnnN')[1] >> 13
|
80
|
+
end
|
81
|
+
|
82
|
+
def to_guid
|
83
|
+
elements = @bytes.unpack("NnnCCa6")
|
84
|
+
node = elements[-1].unpack('C*')
|
85
|
+
elements[-1] = '%02x%02x%02x%02x%02x%02x' % node
|
86
|
+
"%08x-%04x-%04x-%02x%02x-%s" % elements
|
87
|
+
end
|
88
|
+
|
89
|
+
def seconds
|
90
|
+
total_usecs / 1_000_000
|
91
|
+
end
|
92
|
+
|
93
|
+
def usecs
|
94
|
+
total_usecs % 1_000_000
|
95
|
+
end
|
96
|
+
|
97
|
+
def <=>(other)
|
98
|
+
total_usecs <=> other.send(:total_usecs)
|
99
|
+
end
|
100
|
+
|
101
|
+
def ==(other)
|
102
|
+
to_s == other.to_s
|
103
|
+
end
|
104
|
+
|
105
|
+
def inspect(long = false)
|
106
|
+
"<UUID##{object_id} time: #{
|
107
|
+
Time.at(seconds).inspect
|
108
|
+
}, usecs: #{
|
109
|
+
usecs
|
110
|
+
} jitter: #{
|
111
|
+
@bytes.unpack('QQ')[1]
|
112
|
+
}" + (long ? ", version: #{version}, variant: #{variant}, guid: #{to_guid}>" : ">")
|
113
|
+
end
|
114
|
+
|
115
|
+
def hash
|
116
|
+
@bytes.hash
|
117
|
+
end
|
118
|
+
|
119
|
+
def eql?(other)
|
120
|
+
other.is_a?(Comparable) and @bytes == other.to_s
|
121
|
+
end
|
122
|
+
|
123
|
+
def ==(other)
|
124
|
+
other.respond_to?(:to_i) && self.to_i == other.to_i
|
125
|
+
end
|
126
|
+
|
127
|
+
def to_s
|
128
|
+
@bytes
|
129
|
+
end
|
130
|
+
|
131
|
+
private
|
132
|
+
|
133
|
+
def total_usecs
|
134
|
+
elements = @bytes.unpack("NnnQ")
|
135
|
+
(elements[0] + (elements[1] << 32) + ((elements[2] & 0x0FFF) << 48) - GREGORIAN_EPOCH_OFFSET) / 10
|
136
|
+
end
|
137
|
+
end
|
data/simple_uuid.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{simple_uuid}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Ryan King"]
|
9
|
+
s.cert_chain = ["/Users/ryan/.gemkeys/gem-public_cert.pem"]
|
10
|
+
s.date = %q{2010-01-20}
|
11
|
+
s.description = %q{Simple UUID generation.}
|
12
|
+
s.email = %q{ryan@twitter.com}
|
13
|
+
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib/simple_uuid.rb"]
|
14
|
+
s.files = ["CHANGELOG", "LICENSE", "README", "Rakefile", "lib/simple_uuid.rb", "test/test_uuid.rb", "Manifest", "simple_uuid.gemspec"]
|
15
|
+
s.homepage = %q{}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Simple_uuid", "--main", "README"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{simple_uuid}
|
19
|
+
s.rubygems_version = %q{1.3.5}
|
20
|
+
s.signing_key = %q{/Users/ryan/.gemkeys/gem-private_key.pem}
|
21
|
+
s.summary = %q{Simple UUID generation.}
|
22
|
+
s.test_files = ["test/test_uuid.rb"]
|
23
|
+
|
24
|
+
if s.respond_to? :specification_version then
|
25
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
26
|
+
s.specification_version = 3
|
27
|
+
|
28
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
29
|
+
else
|
30
|
+
end
|
31
|
+
else
|
32
|
+
end
|
33
|
+
end
|
data/test/test_uuid.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'simple_uuid'
|
3
|
+
|
4
|
+
class UUIDTest < Test::Unit::TestCase
|
5
|
+
def test_uuid_sort
|
6
|
+
ary = []
|
7
|
+
5.times { ary << UUID.new(Time.at(rand(2**31))) }
|
8
|
+
assert_equal ary.map { |_| _.seconds }.sort, ary.sort.map { |_| _.seconds }
|
9
|
+
assert_not_equal ary.sort, ary.sort_by {|_| _.to_guid }
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_uuid_equality
|
13
|
+
uuid = UUID.new
|
14
|
+
assert_equal uuid, UUID.new(uuid)
|
15
|
+
assert_equal uuid, UUID.new(uuid.to_s)
|
16
|
+
assert_equal uuid, UUID.new(uuid.to_i)
|
17
|
+
assert_equal uuid, UUID.new(uuid.to_guid)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_uuid_error
|
21
|
+
assert_raises(TypeError) do
|
22
|
+
UUID.new("bogus")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_types_behave_well
|
27
|
+
assert !(UUID.new() == false)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_uuid_casting_error
|
31
|
+
assert_raises(TypeError) do
|
32
|
+
UUID.new({})
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data.tar.gz.sig
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_uuid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan King
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
- |
|
12
|
+
-----BEGIN CERTIFICATE-----
|
13
|
+
MIIDNjCCAh6gAwIBAgIBADANBgkqhkiG9w0BAQUFADBBMQ0wCwYDVQQDDARyeWFu
|
14
|
+
MRswGQYKCZImiZPyLGQBGRYLdGhlcnlhbmtpbmcxEzARBgoJkiaJk/IsZAEZFgNj
|
15
|
+
b20wHhcNMTAwMTA4MTc1MDM0WhcNMTEwMTA4MTc1MDM0WjBBMQ0wCwYDVQQDDARy
|
16
|
+
eWFuMRswGQYKCZImiZPyLGQBGRYLdGhlcnlhbmtpbmcxEzARBgoJkiaJk/IsZAEZ
|
17
|
+
FgNjb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDLPp+0PtRT3qCI
|
18
|
+
02sMsADSn7Uf1GpyXUtk4Fb94LqUO6Scl91YDmbFMpjzrQwQvBYMIVreWcwSish6
|
19
|
+
nip6WEk9lqXcOeDmex/qY2/FVXG8ffqjFHiNiN9vpWrWj5VMICequ+ftzWLKsPIS
|
20
|
+
DGJ4o+Z6wEYRuirgaRPCYAUDPglsaqctJ56wPuycryMe5+ApSkOS9iLWMprQKEAq
|
21
|
+
j2R2OBV0dSARdbtzuKwrP7sLDo7uPa0egFBUlcZ+nujGr4LvmpryB8scNRNmZK1w
|
22
|
+
1rEI7O06CbULj08qYxEhnKmFE7LbBoN/HrmvZLVQK5mWuiZQhtmJuhBfStJsaDux
|
23
|
+
5tBEkYZVAgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQW
|
24
|
+
BBSnLarDEo5eBE2arSMrBdOOhtrnPTANBgkqhkiG9w0BAQUFAAOCAQEANER07s4K
|
25
|
+
Pvc1DSduliRDMUax/VSfLzDTtTAQwuSAPDrWAYXKugcJtOZOXjDbGL7c5zoWmy9u
|
26
|
+
Fn5vEVdm/93J+84D/IMaaof3BwX/NNEYH01CeZEIGMfc5AFFha7dabzP/uiPpb/c
|
27
|
+
GSvomC9IzyN37+eWwOS16cC+5XnBT6KRCaXYg2Fh6WpTgde67OVgXr4Q58HXlaZ+
|
28
|
+
/2BB3wq9lZ4JskvlpYpYnlPAUyiyc6R2Mjts1pURz5nkW4SuS7Kd1KCOOyr1McDH
|
29
|
+
VP12sTSjJclmI17BjDGQpAF0n9v5ExhJxWpeOjeBUPQsOin3ypEM1KkckLmOKvH6
|
30
|
+
zyKMYVRO0z/58g==
|
31
|
+
-----END CERTIFICATE-----
|
32
|
+
|
33
|
+
date: 2010-01-20 00:00:00 -08:00
|
34
|
+
default_executable:
|
35
|
+
dependencies: []
|
36
|
+
|
37
|
+
description: Simple UUID generation.
|
38
|
+
email: ryan@twitter.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- CHANGELOG
|
45
|
+
- LICENSE
|
46
|
+
- README
|
47
|
+
- lib/simple_uuid.rb
|
48
|
+
files:
|
49
|
+
- CHANGELOG
|
50
|
+
- LICENSE
|
51
|
+
- README
|
52
|
+
- Rakefile
|
53
|
+
- lib/simple_uuid.rb
|
54
|
+
- test/test_uuid.rb
|
55
|
+
- Manifest
|
56
|
+
- simple_uuid.gemspec
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: ""
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --line-numbers
|
64
|
+
- --inline-source
|
65
|
+
- --title
|
66
|
+
- Simple_uuid
|
67
|
+
- --main
|
68
|
+
- README
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: "1.2"
|
82
|
+
version:
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project: simple_uuid
|
86
|
+
rubygems_version: 1.3.5
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Simple UUID generation.
|
90
|
+
test_files:
|
91
|
+
- test/test_uuid.rb
|
metadata.gz.sig
ADDED
Binary file
|