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 ‘app.dialog.host’ 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.

Methods

Included Modules

Singleton

Public Instance methods

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

[Source]

# 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
    end

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

[Source]

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

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

[Source]

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

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

[Source]

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

[Validate]