Module: Taggable

Defined in:
app/models/base_new.rb

Overview

Sequal::Model plugin to inject the Taggable feature to the model class.

Taggable model supports the features below:

  • Taggable.uuid_prefix to both set and get uuid_prefix for the model.

  • Collision detection for specified uuid_prefix.

  • Generate unique value for :uuid column at initialization.

  • Add column :uuid if the model is capable of :schema plugin methods.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods Classes: UUIDPrefixDuplication

Constant Summary collapse

UUID_TABLE =
'abcdefghijklmnopqrstuvwxyz0123456789'.split('').freeze
UUID_REGEX =
%r/^(\w+)-([#{UUID_TABLE.join}]+)/

Class Method Summary collapse

Class Method Details

.configure(model) ⇒ Object



42
43
44
45
46
47
48
49
# File 'app/models/base_new.rb', line 42

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) ⇒ Boolean

Checks if the uuid object stored in the database.

Returns:

  • (Boolean)


38
39
40
# File 'app/models/base_new.rb', line 38

def self.exists?(uuid)
  !find(uuid).nil?
end

.find(uuid) ⇒ Object

Find a taggable model object from the given canonical uuid.

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

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

Raises:

  • (ArgumentError)


30
31
32
33
34
35
# File 'app/models/base_new.rb', line 30

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_collectionObject



18
19
20
# File 'app/models/base_new.rb', line 18

def self.uuid_prefix_collection
  @uuid_prefix_collection ||= {}
end