Module: Dcmgr::Models::Taggable

Defined in:
lib/dcmgr/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

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



43
44
45
46
47
48
49
50
51
# File 'lib/dcmgr/models/base_new.rb', line 43

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
  }
  model.many_to_many :tags, :dataset=>lambda { Tag.join(TagMapping.table_name, :tag_id=>:id, :uuid=>self.canonical_uuid); }
end

.exists?(uuid) ⇒ Boolean

Checks if the uuid object stored in the database.

Returns:

  • (Boolean)


39
40
41
# File 'lib/dcmgr/models/base_new.rb', line 39

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)


31
32
33
34
35
36
# File 'lib/dcmgr/models/base_new.rb', line 31

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



19
20
21
# File 'lib/dcmgr/models/base_new.rb', line 19

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