class Rubeyond::UUID

The UUID class handles representing a universally unique identifier.

Examples

# Create a new unique UUID.
@uuid = Rubeyond::UUID.create

# Create an instance of UUID from a uuid value.
@uuid = Rubeyond::UUID.create :uuid => "fd0289a5-8eec-4a08-9283-81d02c9d2fff"

# Create an instance of UUID from an encoded value.
@uuid = Rubeyond::UUID.create :encoded => 336307859334295828133695192821923655679

Attributes

encoded[R]

The encoded value of the UUID.

uuid[R]

A string representation of the UUID.

Public Class Methods

create(options = {}) click to toggle source

Creates a new UUID instance.

If provided, it will create a new instances with the given uuid value.

Attributes

  • options - creation options

Options

  • +:uuid: - the UUID value to use

  • +:encoded: - an encoded UUID

# File lib/rubeyond/uuid.rb, line 55
def self.create(options = {})
  # if we have a uuid then create from that
  if options[:uuid]
    Rubeyond::UUID.new(options[:uuid])
  elsif options[:encoded]
    uuid = decode(options[:encoded])
    Rubeyond::UUID.new(uuid)
  else
    Rubeyond::UUID.new
  end
end

Private Class Methods

encode(uuid) click to toggle source

Encodes the value of the UUID.

# File lib/rubeyond/uuid.rb, line 101
def self.encode(uuid)
  # ensure that the UUID is in the right format
  # xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
  if uuid =~ /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/
    (uuid[0, 8] + uuid[9, 4] + uuid[14, 4] + uuid[19, 4] + uuid[24, 12]).to_i(16)
  else
    raise ArgumentError, "Invalid UUID: #{@uuid}"
  end
end

Public Instance Methods

to_s() click to toggle source

Returns a string representation of the UUID.

# File lib/rubeyond/uuid.rb, line 84
def to_s
  @uuid
end