Class: Optio::ShortSwitchRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/optio/short_switch_registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShortSwitchRegistry

Returns a new instance of ShortSwitchRegistry.



8
9
10
11
# File 'lib/optio/short_switch_registry.rb', line 8

def initialize
  @registered = {}
  @pool = ('a'..'z').to_a
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



6
7
8
# File 'lib/optio/short_switch_registry.rb', line 6

def pool
  @pool
end

#registeredObject (readonly)

Returns the value of attribute registered.



5
6
7
# File 'lib/optio/short_switch_registry.rb', line 5

def registered
  @registered
end

Instance Method Details

#get(switch_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



13
14
15
16
17
18
19
20
# 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