Class: Guid

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

Overview

Guid - Ruby library for portable GUID/UUID generation.

Copyright © 2004 David Garamond <davegaramond at icqmail com>

This library is free software; you can redistribute it and/or modify it under the same terms as Ruby itself.

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)


114
115
116
117
118
119
120
# File 'lib/guid.rb', line 114

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)


106
107
108
109
110
111
112
# File 'lib/guid.rb', line 106

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

.win32?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/guid.rb', line 12

def win32?
  RUBY_PLATFORM =~ /[^r]win/i
end

Instance Method Details

#==(other) ⇒ Object



122
123
124
# File 'lib/guid.rb', line 122

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

#hexdigestObject



90
91
92
# File 'lib/guid.rb', line 90

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

#inspectObject



98
99
100
# File 'lib/guid.rb', line 98

def inspect
  to_s
end

#rawObject



102
103
104
# File 'lib/guid.rb', line 102

def raw
  @bytes
end

#to_sObject



94
95
96
# File 'lib/guid.rb', line 94

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