Module: UniqueId::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/unique_id/base.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#generate_uniqueObject



32
33
34
35
# File 'lib/unique_id/base.rb', line 32

def generate_unique
  value = unique_format(self.class.unique.next(scope:self.unique_scope, type:self.unique_type))
  write_attribute self.class.unique.attribute, value
end

#unique_format(value) ⇒ Object



25
26
27
28
29
30
# File 'lib/unique_id/base.rb', line 25

def unique_format(value)
  if f = self.class.unique.formatter
    return self.instance_exec(self.unique_scope, value, &f)
  end
  value
end

#unique_scopeObject



15
16
17
18
19
20
21
22
23
# File 'lib/unique_id/base.rb', line 15

def unique_scope
  if scope = self.class.unique.scope
    @unique_scope ||= case scope
      when Symbol then self.send(scope)
      when Proc   then self.instance_exec(&scope)
      else scope
    end
  end
end

#unique_typeObject



11
12
13
# File 'lib/unique_id/base.rb', line 11

def unique_type
  self.class.to_s
end