module RUUID::Generator

Generator is responsible for generating new UUIDs. Implementors can include and override methods to provide new UUID variants.

Attributes

source[RW]
version[RW]

Public Class Methods

included(base) click to toggle source
Calls superclass method
# File lib/ruuid/generator.rb, line 7
def self.included(base)
  super
  base.extend ClassMethods
  base.include Singleton
end
new() click to toggle source
# File lib/ruuid/generator.rb, line 29
def initialize
  @version = 0
end

Public Instance Methods

generate() click to toggle source
# File lib/ruuid/generator.rb, line 33
def generate
  raw_bytes.tap { |bytes|
    # Mask version
    bytes[6] = (bytes[6] & 0x0f) | (version << 4)
    # Mask variant
    bytes[8] = (bytes[8] & 0x3f) | 0x80
  }.collect(&:chr).join
end

Private Instance Methods

raw_bytes() click to toggle source
# File lib/ruuid/generator.rb, line 44
def raw_bytes
  unless source.respond_to?(:call)
    raise NotImplementedError, "#{self.class} does not specify a data source"
  end
  source.call.bytes
end