module BetterUUID::ClassMethods

Public Instance Methods

create(clock = nil, time = nil, mac_addr = nil) click to toggle source

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
Also aliased as: create_v1
create_md5(str, namespace) click to toggle source

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
Also aliased as: create_v3
create_random() click to toggle source

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
Also aliased as: create_v4
create_sha1(str, namespace) click to toggle source

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
Also aliased as: create_v5
create_v1(clock = nil, time = nil, mac_addr = nil)
Alias for: create
create_v3(str, namespace)
Alias for: create_md5
create_v4()
Alias for: create_random
create_v5(str, namespace)
Alias for: create_sha1
pack(tl, tm, th, ch, cl, n) click to toggle source

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
parse(obj) click to toggle source

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

get_time() click to toggle source
# 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
mask(v, str) click to toggle source
# 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
mask18(v, str) click to toggle source
# 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
mask19(v, str) click to toggle source
# 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