rename metadata attributes to head/tail
[arvados.git] / lib / assign_uuid.rb
1 module AssignUuid
2
3   def self.included(base)
4     base.extend(ClassMethods)
5     base.validates_presence_of :uuid, :if => :respond_to_uuid?
6     base.validates_uniqueness_of :uuid, :if => :respond_to_uuid?
7     base.before_validation :assign_uuid
8   end
9
10   module ClassMethods
11     def uuid_prefix
12       Digest::MD5.hexdigest(self.to_s).to_i(16).to_s(36)[-5..-1]
13     end
14   end
15
16   protected
17
18   def respond_to_uuid?
19     self.respond_to? :uuid
20   end
21
22   def assign_uuid
23     return true if !self.respond_to_uuid?
24     self.uuid ||= [Server::Application.config.uuid_prefix,
25                    self.class.uuid_prefix,
26                    rand(2**256).to_s(36)[-15..-1]].
27       join '-'
28   end
29 end