module BetterUUID::ClassMethods
Public Instance Methods
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 :)
# 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
UUID generation using MD5 (for backward compat.)
# 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
UUID generation using random-number generator. From it’s random nature, there’s no warranty that the created ID is really universaly unique.
# 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
UUID generation using SHA1. Recommended over create_md5. Namespace
object is another UUID, some of them are pre-defined below.
# 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
The ‘primitive constructor’ of this class Note UUID.pack(uuid.unpack) == uuid
# 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
A simple GUID parser: just ignores unknown characters and convert hexadecimal dump into 16-octet object.
# 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
Private Instance Methods
# File lib/better-uuid/class_methods.rb, line 115 def get_time # UUID epoch is 1582/Oct/15 tt = Time.now tt.to_i*10000000 + tt.tv_usec*10 + 0x01B21DD213814000 end
# File lib/better-uuid/class_methods.rb, line 62 def mask(v, str) if RUBY_VERSION >= '1.9.0' return mask19 v, str else return mask18 v, str end end
# File lib/better-uuid/class_methods.rb, line 51 def mask18(v, str) # :nodoc version = [0, 16, 32, 48, 64, 80][v] str[6] &= 0b00001111 str[6] |= version # str[7] &= 0b00001111 # str[7] |= 0b01010000 str[8] &= 0b00111111 str[8] |= 0b10000000 str end
# File lib/better-uuid/class_methods.rb, line 37 def mask19(v, str) # :nodoc nstr = str.bytes.to_a version = [0, 16, 32, 48, 64, 80][v] nstr[6] &= 0b00001111 nstr[6] |= version # nstr[7] &= 0b00001111 # nstr[7] |= 0b01010000 nstr[8] &= 0b00111111 nstr[8] |= 0b10000000 str = '' nstr.each { |s| str << s.chr } str end