Class: RTunnel::Command::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/rtunnel/commands.rb

Overview

Associates command codes with the classes implementing them.

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



6
7
8
9
# File 'lib/rtunnel/commands.rb', line 6

def initialize
  @classes = {}
  @codes = {}
end

Instance Method Details

#class_for(command_code) ⇒ Object



20
21
22
# File 'lib/rtunnel/commands.rb', line 20

def class_for(command_code)
  @classes[command_code]
end

#code_for(klass) ⇒ Object



24
25
26
# File 'lib/rtunnel/commands.rb', line 24

def code_for(klass)
  @codes[klass]
end

#codes_and_classesObject



28
29
30
31
32
# File 'lib/rtunnel/commands.rb', line 28

def codes_and_classes
  ret_val = []
  @codes.each { |klass, code| ret_val << [code, klass.name] }
  ret_val.sort!
end

#register(klass, command_code) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/rtunnel/commands.rb', line 11

def register(klass, command_code)
  if @codes.has_key? command_code
    raise "Command code #{command_code} already used for #{@codes[command_code].name}"
  end
  
  @codes[klass] = command_code
  @classes[command_code] = klass
end