Class SSHMenu::ClassMapper
In: lib/sshmenu.rb  (Git)
Parent: Object

The ClassMapper is a singleton object shared by all classes throughout the application. Its job is to map a symbolic name such as ‘’ to a class name such as SSHMenu::HostDialog.

It is possible to customise the behaviour of the application by ‘injecting’ mappings which cause different parts of the application to be built using custom classes.

It is not generally possible to modify a mapping after it has been injected since objects of the original class may already have been constructed. The reset_mappings method can be used to discard all known mappings, but this is really only useful to the regression test suite.


Included Modules


Public Instance methods

Takes a symbolic path name such as ‘’ and returns a class object such as SSHMenu::HostDialog. Throws a RuntimeError if the requested path is not mapped to a class.


# File lib/sshmenu.rb, line 92
    def get_class(path)
      return @class_map[path] if @class_map[path]
      raise RuntimeError, "Could not find class for path '#{path}' in:\n" + self

Used to define new mappings. Takes a hash of pathname => classname pairs.


# File lib/sshmenu.rb, line 106
    def inject(map)
      map.keys.each { |k| @class_map[k] = map[k] unless @class_map.key?(k) }

Discard all known mappings. Used by the regression tests to create a series of applications with test classes injected at different points.


# File lib/sshmenu.rb, line 84
    def reset_mappings
      @class_map = { }

Returns a dump of all mappings as a multi-line string - primarily for debugging.


# File lib/sshmenu.rb, line 100
    def to_str { |k| sprintf("%-20s %s\n", k, @class_map[k]) }.join("")