Module: Dcmgr::Models::Taggable::ClassMethods

Defined in:
lib/dcmgr/models/base_new.rb

Instance Method Summary collapse

Instance Method Details

#[](*args) ⇒ Object

Override Model.[] to add lookup by uuid.

Examples:

Account['a-xxxxxx']


171
172
173
174
175
176
177
# File 'lib/dcmgr/models/base_new.rb', line 171

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

Checks the general uuid syntax



194
195
196
# File 'lib/dcmgr/models/base_new.rb', line 194

def check_trimmed_uuid_format(uuid)
  uuid.match(/^[a-z0-9]*$/) && uuid.length <= 255
end

#check_uuid_format(uuid) ⇒ Object

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



199
200
201
# File 'lib/dcmgr/models/base_new.rb', line 199

def check_uuid_format(uuid)
  uuid =~ /^#{self.uuid_prefix}-/
end

#trim_uuid(p_uuid) ⇒ Object

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

Examples:

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

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

Account.trim_uuid('u-abcd1234') # 'u-' prefix is for User model.

Raises:



185
186
187
188
189
190
191
# File 'lib/dcmgr/models/base_new.rb', line 185

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

Getter and setter for uuid_prefix of the class.

Examples:

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

Model1.uuid_prefix # == 'm'
Model1.new.canonical_uuid # == 'm-abcd1234'


155
156
157
158
159
160
161
162
163
164
# File 'lib/dcmgr/models/base_new.rb', line 155

def uuid_prefix(prefix=nil)
  if prefix
    raise UUIDPrefixDuplication, "Found collision for uuid_prefix key: #{prefix}" if Taggable.uuid_prefix_collection.has_key?(prefix)
    
    Taggable.uuid_prefix_collection[prefix]={:class=>self}
    @uuid_prefix = prefix
  end

  @uuid_prefix || (superclass.uuid_prefix if superclass.respond_to?(:uuid_prefix)) || raise("uuid prefix is unset for:#{self}")
end