Class BareTest::UID
In: lib/baretest/uid.rb
Parent: Object

Generate a unique ID. Not using uuid simply to avoid two dependencies (uuid, and uuid‘s dependency ‘macaddr’).

Methods

hex   hex_uid   new  

Constants

Epoch = Time.utc(2000,1,1).to_i # :nodoc:

Attributes

value  [R]  The numeric value of the uid (an Integer)

Public Class methods

Returns a 32byte long String containing a new 16 byte random value in hex representation

[Source]

    # File lib/baretest/uid.rb, line 23
23:     def self.hex_uid
24:       new.hex
25:     end

Create a 128bit (16 Byte) long random number

[Source]

    # File lib/baretest/uid.rb, line 28
28:     def initialize
29:       now = Time.now
30:       
31:       # Works for the next 100 years - should be enough
32:       # after that, it'll wrap around.
33:       time_part_52   = (((now.to_i-Epoch) & 0xffffffff) << 20) + now.usec
34: 
35:       # Not solid, but for the purposes, should work
36:       process_part32 = (Thread.current.object_id ^ Process.pid) & 0xfffffffff
37: 
38:       # Add a bit of random noise
39:       random_part44  = Kernel.rand(0x100000000000)
40: 
41:       @value = (random_part44 << 84) + (process_part32 << 52) + time_part_52
42:     end

Public Instance methods

Returns a 32byte long String containing the 16 byte random value in hex representation

[Source]

    # File lib/baretest/uid.rb, line 46
46:     def hex
47:       "%032x" % @value
48:     end

[Validate]