Class: UUID
- Inherits:
-
Object
- Object
- UUID
- Defined in:
- lib/usesguid/uuid22.rb,
lib/usesguid/uuidtools.rb,
lib/usesguid/uuid_mysql.rb,
lib/usesguid/uuid_sqlserver.rb
Overview
Copyright © 2008 Intuit Written by Brian Morearty MIT License
Constant Summary collapse
- BUCKET_SIZE =
50
- @@chars64 =
Make an array of 64 URL-safe characters
('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + ['-','_']
- @@last_timestamp =
nil
- @@last_node_id =
nil
- @@last_clock_sequence =
nil
- @@state_file =
nil
- @@mutex =
Mutex.new
- @@guid_bucket_mutex =
Mutex.new
- @@guid_bucket =
nil
Instance Attribute Summary collapse
-
#clock_seq_hi_and_reserved ⇒ Object
Returns the value of attribute clock_seq_hi_and_reserved.
-
#clock_seq_low ⇒ Object
Returns the value of attribute clock_seq_low.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
-
#time_hi_and_version ⇒ Object
Returns the value of attribute time_hi_and_version.
-
#time_low ⇒ Object
Returns the value of attribute time_low.
-
#time_mid ⇒ Object
Returns the value of attribute time_mid.
Class Method Summary collapse
-
.convert_byte_string_to_int(byte_string) ⇒ Object
:nodoc:.
-
.convert_int_to_byte_string(integer, size) ⇒ Object
:nodoc:.
-
.create_from_hash(hash_class, namespace, name) ⇒ Object
:nodoc:.
-
.get_mac_address ⇒ Object
Returns the MAC address of the current computer’s network card.
-
.md5_create(namespace, name) ⇒ Object
Creates a UUID using the MD5 hash.
- .mysql2_create(connection = ActiveRecord::Base.connection) ⇒ Object
-
.mysql_create(connection = ActiveRecord::Base.connection) ⇒ Object
We’ll retrieve a bunch of guids at a time to reduce the # of DB round-trips.
-
.parse(uuid_string) ⇒ Object
Parses a UUID from a string.
-
.parse22(s) ⇒ Object
Create a new UUID from a 22char string.
-
.parse_raw(raw_string) ⇒ Object
Parses a UUID from a raw byte string.
-
.random_create ⇒ Object
Creates a UUID from a random value.
-
.sha1_create(namespace, name) ⇒ Object
Creates a UUID using the SHA1 hash.
- .sqlserver_create(connection = ActiveRecord::Base.connection) ⇒ Object
-
.timestamp_create(timestamp = nil) ⇒ Object
Creates a UUID from a timestamp.
-
.true_random ⇒ Object
Returns 128 bits of highly unpredictable data.
Instance Method Summary collapse
-
#<=>(other_uuid) ⇒ Object
Compares two UUIDs lexically.
-
#hexdigest ⇒ Object
Returns the hex digest of the UUID object.
-
#initialize(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) ⇒ UUID
constructor
A new instance of UUID.
-
#inspect ⇒ Object
Returns a representation of the object’s state.
-
#mac_address ⇒ Object
Returns the IEEE 802 address used to generate this UUID or nil if a MAC address was not used.
-
#nil_uuid? ⇒ Boolean
Returns true if this UUID is the nil UUID (00000000-0000-0000-0000-000000000000).
-
#random_node_id? ⇒ Boolean
This method applies only to version 1 UUIDs.
-
#raw ⇒ Object
Returns the raw bytes that represent this UUID.
-
#timestamp ⇒ Object
Returns the timestamp used to generate this UUID.
-
#to_i ⇒ Object
Returns an integer representation for this UUID.
-
#to_s ⇒ Object
Returns a string representation for this UUID.
-
#to_s22 ⇒ Object
return a 22 byte URL-safe string, encoded six bits at a time using 64 characters.
-
#to_uri ⇒ Object
Returns a URI for this UUID.
-
#to_uri_string ⇒ Object
Returns a URI string for this UUID.
-
#valid? ⇒ Boolean
Returns true if this UUID is valid.
-
#variant ⇒ Object
Returns the UUID variant.
-
#version ⇒ Object
Returns the UUID version type.
Constructor Details
#initialize(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) ⇒ UUID
Returns a new instance of UUID.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/usesguid/uuidtools.rb', line 112 def initialize(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) unless time_low >= 0 && time_low < 4294967296 raise ArgumentError, "Expected unsigned 32-bit number for time_low, got #{time_low}." end unless time_mid >= 0 && time_mid < 65536 raise ArgumentError, "Expected unsigned 16-bit number for time_mid, got #{time_mid}." end unless time_hi_and_version >= 0 && time_hi_and_version < 65536 raise ArgumentError, "Expected unsigned 16-bit number for time_hi_and_version, " + "got #{time_hi_and_version}." end unless clock_seq_hi_and_reserved >= 0 && clock_seq_hi_and_reserved < 256 raise ArgumentError, "Expected unsigned 8-bit number for clock_seq_hi_and_reserved, " + "got #{clock_seq_hi_and_reserved}." end unless clock_seq_low >= 0 && clock_seq_low < 256 raise ArgumentError, "Expected unsigned 8-bit number for clock_seq_low, " + "got #{clock_seq_low}." end unless nodes.respond_to? :size raise ArgumentError, "Expected nodes to respond to :size." end unless nodes.size == 6 raise ArgumentError, "Expected nodes to have size of 6." end for node in nodes unless node >= 0 && node < 256 raise ArgumentError, "Expected unsigned 8-bit number for each node, " + "got #{node}." end end @time_low = time_low @time_mid = time_mid @time_hi_and_version = time_hi_and_version @clock_seq_hi_and_reserved = clock_seq_hi_and_reserved @clock_seq_low = clock_seq_low @nodes = nodes end |
Instance Attribute Details
#clock_seq_hi_and_reserved ⇒ Object
Returns the value of attribute clock_seq_hi_and_reserved.
163 164 165 |
# File 'lib/usesguid/uuidtools.rb', line 163 def clock_seq_hi_and_reserved @clock_seq_hi_and_reserved end |
#clock_seq_low ⇒ Object
Returns the value of attribute clock_seq_low.
164 165 166 |
# File 'lib/usesguid/uuidtools.rb', line 164 def clock_seq_low @clock_seq_low end |
#nodes ⇒ Object
Returns the value of attribute nodes.
165 166 167 |
# File 'lib/usesguid/uuidtools.rb', line 165 def nodes @nodes end |
#time_hi_and_version ⇒ Object
Returns the value of attribute time_hi_and_version.
162 163 164 |
# File 'lib/usesguid/uuidtools.rb', line 162 def time_hi_and_version @time_hi_and_version end |
#time_low ⇒ Object
Returns the value of attribute time_low.
160 161 162 |
# File 'lib/usesguid/uuidtools.rb', line 160 def time_low @time_low end |
#time_mid ⇒ Object
Returns the value of attribute time_mid.
161 162 163 |
# File 'lib/usesguid/uuidtools.rb', line 161 def time_mid @time_mid end |
Class Method Details
.convert_byte_string_to_int(byte_string) ⇒ Object
:nodoc:
548 549 550 551 552 553 554 555 |
# File 'lib/usesguid/uuidtools.rb', line 548 def UUID.convert_byte_string_to_int(byte_string) #:nodoc: integer = 0 size = byte_string.size for i in 0..(size - 1) integer += (byte_string[i] << (((size - 1) - i) * 8)) end return integer end |
.convert_int_to_byte_string(integer, size) ⇒ Object
:nodoc:
540 541 542 543 544 545 546 |
# File 'lib/usesguid/uuidtools.rb', line 540 def UUID.convert_int_to_byte_string(integer, size) #:nodoc: byte_string = "" for i in 0..(size - 1) byte_string << ((integer >> (((size - 1) - i) * 8)) & 0xFF) end return byte_string end |
.create_from_hash(hash_class, namespace, name) ⇒ Object
:nodoc:
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/usesguid/uuidtools.rb', line 437 def UUID.create_from_hash(hash_class, namespace, name) #:nodoc: if hash_class == Digest::MD5 version = 3 elsif hash_class == Digest::SHA1 version = 5 else raise ArgumentError, "Expected Digest::SHA1 or Digest::MD5, got #{hash_class.name}." end hash = hash_class.new hash.update(namespace.raw) hash.update(name) hash_string = hash.to_s[0..31] new_uuid = UUID.parse("#{hash_string[0..7]}-#{hash_string[8..11]}-" + "#{hash_string[12..15]}-#{hash_string[16..19]}-#{hash_string[20..31]}") new_uuid.time_hi_and_version &= 0x0FFF new_uuid.time_hi_and_version |= (version << 12) new_uuid.clock_seq_hi_and_reserved &= 0x3F new_uuid.clock_seq_hi_and_reserved |= 0x80 return new_uuid end |
.get_mac_address ⇒ Object
Returns the MAC address of the current computer’s network card. Returns nil if a MAC address could not be found.
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/usesguid/uuidtools.rb', line 462 def UUID.get_mac_address #:nodoc: if RUBY_PLATFORM =~ /win/ && !(RUBY_PLATFORM =~ /darwin/) begin ifconfig_output = `ipconfig /all` mac_addresses = ifconfig_output.scan( Regexp.new("(#{(["[0-9A-F]{2}"] * 6).join("-")})")) if mac_addresses.size > 0 return mac_addresses.first.first.downcase.gsub(/-/, ":") end rescue end else begin ifconfig_output = `ifconfig` mac_addresses = ifconfig_output.scan( Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})")) if mac_addresses.size == 0 ifconfig_output = `ifconfig | grep HWaddr | cut -c39-` mac_addresses = ifconfig_output.scan( Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})")) end if mac_addresses.size == 0 ifconfig_output = `/sbin/ifconfig` mac_addresses = ifconfig_output.scan( Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})")) end if mac_addresses.size == 0 ifconfig_output = `/sbin/ifconfig | grep HWaddr | cut -c39-` mac_addresses = ifconfig_output.scan( Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})")) end if mac_addresses.size > 0 return mac_addresses.first.first end rescue end end end |
.md5_create(namespace, name) ⇒ Object
Creates a UUID using the MD5 hash. (Version 3)
271 272 273 |
# File 'lib/usesguid/uuidtools.rb', line 271 def UUID.md5_create(namespace, name) return UUID.create_from_hash(Digest::MD5, namespace, name) end |
.mysql2_create(connection = ActiveRecord::Base.connection) ⇒ Object
26 27 28 |
# File 'lib/usesguid/uuid_mysql.rb', line 26 def self.mysql2_create(connection=ActiveRecord::Base.connection) mysql_create connection end |
.mysql_create(connection = ActiveRecord::Base.connection) ⇒ Object
We’ll retrieve a bunch of guids at a time to reduce the # of DB round-trips. If the guid bucket is empty, re-fill it by calling MySQL. Then return a guid.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/usesguid/uuid_mysql.rb', line 13 def self.mysql_create(connection=ActiveRecord::Base.connection) raise "UUID.mysql_create only works with MySQL" unless connection.adapter_name.downcase =~ /mysql/ @@guid_bucket_mutex.synchronize do if @@guid_bucket.blank? uuid_functions = Array.new(BUCKET_SIZE, "UUID()") @@guid_bucket = connection.execute("SELECT #{uuid_functions.join(',')}").first end # My tests show shift is much faster than slice!(0), pop, or delete_at(0) parse @@guid_bucket.shift end end |
.parse(uuid_string) ⇒ Object
Parses a UUID from a string.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/usesguid/uuidtools.rb', line 168 def UUID.parse(uuid_string) unless uuid_string.kind_of? String raise ArgumentError, "Expected String, got #{uuid_string.class.name} instead." end uuid_components = uuid_string.downcase.scan( Regexp.new("^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-" + "([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$")).first raise ArgumentError, "Invalid UUID format." if uuid_components.nil? time_low = uuid_components[0].to_i(16) time_mid = uuid_components[1].to_i(16) time_hi_and_version = uuid_components[2].to_i(16) clock_seq_hi_and_reserved = uuid_components[3].to_i(16) clock_seq_low = uuid_components[4].to_i(16) nodes = [] for i in 0..5 nodes << uuid_components[5][(i * 2)..(i * 2) + 1].to_i(16) end return UUID.new(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) end |
.parse22(s) ⇒ Object
Create a new UUID from a 22char string
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/usesguid/uuid22.rb', line 24 def self.parse22(s) # get the integer representation integer=0 s.each_byte {|c| integer = integer << 6 pos=@@chars64.index(c.chr) integer+=pos } time_low = (integer >> 96) & 0xFFFFFFFF time_mid = (integer >> 80) & 0xFFFF time_hi_and_version = (integer >> 64) & 0xFFFF clock_seq_hi_and_reserved = (integer >> 56) & 0xFF clock_seq_low = (integer >> 48) & 0xFF nodes = [] for i in 0..5 do nodes << ((integer >> (40 - (i * 8))) & 0xFF) end return new(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) end |
.parse_raw(raw_string) ⇒ Object
Parses a UUID from a raw byte string.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/usesguid/uuidtools.rb', line 191 def UUID.parse_raw(raw_string) unless raw_string.kind_of? String raise ArgumentError, "Expected String, got #{raw_string.class.name} instead." end integer = UUID.convert_byte_string_to_int(raw_string) time_low = (integer >> 96) & 0xFFFFFFFF time_mid = (integer >> 80) & 0xFFFF time_hi_and_version = (integer >> 64) & 0xFFFF clock_seq_hi_and_reserved = (integer >> 56) & 0xFF clock_seq_low = (integer >> 48) & 0xFF nodes = [] for i in 0..5 nodes << ((integer >> (40 - (i * 8))) & 0xFF) end return UUID.new(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) end |
.random_create ⇒ Object
Creates a UUID from a random value.
212 213 214 215 216 217 218 219 |
# File 'lib/usesguid/uuidtools.rb', line 212 def UUID.random_create() new_uuid = UUID.parse_raw(UUID.true_random) new_uuid.time_hi_and_version &= 0x0FFF new_uuid.time_hi_and_version |= (4 << 12) new_uuid.clock_seq_hi_and_reserved &= 0x3F new_uuid.clock_seq_hi_and_reserved |= 0x80 return new_uuid end |
.sha1_create(namespace, name) ⇒ Object
Creates a UUID using the SHA1 hash. (Version 5)
276 277 278 |
# File 'lib/usesguid/uuidtools.rb', line 276 def UUID.sha1_create(namespace, name) return UUID.create_from_hash(Digest::SHA1, namespace, name) end |
.sqlserver_create(connection = ActiveRecord::Base.connection) ⇒ Object
3 4 5 6 |
# File 'lib/usesguid/uuid_sqlserver.rb', line 3 def self.sqlserver_create(connection=ActiveRecord::Base.connection) raise "UUID.sql_server_create only works with SQL Server" unless connection.adapter_name.downcase =~ /sqlserver/ parse connection.newid_function end |
.timestamp_create(timestamp = nil) ⇒ Object
Creates a UUID from a timestamp.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/usesguid/uuidtools.rb', line 222 def UUID.(=nil) # We need a lock here to prevent two threads from ever # getting the same timestamp. @@mutex.synchronize do # Always use GMT to generate UUIDs. if .nil? = Time.now.gmtime else = .gmtime end # Convert to 100 nanosecond blocks = (.tv_sec * 10000000) + (.tv_usec * 10) + 0x01B21DD213814000 nodes = UUID.get_mac_address.split(":").collect do |octet| octet.to_i(16) end node_id = 0 for i in 0..5 node_id += (nodes[i] << (40 - (i * 8))) end clock_sequence = @@last_clock_sequence if clock_sequence.nil? clock_sequence = UUID.convert_byte_string_to_int(UUID.true_random) end if @@last_node_id != nil && @@last_node_id != node_id # The node id has changed. Change the clock id. clock_sequence = UUID.convert_byte_string_to_int(UUID.true_random) elsif @@last_timestamp != nil && < @@last_timestamp clock_sequence = clock_sequence + 1 end @@last_timestamp = @@last_node_id = node_id @@last_clock_sequence = clock_sequence time_low = & 0xFFFFFFFF time_mid = (( >> 32) & 0xFFFF) time_hi_and_version = (( >> 48) & 0x0FFF) time_hi_and_version |= (1 << 12) clock_seq_low = clock_sequence & 0xFF; clock_seq_hi_and_reserved = (clock_sequence & 0x3F00) >> 8 clock_seq_hi_and_reserved |= 0x80 return UUID.new(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) end end |
.true_random ⇒ Object
Returns 128 bits of highly unpredictable data. The random number generator isn’t perfect, but it’s much, much better than the built-in pseudorandom number generators.
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'lib/usesguid/uuidtools.rb', line 504 def UUID.true_random #:nodoc: require 'benchmark' hash = Digest::SHA1.new performance = Benchmark.measure do hash.update(rand.to_s) hash.update(srand.to_s) hash.update(rand.to_s) hash.update(srand.to_s) hash.update(Time.now.to_s) hash.update(rand.to_s) hash.update(self.object_id.to_s) hash.update(rand.to_s) hash.update(hash.object_id.to_s) hash.update(self.methods.inspect) begin random_device = nil if File.exists? "/dev/urandom" random_device = File.open "/dev/urandom", "r" elsif File.exists? "/dev/random" random_device = File.open "/dev/random", "r" end hash.update(random_device.read(20)) if random_device != nil rescue end begin srand(hash.to_s.to_i(16) >> 128) rescue end hash.update(rand.to_s) hash.update(UUID.true_random) if (rand(2) == 0) end hash.update(performance.real.to_s) hash.update(performance.inspect) return UUID.convert_int_to_byte_string(hash.to_s[4..35].to_i(16), 16) end |
Instance Method Details
#<=>(other_uuid) ⇒ Object
Compares two UUIDs lexically
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/usesguid/uuidtools.rb', line 368 def <=>(other_uuid) check = self.time_low <=> other_uuid.time_low return check if check != 0 check = self.time_mid <=> other_uuid.time_mid return check if check != 0 check = self.time_hi_and_version <=> other_uuid.time_hi_and_version return check if check != 0 check = self.clock_seq_hi_and_reserved <=> other_uuid.clock_seq_hi_and_reserved return check if check != 0 check = self.clock_seq_low <=> other_uuid.clock_seq_low return check if check != 0 for i in 0..5 if (self.nodes[i] < other_uuid.nodes[i]) return -1 end if (self.nodes[i] > other_uuid.nodes[i]) return 1 end end return 0 end |
#hexdigest ⇒ Object
Returns the hex digest of the UUID object.
397 398 399 |
# File 'lib/usesguid/uuidtools.rb', line 397 def hexdigest return self.to_i.to_s(16) end |
#inspect ⇒ Object
Returns a representation of the object’s state
392 393 394 |
# File 'lib/usesguid/uuidtools.rb', line 392 def inspect return "#<UUID:0x#{self.object_id.to_s(16)} UUID:#{self.to_s}>" end |
#mac_address ⇒ Object
Returns the IEEE 802 address used to generate this UUID or nil if a MAC address was not used.
347 348 349 350 351 352 353 |
# File 'lib/usesguid/uuidtools.rb', line 347 def mac_address return nil if self.version != 1 return nil if self.random_node_id? return (self.nodes.collect do |node| sprintf("%2.2x", node) end).join(":") end |
#nil_uuid? ⇒ Boolean
Returns true if this UUID is the nil UUID (00000000-0000-0000-0000-000000000000).
293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/usesguid/uuidtools.rb', line 293 def nil_uuid? return false if self.time_low != 0 return false if self.time_mid != 0 return false if self.time_hi_and_version != 0 return false if self.clock_seq_hi_and_reserved != 0 return false if self.clock_seq_low != 0 self.nodes.each do |node| return false if node != 0 end return true end |
#random_node_id? ⇒ Boolean
This method applies only to version 1 UUIDs. Checks if the node ID was generated from a random number or from an IEEE 802 address (MAC address). Always returns false for UUIDs that aren’t version 1. This should not be confused with version 4 UUIDs where more than just the node id is random.
286 287 288 289 |
# File 'lib/usesguid/uuidtools.rb', line 286 def random_node_id? return false if self.version != 1 return ((self.nodes.first & 0x01) == 1) end |
#raw ⇒ Object
Returns the raw bytes that represent this UUID.
402 403 404 |
# File 'lib/usesguid/uuidtools.rb', line 402 def raw return UUID.convert_int_to_byte_string(self.to_i, 16) end |
#timestamp ⇒ Object
Returns the timestamp used to generate this UUID
356 357 358 359 360 361 362 363 364 365 |
# File 'lib/usesguid/uuidtools.rb', line 356 def return nil if self.version != 1 = 0 += ((self.time_hi_and_version & 0x0FFF) << 48) += (self.time_mid << 32) += self.time_low return Time.at( ( - 0x01B21DD213814000) / 10000000.0) end |
#to_i ⇒ Object
Returns an integer representation for this UUID.
417 418 419 420 421 422 423 424 425 |
# File 'lib/usesguid/uuidtools.rb', line 417 def to_i bytes = (time_low << 96) + (time_mid << 80) + (time_hi_and_version << 64) + (clock_seq_hi_and_reserved << 56) + (clock_seq_low << 48) for i in 0..5 bytes += (nodes[i] << (40 - (i * 8))) end return bytes end |
#to_s ⇒ Object
Returns a string representation for this UUID.
407 408 409 410 411 412 413 414 |
# File 'lib/usesguid/uuidtools.rb', line 407 def to_s result = sprintf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", @time_low, @time_mid, @time_hi_and_version, @clock_seq_hi_and_reserved, @clock_seq_low); for i in 0..5 result << sprintf("%2.2x", @nodes[i]) end return result end |
#to_s22 ⇒ Object
return a 22 byte URL-safe string, encoded six bits at a time using 64 characters
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/usesguid/uuid22.rb', line 12 def to_s22 integer=self.to_i rval='' 22.times do c=(integer & 0x3F) rval+=@@chars64[c] integer =integer >> 6 end return rval.reverse end |
#to_uri ⇒ Object
Returns a URI for this UUID.
428 429 430 |
# File 'lib/usesguid/uuidtools.rb', line 428 def to_uri return URI.parse(self.to_uri_string) end |
#to_uri_string ⇒ Object
Returns a URI string for this UUID.
433 434 435 |
# File 'lib/usesguid/uuidtools.rb', line 433 def to_uri_string return "urn:uuid:#{self.to_s}" end |
#valid? ⇒ Boolean
Returns true if this UUID is valid.
336 337 338 339 340 341 342 343 |
# File 'lib/usesguid/uuidtools.rb', line 336 def valid? if [0b000, 0b100, 0b110, 0b111].include?(self.variant) && (1..5).include?(self.version) return true else return false end end |
#variant ⇒ Object
Returns the UUID variant. Possible values: 0b000 - Reserved, NCS backward compatibility. 0b100 - The variant specified in this document. 0b110 - Reserved, Microsoft Corporation backward compatibility. 0b111 - Reserved for future definition.
322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/usesguid/uuidtools.rb', line 322 def variant variant_raw = (clock_seq_hi_and_reserved >> 5) result = nil if (variant_raw >> 2) == 0 result = 0x000 elsif (variant_raw >> 1) == 2 result = 0x100 else result = variant_raw end return (result >> 6) end |
#version ⇒ Object
Returns the UUID version type. Possible values: 1 - Time-based with unique or random host identifier 2 - DCE Security version (with POSIX UIDs) 3 - Name-based (MD5 hash) 4 - Random 5 - Name-based (SHA-1 hash)
312 313 314 |
# File 'lib/usesguid/uuidtools.rb', line 312 def version return (time_hi_and_version >> 12) end |