Module: Dcmgr::Models::UUIDMethods

Included in:
Base
Defined in:
lib/dcmgr/models/base.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#before_createObject



41
42
43
# File 'lib/dcmgr/models/base.rb', line 41

def before_create
  setup_uuid
end

#generate_uuidObject



33
34
35
# File 'lib/dcmgr/models/base.rb', line 33

def generate_uuid
  "%08x" % rand(16 ** 8)
end

#save(*columns) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/dcmgr/models/base.rb', line 45

def save(*columns)
  super
rescue Sequel::DatabaseError => e
  Dcmgr.logger.info "db error: %s" % e
  Dcmgr.logger.info "  " + e.backtrace.join("\n  ")

  raise DuplicateUUIDError if /^Mysql::Error: Duplicate/ =~ e.message
  raise e
end

#setup_uuidObject



37
38
39
# File 'lib/dcmgr/models/base.rb', line 37

def setup_uuid
  self.uuid = generate_uuid unless self.values[:uuid]
end

#to_sObject



59
60
61
# File 'lib/dcmgr/models/base.rb', line 59

def to_s
  uuid
end

#uuidObject



55
56
57
# File 'lib/dcmgr/models/base.rb', line 55

def uuid
  "%s-%s" % [self.class.prefix_uuid, self.values[:uuid]]
end