Class: Guid

Inherits:
Object
  • Object
show all
Includes:
Guid_Unix_, Guid_Win32_
Defined in:
lib/wwmd/guid.rb

Constant Summary

Constants included from Guid_Win32_

Guid_Win32_::CRYPT_VERIFYCONTEXT, Guid_Win32_::CryptAcquireContext, Guid_Win32_::CryptGenRandom, Guid_Win32_::CryptReleaseContext, Guid_Win32_::FORMAT_MESSAGE_FROM_SYSTEM, Guid_Win32_::FORMAT_MESSAGE_IGNORE_INSERTS, Guid_Win32_::FormatMessageA, Guid_Win32_::GetLastError, Guid_Win32_::PROV_RSA_FULL

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Guid_Unix_

#initialize

Methods included from Guid_Win32_

#initialize, #lastErrorMessage

Class Method Details

.from_raw(bytes) ⇒ Object

Raises:

  • (ArgumentError)


108
109
110
111
112
113
114
# File 'lib/wwmd/guid.rb', line 108

def self.from_raw(bytes)
  raise ArgumentError, "Invalid GUID raw bytes, length must be 16 bytes" unless
    bytes.length == 16
  guid = Guid.allocate
  guid.instance_eval { @bytes = bytes }
  guid
end

.from_s(s) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
# File 'lib/wwmd/guid.rb', line 100

def self.from_s(s)
  raise ArgumentError, "Invalid GUID hexstring" unless
    s =~ /\A[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}\z/i
  guid = Guid.allocate
  guid.instance_eval { @bytes = [s.gsub(/[^0-9a-f]+/i, '')].pack "h*" }
  guid
end

Instance Method Details

#==(other) ⇒ Object



116
117
118
# File 'lib/wwmd/guid.rb', line 116

def ==(other)
  @bytes == other.raw
end

#hexdigestObject



84
85
86
# File 'lib/wwmd/guid.rb', line 84

def hexdigest
  @bytes.unpack("h*")[0]
end

#inspectObject



92
93
94
# File 'lib/wwmd/guid.rb', line 92

def inspect
  to_s
end

#rawObject



96
97
98
# File 'lib/wwmd/guid.rb', line 96

def raw
  @bytes
end

#to_sObject



88
89
90
# File 'lib/wwmd/guid.rb', line 88

def to_s
  @bytes.unpack("h8 h4 h4 h4 h12").join "-"
end