Class: Trax::Model::UUID

Inherits:
String
  • Object
show all
Defined in:
lib/trax/model/uuid.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.===(val) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/trax/model/uuid.rb', line 7

def self.===(val)
  return false unless (val.is_a?(::Trax::Model::UUID) || val.is_a?(::String)) && val.length == 36
  return true if val.is_a?(::Trax::Model::UUID)

  #i.e. if we have 2 and 3 digit lengths, for value 'ABCDE' return ['AB', 'ABC']
  value_samples = prefix_lengths.map do |limit|
    val[0..(limit-1)]
  end

  return value_samples.any?{|sample| prefixes.include?(sample) }
end

.generate(prefix = nil) ⇒ Object



23
24
25
26
27
# File 'lib/trax/model/uuid.rb', line 23

def self.generate(prefix = nil)
  uuid = ::SecureRandom.uuid
  uuid[0..(prefix.length-1)] = prefix if prefix
  uuid
end

.klass_prefix_mapObject



19
20
21
# File 'lib/trax/model/uuid.rb', line 19

def self.klass_prefix_map
  prefix_map.invert
end

.prefix(prefix_value, klass) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/trax/model/uuid.rb', line 29

def self.prefix(prefix_value, klass)
  if prefix_map.has_key(:"#{prefix_value}") && prefix_map[:"#{prefix_value}"] == klass
    raise ::Trax::Model::Errors::DuplicatePrefix.new(prefix_value)
  end

  prefix_map[:"#{prefix_value}"] = klass
end

.prefix_lengthsObject



41
42
43
# File 'lib/trax/model/uuid.rb', line 41

def self.prefix_lengths
  @prefix_lengths ||= prefixes.map(&:length).uniq
end

.prefixesObject



37
38
39
# File 'lib/trax/model/uuid.rb', line 37

def self.prefixes
  @prefixes ||= ::Trax::Model::Registry.uuid_map.keys
end

.register(&block) ⇒ Object



45
46
47
# File 'lib/trax/model/uuid.rb', line 45

def self.register(&block)
  instance_exec(&block)
end

Instance Method Details

#recordObject



49
50
51
# File 'lib/trax/model/uuid.rb', line 49

def record
  @record ||= record_type ? record_type.find_by(:"#{record_type.unique_id_config.uuid_column}" => self) : nil
end

#record_typeObject



53
54
55
# File 'lib/trax/model/uuid.rb', line 53

def record_type
  @record_type ||= ::Trax::Model::Registry.model_type_for_uuid(self)
end