Module: BetterUUID::InstanceMethods

Includes:
Comparable
Included in:
BetterUUID
Defined in:
lib/better-uuid/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object

UUIDs are comparable (don’t know what benefits are there, though).



81
82
83
# File 'lib/better-uuid/instance_methods.rb', line 81

def <=>(other)
  to_s <=> other.to_s
end

#==(other) ⇒ Object

Two UUIDs are said to be equal if and only if their (byte-order canonicalized) integer representations are equivallent. Refer RFC4122 for details.



75
76
77
# File 'lib/better-uuid/instance_methods.rb', line 75

def ==(other)
  to_i == other.to_i
end

#to_intObject Also known as: to_i

Convert into 128-bit unsigned integer Typically a Bignum instance, but can be a Fixnum.



55
56
57
58
59
60
# File 'lib/better-uuid/instance_methods.rb', line 55

def to_int
  tmp = self.raw_bytes.unpack 'C*'
  tmp.inject do |r, i|
    r * 256 | i
  end
end

#to_sObject Also known as: guid

Generate the string representation (a.k.a GUID) of this UUID



39
40
41
42
43
44
# File 'lib/better-uuid/instance_methods.rb', line 39

def to_s
  a = unpack
  tmp = a[-1].unpack 'C*'
  a[-1] = sprintf '%02x%02x%02x%02x%02x%02x', *tmp
  '%08x-%04x-%04x-%02x%02x-%s' % a
end

#to_uriObject Also known as: urn

Convert into a RFC4122-comforming URN representation



48
49
50
# File 'lib/better-uuid/instance_methods.rb', line 48

def to_uri
  'urn:uuid:' + self.to_s
end

#unpackObject

The ‘primitive deconstructor’, or the dual to pack. Note UUID.pack(uuid.unpack) == uuid



34
35
36
# File 'lib/better-uuid/instance_methods.rb', line 34

def unpack
  raw_bytes.unpack 'NnnCCa6'
end

#versionObject

Gets the version of this UUID returns nil if bad version



65
66
67
68
69
70
# File 'lib/better-uuid/instance_methods.rb', line 65

def version
  a = unpack
  v = (a[2] & 0xF000).to_s(16)[0].chr.to_i
  return v if (1..5).include? v
  return nil
end