Class: UUID

Inherits:
Object
  • Object
show all
Defined in:
lib/locality-uuid.rb

Constant Summary collapse

GEMVERSION =
'1.2.0'
@@VERSION =
'b'
@@MAC =
Mac.addr[7..-1].gsub(/:/, '')
@@MIDDLE =
@@VERSION + @@MAC
@@PRIME =
198491317
@@COUNTER_MAX =
4294967295
@@PID_MAX =
65536
@@PID =
$$ % @@PID_MAX
@@REGEX =
/^[0-9a-zA-Z]{8}\-[0-9a-zA-Z]{4}\-[0-9a-zA-Z]{4}\-[0-9a-zA-Z]{4}\-[0-9a-zA-Z]{12}$/
@@sequential =
false
@@counter =
Atomic.new(Random.rand(@@COUNTER_MAX))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil) ⇒ UUID

Returns a new instance of UUID.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/locality-uuid.rb', line 49

def initialize input = nil
  if input == nil
    generate
  elsif input.is_a? String
    parse input
  elsif input.is_a? UUID
    @content = input.bytes
  else
    raise "could not construct UUID with input #{input}"
  end
end

Class Method Details

.use_sequential_idsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/locality-uuid.rb', line 61

def self.use_sequential_ids
  if !@@sequential
    # get a string that changes every 10 minutes
    String date = Time.now.utc.strftime("%Y%m%d%H%M").slice(0, 11)

    # run an md5 hash of the string, no reason this needs to be secure
    digest = Digest::MD5.hexdigest date

    # get first 4 bytes of digest, reverse, and turn into int
    x = [digest[0...8]].pack("H*").unpack("l")[0]
    @@counter.update { |curr| x }
  end
  @@sequential = true
end

.use_variable_idsObject



76
77
78
# File 'lib/locality-uuid.rb', line 76

def self.use_variable_ids
  @@sequential = false
end

Instance Method Details

#==(other) ⇒ Object



126
127
128
129
# File 'lib/locality-uuid.rb', line 126

def == other
  return false unless other && other.is_a?(UUID)
  return (self.bytes == other.bytes)
end

#bytesObject



80
81
82
# File 'lib/locality-uuid.rb', line 80

def bytes
  String.new @content
end

#macObject



145
146
147
# File 'lib/locality-uuid.rb', line 145

def mac
  '_____' + @content[6..9].unpack('H*')[0][1..-1]
end

#pidObject



135
136
137
138
# File 'lib/locality-uuid.rb', line 135

def pid
  raise "incorrect UUID version: #{version}" unless version == @@VERSION
  @content[4..5].unpack('S>')[0]
end

#timestampObject



140
141
142
143
# File 'lib/locality-uuid.rb', line 140

def timestamp
  i = ("\x00\x00" + @content[10..15]).unpack('Q>')[0]
  Time.at((i / 1000), (i % 1000 * 1000)).utc
end

#to_sObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/locality-uuid.rb', line 84

def to_s
  s = @content.unpack('H*')[0]
  x = '________-____-____-____-____________'
  x[0 ] = s[0 ]
  x[1 ] = s[1 ]
  x[2 ] = s[2 ]
  x[3 ] = s[3 ]
  x[4 ] = s[4 ]
  x[5 ] = s[5 ]
  x[6 ] = s[6 ]
  x[7 ] = s[7 ]
  
  x[9 ] = s[8 ]
  x[10] = s[9 ]
  x[11] = s[10]
  x[12] = s[11]

  x[14] = s[12]
  x[15] = s[13]
  x[16] = s[14]
  x[17] = s[15]

  x[19] = s[16]
  x[20] = s[17]
  x[21] = s[18]
  x[22] = s[19]

  x[24] = s[20]
  x[25] = s[21]
  x[26] = s[22]
  x[27] = s[23]
  x[28] = s[24]
  x[29] = s[25]
  x[30] = s[26]
  x[31] = s[27]
  x[32] = s[28]
  x[33] = s[29]
  x[34] = s[30]
  x[35] = s[31]
  return x
end

#versionObject



131
132
133
# File 'lib/locality-uuid.rb', line 131

def version
  @content[6].unpack('H')[0]
end