class Terrestrial::Cli::Bootstrapper::IdGenerator

Constants

MAX_IDENTIFIER_LENGTH

Public Class Methods

generate(string) click to toggle source
# File lib/terrestrial/cli/bootstrapper.rb, line 181
def generate(string)
  id = do_generate_id(string)

  attempt = 1
  while id_already_exists?(id)
    id = increment_id(id, attempt)
    attempt += 1
  end
  id_history << id
  id
end
reset!() click to toggle source
# File lib/terrestrial/cli/bootstrapper.rb, line 193
def reset!
  @history = []
end

Private Class Methods

do_generate_id(string) click to toggle source
# File lib/terrestrial/cli/bootstrapper.rb, line 208
def do_generate_id(string)
  string
    .gsub(/%\d\$@/, '')
    .gsub(/[^0-9a-z ]/i, '')
    .split(" ")[0..(MAX_IDENTIFIER_LENGTH - 1)]
    .join("_")
    .upcase
end
id_already_exists?(id) click to toggle source
# File lib/terrestrial/cli/bootstrapper.rb, line 217
def id_already_exists?(id)
  id_history.any? {|previous| previous == id }
end
id_history() click to toggle source
# File lib/terrestrial/cli/bootstrapper.rb, line 221
def id_history
  @history ||= []
end
increment_id(id, attempt) click to toggle source
# File lib/terrestrial/cli/bootstrapper.rb, line 199
def increment_id(id, attempt)
  if id[-1] == (attempt).to_s
    id[-1] = (attempt + 1).to_s
    id
  else
    id << "_#{attempt + 1}"
  end
end