class Cql::Uuid

Represents a UUID value.

This is a very basic implementation of UUIDs and exists more or less just to encode and decode UUIDs from and to Cassandra.

If you want to generate UUIDs see {Cql::TimeUuid::Generator}.

Constants

EMPTY_STRING
HEX_RE
HYPHEN
RAW_FORMAT

Public Class Methods

new(n) click to toggle source

Creates a new UUID either from a string (expected to be on the standard 8-4-4-4-12 form, or just 32 characters without hyphens), or from a 128 bit number.

@raise [ArgumentError] if the string does not conform to the expected format

# File lib/cql/uuid.rb, line 18
def initialize(n)
  case n
  when String
    @n = from_s(n)
  else
    @n = n
  end
end

Public Instance Methods

==(other)
Alias for: eql?
eql?(other) click to toggle source

@private

# File lib/cql/uuid.rb, line 54
def eql?(other)
  other.respond_to?(:value) && self.value == other.value
end
Also aliased as: ==
hash() click to toggle source
# File lib/cql/uuid.rb, line 40
def hash
  @n.hash
end
to_i()
Alias for: value
to_s() click to toggle source

Returns a string representation of this UUID in the standard 8-4-4-4-12 form.

# File lib/cql/uuid.rb, line 29
def to_s
  @s ||= begin
    s = RAW_FORMAT % @n
    s.insert(20, HYPHEN)
    s.insert(16, HYPHEN)
    s.insert(12, HYPHEN)
    s.insert( 8, HYPHEN)
    s
  end
end
value() click to toggle source

Returns the numerical representation of this UUID

@return [Bignum] the 128 bit numerical representation

# File lib/cql/uuid.rb, line 48
def value
  @n
end
Also aliased as: to_i

Private Instance Methods

from_s(str) click to toggle source

See github.com/jruby/jruby/issues/1608

# File lib/cql/uuid.rb, line 68
def from_s(str)
  str = str.gsub(HYPHEN, EMPTY_STRING)
  raise ArgumentError, "Expected 32 hexadecimal digits but got #{str.length}" unless str.length == 32
  raise ArgumentError, "invalid value for Integer(): \"#{str}\"" unless str =~ HEX_RE
  Integer(str, 16)
end