simple_uuid 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.1.0 move UUID class into a SimpleUUID module
2
+
1
3
  v0.0.2 Update equality test for compatibility with Cassandra gem.
2
4
 
3
5
  v0.0.1 Initial extraction from the cassandra gem.
data/lib/simple_uuid.rb CHANGED
@@ -8,127 +8,129 @@ class Time
8
8
  end
9
9
  end
10
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
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
- class InvalidVersion < StandardError #:nodoc:
15
- end
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} (invalid bytecount)"
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
- 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
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
- 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
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
- def variant
79
- @bytes.unpack('QnnN')[1] >> 13
80
- end
79
+ def variant
80
+ @bytes.unpack('QnnN')[1] >> 13
81
+ end
81
82
 
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
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
- def seconds
90
- total_usecs / 1_000_000
91
- end
90
+ def seconds
91
+ total_usecs / 1_000_000
92
+ end
92
93
 
93
- def usecs
94
- total_usecs % 1_000_000
95
- end
94
+ def usecs
95
+ total_usecs % 1_000_000
96
+ end
96
97
 
97
- def <=>(other)
98
- total_usecs <=> other.send(:total_usecs)
99
- end
98
+ def <=>(other)
99
+ total_usecs <=> other.send(:total_usecs)
100
+ end
100
101
 
101
- def ==(other)
102
- to_s == other.to_s
103
- end
102
+ def ==(other)
103
+ to_s == other.to_s
104
+ end
104
105
 
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
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
- def to_s
116
- @bytes
117
- end
118
- alias bytes to_s
116
+ def to_s
117
+ @bytes
118
+ end
119
+ alias bytes to_s
119
120
 
120
- def hash
121
- @bytes.hash
122
- end
121
+ def hash
122
+ @bytes.hash
123
+ end
123
124
 
124
- def eql?(other)
125
- other.respond_to?(:bytes) && bytes == other.bytes
126
- end
125
+ def eql?(other)
126
+ other.respond_to?(:bytes) && bytes == other.bytes
127
+ end
127
128
 
128
- private
129
+ private
129
130
 
130
- def total_usecs
131
- elements = @bytes.unpack("NnnQ")
132
- (elements[0] + (elements[1] << 32) + ((elements[2] & 0x0FFF) << 48) - GREGORIAN_EPOCH_OFFSET) / 10
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.2"
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-01-25}
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
@@ -1,5 +1,6 @@
1
1
  require 'test/unit'
2
2
  require 'simple_uuid'
3
+ include SimpleUUID
3
4
 
4
5
  class UUIDTest < Test::Unit::TestCase
5
6
  def test_uuid_sort
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.2
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-01-25 00:00:00 -08:00
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