module Sequel::Plugins::UUIDPrefix

Sequel::Model plugin to inject the UUIDPrefix feature to the model class.

UUIDPrefix model supports the features below:

Constants

UUID_REGEX
UUID_TABLE

Public Class Methods

apply(model, _options = {}) click to toggle source
# File lib/sequel/plugins/uuid_prefix.rb, line 23
def self.apply(model, _options = {})
  model.plugin Sequel::Plugins::AfterInitialize
end
configure(model) click to toggle source
# File lib/sequel/plugins/uuid_prefix.rb, line 51
def self.configure(model)
  model.schema_builders << proc {
    unless has_column?(:uuid)
      # add :uuid column with unique index constraint.
      column(:uuid, String, :size=>8, :null=>false, :fixed=>true, :unique=>true)
    end
  }
end
exists?(uuid) click to toggle source

Checks if the uuid object stored in the database.

# File lib/sequel/plugins/uuid_prefix.rb, line 47
def self.exists?(uuid)
  !find(uuid).nil?
end
find(uuid) click to toggle source

Find a model with an prefixed uuid object from the given canonical uuid.

# Find an account. UUIDPrefix.find(‘a-xxxxxxxx’)

# Find a user. UUIDPrefix.find(‘u-xxxxxxxx’)

# File lib/sequel/plugins/uuid_prefix.rb, line 39
def self.find(uuid)
  raise ArgumentError, "Invalid uuid syntax: #{uuid}" unless uuid =~ UUID_REGEX
  upc = uuid_prefix_collection[$1.downcase]
  raise "Unknown uuid prefix: #{$1.downcase}" if upc.nil?
  upc[:class].find(:uuid=>$2)
end
uuid_prefix_collection() click to toggle source
# File lib/sequel/plugins/uuid_prefix.rb, line 27
def self.uuid_prefix_collection
  @uuid_prefix_collection ||= {}
end