Class: GuidSpanner::Guid

Inherits:
Object
  • Object
show all
Defined in:
lib/guid_spanner/guid.rb

Overview

Class to convert from 32 character guids to 36 character guids and back

Class Method Summary collapse

Class Method Details

.pack_to_32(guid_str) ⇒ Object

class method to convert 36-char guid to 32-char version



15
16
17
18
19
# File 'lib/guid_spanner/guid.rb', line 15

def self.pack_to_32(guid_str)
  return guid_str if valid_32?(guid_str)
  raise GuidSpanner::Exceptions::InvalidUuidFormatError unless valid_36?(guid_str)
  guid_str.delete('-')
end

.unpack_to_36(guid_str) ⇒ Object

class method to convert 32-char guid to 36-char version



8
9
10
11
12
# File 'lib/guid_spanner/guid.rb', line 8

def self.unpack_to_36(guid_str)
  raise GuidSpanner::Exceptions::InvalidUuidFormatError unless valid_32?(guid_str) || valid_36?(guid_str)
  return guid_str if guid_str.length == 36
  guid_str.unpack('A8A4A4A4A12').join('-')
end

.valid_32?(guid_str) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/guid_spanner/guid.rb', line 22

def valid_32?(guid_str)
  res = guid_str =~ /^([0-9A-Fa-f]{8}[0-9A-Fa-f]{4}[0-9A-Fa-f]{4}[0-9A-Fa-f]{4}[0-9A-Fa-f]{12})$/
  res ? true : false
end

.valid_36?(guid_str) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/guid_spanner/guid.rb', line 27

def valid_36?(guid_str)
  res = guid_str =~ /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/
  res ? true : false
end