Class: GnUUID::UUID

Inherits:
Object
  • Object
show all
Defined in:
lib/gn_uuid/uuid.rb

Overview

handles UUID byte string and it’s conversion to different format NOTE: this is a general UUID class which is not limited to only v5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ UUID

Returns a new instance of UUID.



9
10
11
12
# File 'lib/gn_uuid/uuid.rb', line 9

def initialize(bytes)
  @bytes = bytes
  @ary = @bytes.unpack("C*")
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



7
8
9
# File 'lib/gn_uuid/uuid.rb', line 7

def bytes
  @bytes
end

Instance Method Details

#to_iObject



21
22
23
# File 'lib/gn_uuid/uuid.rb', line 21

def to_i
  @to_i ||= @ary.inject(0) { |a, e| a << 8 | e }
end

#to_sObject



14
15
16
17
18
19
# File 'lib/gn_uuid/uuid.rb', line 14

def to_s
  return @str if @str
  fmt = "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" \
        "%02x%02x%02x%02x%02x%02x"
  @str = fmt % @ary
end

#to_uriObject Also known as: inspect



25
26
27
# File 'lib/gn_uuid/uuid.rb', line 25

def to_uri
  "urn:uuid:" + to_s
end

#versionObject



31
32
33
# File 'lib/gn_uuid/uuid.rb', line 31

def version
  @ary[6] >> 4
end