module Sequel::Plugins::UUIDPrefix::ClassMethods

Public Instance Methods

[](*args) click to toggle source

Override Model.[] to add lookup by uuid.

@example

Account['a-xxxxxx']
Calls superclass method
# File lib/sequel/plugins/uuid_prefix.rb, line 161
def [](*args)
  if args.size == 1 and args[0].is_a? String
    super(:uuid=>trim_uuid(args[0]))
  else
    super(*args)
  end
end
check_trimmed_uuid_format(uuid) click to toggle source

Checks the general uuid syntax

# File lib/sequel/plugins/uuid_prefix.rb, line 192
def check_trimmed_uuid_format(uuid)
  uuid.match(/^[\w]+$/) && uuid.length <= 255
end
check_uuid_format(uuid) click to toggle source

Checks the uuid syntax if it is for the UUIDPrefix class.

# File lib/sequel/plugins/uuid_prefix.rb, line 197
def check_uuid_format(uuid)
  uuid =~ /^#{self.uuid_prefix}-/
end
dataset_where_uuid(p_uuid) click to toggle source

Returns dataset which has been selected for the uuid.

@example

Account.dataset_where_uuid('a-xxxxxx')
# File lib/sequel/plugins/uuid_prefix.rb, line 173
def dataset_where_uuid(p_uuid)
  dataset.where(uuid: trim_uuid(p_uuid))
end
trim_uuid(p_uuid) click to toggle source

Returns the uuid string which is removed prefix part: /^(:?w+)-/.

@example

Account.trim_uuid('a-abcd1234') # = 'abcd1234'

@example Will get InvalidUUIDError as the uuid with invalid prefix has been tried.

Account.trim_uuid('u-abcd1234') # 'u-' prefix is for User model.
# File lib/sequel/plugins/uuid_prefix.rb, line 183
def trim_uuid(p_uuid)
  regex = %r/^#{self.uuid_prefix}-/
  if p_uuid and p_uuid =~ regex
    return p_uuid.sub(regex, '')
  end
  raise InvalidUUIDError, "Invalid uuid or unsupported uuid: #{p_uuid} in #{self}"
end
uuid_prefix(prefix=nil) click to toggle source

Getter and setter for uuid_prefix of the class.

@example

class Model1 < Sequel::Model
  plugin UUIDPrefix
  uuid_prefix('m')
end

Model1.uuid_prefix # == 'm'
Model1.new.canonical_uuid # == 'm-abcd1234'
# File lib/sequel/plugins/uuid_prefix.rb, line 141
def uuid_prefix(prefix=nil)
  if prefix
    if UUIDPrefix.uuid_prefix_collection.has_key?(prefix)
      raise UUIDPrefixDuplication, "Found collision for uuid_prefix key: #{prefix}"
    end

    UUIDPrefix.uuid_prefix_collection[prefix]={:class=>self}
    @uuid_prefix = prefix
  end

  @uuid_prefix ||
    (superclass.uuid_prefix if superclass.respond_to?(:uuid_prefix)) ||
    raise(UnsetUUIDPrefix, "uuid prefix is unset for: #{self}")
end
valid_uuid_syntax?(uuid) click to toggle source
# File lib/sequel/plugins/uuid_prefix.rb, line 201
def valid_uuid_syntax?(uuid)
  uuid =~ /^#{self.uuid_prefix}-[\w]+/
end