Class: MaskedIdentifier::CodeGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/code-generator.rb

Defined Under Namespace

Classes: InvalidAttemptsValue, InvalidCharset, InvalidCodeLength, TooManyFailedAttempts

Class Method Summary collapse

Class Method Details

.unique_code(klass, property, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/code-generator.rb', line 23

def self.unique_code(klass, property, options = {})
  attempts = options[:attempts] || 20
  raise InvalidAttemptsValue, 'Attempts value must be an integer >= 1' if !attempts.is_a?(Integer) || attempts <= 0

  begin
    i ||= 0; i += 1
    raise TooManyFailedAttempts, 'Failed to find a unique code' if i > attempts
    code = self.random_code length: options[:length], charset: options[:charset]
  end until self.code_is_unique? klass, property, code
  return code
end