class Optio::ShortSwitchRegistry

Attributes

pool[R]
registered[R]

Public Class Methods

new() click to toggle source
# File lib/optio/short_switch_registry.rb, line 8
def initialize
  @registered = {}
  @pool = ('a'..'z').to_a
end

Public Instance Methods

get(switch_name) click to toggle source
# File lib/optio/short_switch_registry.rb, line 22
def get(switch_name)
  switch_list = switch_name.to_s.chars.to_a.uniq
  chosen_switch = switch_list.find do |short_switch|
    @pool.include?(short_switch)
  end
  if !chosen_switch
    chosen_switch = random_short_switch
  end
  @pool.delete(chosen_switch)
  @registered[switch_name] = chosen_switch
  chosen_switch
end
register(switch_name, short_name) click to toggle source
# File lib/optio/short_switch_registry.rb, line 13
def register(switch_name, short_name)
  if !@pool.include?(short_name)
    raise Exceptions::ShortSwitchAlreadyExistsError,
      "#{short_name} already registered with #{switch_name}"
  end
  @pool.delete(short_name)
  @registered[switch_name] = short_name
end

Private Instance Methods

random_short_switch() click to toggle source
# File lib/optio/short_switch_registry.rb, line 37
def random_short_switch
  @pool.shuffle.first
end