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

Methods in this class are called from a wrapper script to create an application object. The wrapper script will typically follow this sequence:

Methods

Public Class methods

Scans through the supplied command-line arguments and extracts the config file name if supplied. This is done early because the ‘app’ class is responsible for handling command line arguments but the config file might contain a class mapping for ‘app’.

[Source]

# File lib/sshmenu.rb, line 210
    def Factory.extract_config_file(args = [])
      i = args.index('--config-file') || args.index('-c') || return
      return args[i+1]
    end

A proxy for SSHMenu::ClassMapper.inject

[Source]

# File lib/sshmenu.rb, line 223
    def Factory.inject_defaults
        mapper.inject('app' => SSHMenu::App)
    end

Used to create an application object. The object will be of the class associated with the path ‘app’ in the ClassMapper (default: SSHMenu::App).

This method accepts a hash of options - all of which may be omitted. The following options are recognised:

  • :window - a Gtk::Window into which the application UI (essentially a single button) will be packed
  • :model_class - the class which should be used to handle reading and writing the config file (default is SSHMenu::Config)
  • :args - command-line arguments passed to the wrapper script (ARGV will be used by default)

The wrapper script can optionally supply a block to this method. When the user selects a host from the menu, the block will be invoked instead of the default launch method and will be passed the host object.

Any exceptions generated during construction of the application will be trapped. If possible, the error details will be displayed in a popup window since the STDERR of a panel applet is not typically visible to a user.

[Source]

# File lib/sshmenu.rb, line 152
    def Factory.make_app(*args, &launch_proc)
      begin
        options = parse_options(args)
        mapper.inject('app.model' => options[:model_class])
        config_file = extract_config_file(options[:args])
        config = mapper.get_class('app.model').new(:filename => config_file)

        inject_defaults
        return mapper.get_class('app').new(config, options, launch_proc)
      rescue SystemExit
        exit
      rescue ShowVersionException
        puts "SSHMenu Version #{SSHMenu.version}"
        exit
      rescue Exception => detail
        mapper.inject('app' => SSHMenu::App)
        mapper.get_class('app').fatal_error(detail)
        exit
      end
    end

Returns the ClassMapper singleton object

[Source]

# File lib/sshmenu.rb, line 217
    def Factory.mapper
      ClassMapper.instance
    end

Sets allowable options and default values for options passed to make_app. and apply defaults.

[Source]

# File lib/sshmenu.rb, line 197
    def Factory.option_defaults
      return {
        :window         => nil,
        :model_class    => SSHMenu::Config,
        :args           => ARGV
      }
    end

Helper routine called by Factory.make_app to validate the supplied options and apply defaults.

[Source]

# File lib/sshmenu.rb, line 176
    def Factory.parse_options(args)
      default = option_defaults
      if args[0].is_a?(Hash)
        args = args[0]
      else
        args = { :window => args[0], :model_class => args[1] }
      end
      options = {}
      args.each do |k,v|
        raise "Unrecognised option '#{k}'" unless default.has_key?(k)
        options[k] = v
      end
      default.each do |k,v|
        options[k] = v if options[k].nil?
      end
      return options
    end

[Validate]