uuid4r-candy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/uuid4r-candy.rb +175 -0
  2. metadata +76 -0
@@ -0,0 +1,175 @@
1
+ require 'uuid4r'
2
+
3
+ module UUID
4
+ GREGORIAN_EPOCH_OFFSET = 0x01B2_1DD2_1381_4000 # Oct 15, 1582
5
+ VARIANT = 0b1000_0000_0000_0000
6
+
7
+ # Will accept :str, :bin, :txt
8
+ module TemplateUUID4R
9
+ def to_s
10
+ self.export(:str)
11
+ end
12
+ def to_b
13
+ self.export(:bin)
14
+ end
15
+ def to_txt
16
+ self.export(:txt)
17
+ end
18
+ end
19
+
20
+ class V1 < UUID4R::UUID4Rv1
21
+ include TemplateUUID4R
22
+ def to_time
23
+ UUID.time(self)
24
+ end
25
+ def to_mac
26
+ UUID.mac(self)
27
+ end
28
+ def clock_low_seq
29
+ UUID.clock_low_seq(self)
30
+ end
31
+ alias :random :clock_low_seq
32
+ end
33
+ class TimeStamp < V1 ; end
34
+
35
+ class V3 < UUID4R::UUID4Rv3
36
+ include TemplateUUID4R
37
+ end
38
+ class MD5 < V3 ; end
39
+
40
+ class V4 < UUID4R::UUID4Rv4
41
+ include TemplateUUID4R
42
+ end
43
+ class Random < V4 ; end
44
+
45
+ class V5 < UUID4R::UUID4Rv5
46
+ include TemplateUUID4R
47
+ end
48
+ class SHA < V5 ; end
49
+
50
+ def self.normalize(uuid)
51
+ case uuid
52
+ when String
53
+ p [ uuid, uuid.size ]
54
+ case uuid.size
55
+ when 16
56
+ uuid = UUID4R::import(:bin, uuid)
57
+ when 32
58
+ uuid = UUID4R::import(:str, uuid)
59
+ else
60
+ raise "Invalid import format"
61
+ end
62
+ else
63
+ uuid
64
+ end
65
+ end
66
+
67
+ # Most of the credit goes to https://github.com/ryanking/simple_uuid
68
+ def self.time(uuid)
69
+ uuid = self.normalize(uuid)
70
+ return uuid if uuid.nil?
71
+ bin = (uuid.respond_to? :to_b) ? uuid.to_b : uuid.export(:bin)
72
+ elements = bin.unpack("Nnn")
73
+ usecs = (elements[0] + (elements[1] << 32) + ((elements[2] & 0x0FFF) << 48) - GREGORIAN_EPOCH_OFFSET) / 10
74
+ Time.at( usecs / 1_000_000, usecs % 1_000_000 )
75
+ end
76
+
77
+ def self.mac(uuid)
78
+ uuid = self.normalize(uuid)
79
+ return uuid if uuid.nil?
80
+ bin = (uuid.respond_to? :to_b) ? uuid.to_b : uuid.export(:bin)
81
+ '%02x:%02x:%02x:%02x:%02x:%02x' % bin.unpack('QnCCCCCC')[2..-1]
82
+ end
83
+
84
+ def self.clock_low_seq(uuid)
85
+ uuid = self.normalize(uuid)
86
+ return uuid if uuid.nil?
87
+ bin = (uuid.respond_to? :to_b) ? uuid.to_b : uuid.export(:bin)
88
+ bin.unpack('QCC')[2]
89
+ end
90
+
91
+ # Most of the credit goes to https://github.com/ryanking/simple_uuid
92
+ def self.variant(uuid)
93
+ uuid = self.normalize(uuid)
94
+ bin = (uuid.respond_to? :to_b) ? uuid.to_b : uuid.export(:bin)
95
+ bin.unpack('QnnN')[1] >> 13
96
+ end
97
+
98
+ def self.v1
99
+ V1.new
100
+ end
101
+
102
+ def self.timestamp
103
+ TimeStamp.new
104
+ end
105
+
106
+ def self.v3(ns, n)
107
+ V3.new(ns, n)
108
+ end
109
+
110
+ def self.md5(ns, n)
111
+ MD5.new(ns, n)
112
+ end
113
+
114
+ def self.v4
115
+ V4.new
116
+ end
117
+
118
+ def self.random
119
+ Random.new
120
+ end
121
+
122
+ def self.v5(ns, n)
123
+ V5.new(ns, n)
124
+ end
125
+
126
+ def self.sha(ns, n)
127
+ SHA.new(ns, n)
128
+ end
129
+
130
+ end
131
+
132
+ if $0 == __FILE__
133
+
134
+ t = UUID.sha("ns:DNS", "HOOL")
135
+ p "SHA: #{t}"
136
+ s = UUID::TimeStamp.new
137
+ p "TIME: #{s}"
138
+ p [ :compare , t <=> s ]
139
+
140
+
141
+ tt = UUID.time(t)
142
+ st = UUID.time(s)
143
+ p [ :time , tt, st, tt.usec, st.usec , tt <=> st, st <=> tt , st < tt]
144
+ p [ s.to_mac, s.clock_low_seq, '%02x' % s.clock_low_seq ]
145
+ p UUID.variant(t)
146
+
147
+ require 'benchmark'
148
+
149
+ n = 100000
150
+ Benchmark.bm(7) do |x|
151
+ t = nil
152
+
153
+ x.report("create TimeStamp") {
154
+ n.times {
155
+ t = UUID::TimeStamp.new
156
+ }
157
+ }
158
+ x.report("str_export") {
159
+ n.times {
160
+ t.to_s
161
+ }
162
+ }
163
+ x.report("clock_low_seq") {
164
+ n.times {
165
+ t.clock_low_seq
166
+ }
167
+ }
168
+ x.report("to_mac") {
169
+ n.times {
170
+ t.to_mac
171
+ }
172
+ }
173
+
174
+ end
175
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uuid4r-candy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Digital Akasha
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-12-12 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: uuid4r
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 1
30
+ - 2
31
+ version: 0.1.2
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description:
35
+ email: tormenta@digitalakasha.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/uuid4r-candy.rb
44
+ has_rdoc: true
45
+ homepage: https://github.com/tormenta/uuid4r-candy
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.6
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Class/Module Candy For gem uuid4r
75
+ test_files: []
76
+