Class: Guid

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

Constant Summary collapse

@@d36 =
('a'..'z').to_a + ('0'..'9').to_a
@@rd36 =
{}

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_base36(val) ⇒ Object

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rack_direct/guid.rb', line 127

def Guid.from_base36(val)
  val = val.downcase
  raise ArgumentError unless val =~ /\A[a-z][a-z0-9]{24}\z/
  n = 0
  mult = 1
  val.reverse.each_byte {|c|
    n += @@rd36[c] * mult
    mult *= 36
  }
  Guid.from_i(n)
end

.from_hex(s) ⇒ Object

Raises:

  • (ArgumentError)


139
140
141
142
143
144
145
# File 'lib/rack_direct/guid.rb', line 139

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

.from_i(val) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rack_direct/guid.rb', line 148

def Guid.from_i(val)
  bytes = [
      (val & 0xffffffff000000000000000000000000) >> 96,
      (val & 0x00000000ffffffff0000000000000000) >> 64,
      (val & 0x0000000000000000ffffffff00000000) >> 32,
      (val & 0x000000000000000000000000ffffffff)
    ].pack('NNNN')
  guid = Guid.allocate
  guid.instance_eval { @bytes = bytes }
  guid
end

.from_raw(bytes) ⇒ Object

Raises:

  • (ArgumentError)


108
109
110
111
112
113
114
# File 'lib/rack_direct/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/rack_direct/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/rack_direct/guid.rb', line 116

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

#hexdigestObject Also known as: to_hex



82
83
84
# File 'lib/rack_direct/guid.rb', line 82

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

#inspectObject



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

def inspect
  to_s
end

#rawObject



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

def raw
  @bytes
end

#to_base36Object



167
168
169
170
# File 'lib/rack_direct/guid.rb', line 167

def to_base36
  self.to_i.to_s(36).tr('0-9a-z', 'a-z0-9').rjust(25, 'a')
  # self.to_s.tr('0-9a-z', 'a-z0-9').rjust(25, 'a')
end

#to_iObject



160
161
162
163
164
165
# File 'lib/rack_direct/guid.rb', line 160

def to_i
   (@bytes[ 0 ..  3].unpack('N')[0] << 96) +
   (@bytes[ 4 ..  7].unpack('N')[0] << 64) +
   (@bytes[ 8 .. 11].unpack('N')[0] << 32) +
   (@bytes[12 .. 15].unpack('N')[0])
end

#to_sObject



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

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