Module: BetterUUID::ClassMethods

Included in:
BetterUUID
Defined in:
lib/better-uuid/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#create(clock = nil, time = nil, mac_addr = nil) ⇒ Object Also known as: create_v1

create the ‘version 1’ UUID with current system clock, current UTC timestamp, and the IEEE 802 address (so-called MAC address).

Speed notice: it’s slow. It writes some data into hard drive on every invokation. If you want to speed this up, try remounting tmpdir with a memory based filesystem (such as tmpfs). STILL slow? then no way but rewrite it with c :)



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/better-uuid/class_methods.rb', line 129

def create(clock = nil, time = nil, mac_addr = nil)
  c, m = StateFile::update(clock, mac_addr)
  time ||= get_time

  tl = time & 0xFFFF_FFFF
  tm = time >> 32
  tm = tm & 0xFFFF
  th = time >> 48
  th = th & 0x0FFF
  th = th | 0x1000
  cl = c & 0xFF
  ch = c & 0x3F00
  ch = ch >> 8
  ch = ch | 0x80
  pack tl, tm, th, cl, ch, m
end

#create_md5(str, namespace) ⇒ Object Also known as: create_v3

UUID generation using MD5 (for backward compat.)



86
87
88
89
90
91
92
93
94
95
# File 'lib/better-uuid/class_methods.rb', line 86

def create_md5(str, namespace)
  md5 = Digest::MD5.new
  md5.update namespace.raw_bytes
  md5.update str
  sum = md5.digest
  raw = mask 3, sum[0..16]
  ret = new raw
  ret.freeze
  ret
end

#create_randomObject Also known as: create_v4

UUID generation using random-number generator. From it’s random nature, there’s no warranty that the created ID is really universaly unique.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/better-uuid/class_methods.rb', line 101

def create_random
  rnd = [
    rand(0x100000000),
    rand(0x100000000),
    rand(0x100000000),
    rand(0x100000000),
  ].pack 'N4'
  raw = mask 4, rnd
  ret = new raw
  ret.freeze
  ret
end

#create_sha1(str, namespace) ⇒ Object Also known as: create_v5

UUID generation using SHA1. Recommended over create_md5. Namespace object is another UUID, some of them are pre-defined below.



73
74
75
76
77
78
79
80
81
82
# File 'lib/better-uuid/class_methods.rb', line 73

def create_sha1(str, namespace)
  sha1 = Digest::SHA1.new
  sha1.update namespace.raw_bytes
  sha1.update str
  sum = sha1.digest
  raw = mask 5, sum[0..15]
  ret = new raw
  ret.freeze
  ret
end

#pack(tl, tm, th, ch, cl, n) ⇒ Object

The ‘primitive constructor’ of this class Note UUID.pack(uuid.unpack) == uuid



160
161
162
163
164
165
# File 'lib/better-uuid/class_methods.rb', line 160

def pack(tl, tm, th, ch, cl, n)
  raw = [tl, tm, th, ch, cl, n].pack 'NnnCCa6'
  ret = new raw
  ret.freeze
  ret
end

#parse(obj) ⇒ Object

A simple GUID parser: just ignores unknown characters and convert hexadecimal dump into 16-octet object.



149
150
151
152
153
154
155
156
# File 'lib/better-uuid/class_methods.rb', line 149

def parse(obj)
  str = obj.dup.to_s.sub %r/\Aurn:uuid:/, ''
  str.gsub! %r/[^0-9A-Fa-f]/, ''
  raw = str[0..31].lines.to_a.pack 'H*'
  ret = new raw
  ret.freeze
  ret
end