Module: HBLText::IDMaker

Defined in:
lib/hbl_text/id_maker.rb

Constant Summary collapse

DEBUG =

constants

false
DEBUG2 =
false

Class Method Summary collapse

Class Method Details

.make_identifier(identifiable, n, **options) ⇒ Object

find a pretty unique n-letter identifier for an arbitrary string

string: (str) arbitrary string to create an identifier from

n:   (int)   identifier length

options:

refs:   (arr)   list of existing (occupied) identifiers
suffix: (str)   suffix to add to create more choices for identifier creation
                f.i. "123456789"
verbose: (bool) flag should output just identifier or id with meta info
                meta info: [id, method no., uggliness]

remarks:

identifier with only letters:

3 letters:  26^3 =  17.576   possible unique combinations
4 letters:  26^4 = 456.976   possible unique combinations

letters and numbers:

3 digits:   36^3 =    46.656 possible unique combinations
4 digits:   36^4 = 1.679.616 possible unique combinations


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hbl_text/id_maker.rb', line 56

def make_identifier( identifiable, n, **options)

  refs     = options[:refs]    || []
  suffix   = options[:suffix]  || ""
  verbose  = options[:verbose]
  
  (0..(suffix.length + 1)).each do |pos|
    ref, uggliness, method = find_identifier( identifiable, n, refs, suffix[0..pos])
    Rails.logger.info [ref, method, sprintf("%04d", uggliness.presence || 9999), identifiable].map(&:to_s).join(" : ") if DEBUG || DEBUG2
    if ref.present?
      return verbose ? [ref, method, uggliness] : ref
    end
  end
  
  return verbose ? [nil, nil, nil] : nil
end