Module: UUID

Defined in:
lib/uuid4r-candy.rb

Defined Under Namespace

Modules: TemplateUUID4R Classes: MD5, Random, SHA, TimeStamp, V1, V3, V4, V5

Constant Summary collapse

GREGORIAN_EPOCH_OFFSET =

Oct 15, 1582

0x01B2_1DD2_1381_4000
VARIANT =
0b1000_0000_0000_0000

Class Method Summary collapse

Class Method Details

.clock_low_seq(uuid) ⇒ Object



84
85
86
87
88
89
# File 'lib/uuid4r-candy.rb', line 84

def self.clock_low_seq(uuid)
  uuid = self.normalize(uuid)
  return uuid if uuid.nil?
  bin = (uuid.respond_to? :to_b) ? uuid.to_b : uuid.export(:bin)
  bin.unpack('QCC')[2]
end

.mac(uuid) ⇒ Object



77
78
79
80
81
82
# File 'lib/uuid4r-candy.rb', line 77

def self.mac(uuid)
  uuid = self.normalize(uuid)
  return uuid if uuid.nil?
  bin = (uuid.respond_to? :to_b) ? uuid.to_b : uuid.export(:bin)
  '%02x:%02x:%02x:%02x:%02x:%02x' % bin.unpack('QnCCCCCC')[2..-1]
end

.md5(ns, n) ⇒ Object



110
111
112
# File 'lib/uuid4r-candy.rb', line 110

def self.md5(ns, n)
  MD5.new(ns, n)
end

.normalize(uuid) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/uuid4r-candy.rb', line 50

def self.normalize(uuid)
  case uuid
  when String
    p [ uuid, uuid.size ]
    case uuid.size
    when 16
      uuid = UUID4R::import(:bin, uuid)
    when 32
      uuid  = UUID4R::import(:str, uuid)
    else
      raise "Invalid import format"
    end
  else
    uuid
  end 
end

.randomObject



118
119
120
# File 'lib/uuid4r-candy.rb', line 118

def self.random
  Random.new
end

.sha(ns, n) ⇒ Object



126
127
128
# File 'lib/uuid4r-candy.rb', line 126

def self.sha(ns, n)
  SHA.new(ns, n)
end

.time(uuid) ⇒ Object

Most of the credit goes to github.com/ryanking/simple_uuid



68
69
70
71
72
73
74
75
# File 'lib/uuid4r-candy.rb', line 68

def self.time(uuid)
  uuid = self.normalize(uuid)
  return uuid if uuid.nil?
  bin = (uuid.respond_to? :to_b) ? uuid.to_b : uuid.export(:bin)
  elements = bin.unpack("Nnn")
  usecs = (elements[0] + (elements[1] << 32) + ((elements[2] & 0x0FFF) << 48) - GREGORIAN_EPOCH_OFFSET) / 10
  Time.at( usecs / 1_000_000, usecs % 1_000_000 )
end

.timestampObject



102
103
104
# File 'lib/uuid4r-candy.rb', line 102

def self.timestamp
  TimeStamp.new
end

.v1Object



98
99
100
# File 'lib/uuid4r-candy.rb', line 98

def self.v1
  V1.new
end

.v3(ns, n) ⇒ Object



106
107
108
# File 'lib/uuid4r-candy.rb', line 106

def self.v3(ns, n)
  V3.new(ns, n)
end

.v4Object



114
115
116
# File 'lib/uuid4r-candy.rb', line 114

def self.v4
  V4.new
end

.v5(ns, n) ⇒ Object



122
123
124
# File 'lib/uuid4r-candy.rb', line 122

def self.v5(ns, n)
  V5.new(ns, n)
end

.variant(uuid) ⇒ Object

Most of the credit goes to github.com/ryanking/simple_uuid



92
93
94
95
96
# File 'lib/uuid4r-candy.rb', line 92

def self.variant(uuid)
  uuid = self.normalize(uuid)
  bin = (uuid.respond_to? :to_b) ? uuid.to_b : uuid.export(:bin)
  bin.unpack('QnnN')[1] >> 13
end