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:
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’.
# 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
# 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:
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.
# 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
# File lib/sshmenu.rb, line 217 def Factory.mapper ClassMapper.instance end
Helper routine called by Factory.make_app to validate the supplied options and apply defaults.
# 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