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

The SSHMenu::Config class implements the data model for the application. It is responsible for:

  • reading the configuration file
  • maintaining an in-memory representation of the menu items and option settings
  • writing the config file if changes are made via the preferences dialog

The ClassMapper is used to delegate chunks of functionality to different classes as follows:

‘app.model.item’ => SSHMenu::Item
base class for menu items (including separators)
‘app.model.hostitem’ => SSHMenu::HostItem
host menu items
‘app.model.menuitem’ => SSHMenu::MenuItem
sub-menu items
‘app.model.autoconf’ => SSHMenu::SetupWizard
initial setupwizard

Methods

Constants

DefaultTooltip = 'Open an SSH session in a new window'

Attributes

filename  [R]  pathname of user‘s config file ($HOME/.sshmenu)

Public Class methods

Called from SSHMenu::Factory#make_app. Calls load_classes and inject_defaults to set up any class mapping overrides defined in the config file.

[Source]

# File lib/sshmenu.rb, line 1226
    def initialize(args = {})
      @globals    = { 'tooltip' => DefaultTooltip }
      @menu_items = [ ]
      @home_dir   = nil # suppress warning message
      @timestamp  = nil
      @classes    = { }

      if args[:filename]
        @filename = args[:filename]
      else
        @filename = home_dir + '.sshmenu'
      end

      load_classes
      inject_defaults
    end

Public Instance methods

Takes a host item, appends it to the main menu and saves the result

[Source]

# File lib/sshmenu.rb, line 1503
    def append_host(item)
      @menu_items.push(item)
      self.save
    end

Called to invoke the setup wizard

[Source]

# File lib/sshmenu.rb, line 1292
    def autoconfigure
      wizard = mapper.get_class('app.model.autoconf')
      a = wizard.new.autoconfigure(self) or return
      set_items_from_array(a)
    end

Sets the state of the ‘Backup on save’ option

[Source]

# File lib/sshmenu.rb, line 1476
    def back_up_config=(val)
      set('back_up_config', val ? 1 : 0)
    end

Returns true if the ‘Backup on save’ option is enabled

[Source]

# File lib/sshmenu.rb, line 1467
    def back_up_config?
      if opt = get('back_up_config')
        return opt != 0
      end
      return false
    end

Iterator for walking the tree of menu items as a depth first traversal.

[Source]

# File lib/sshmenu.rb, line 1510
    def each_item(&action)  # :yields: parent_items, item
      parents = []
      iterate_items(@menu_items, parents, action)
    end

Gets the value of an attribute in the ‘globals’ config section

[Source]

# File lib/sshmenu.rb, line 1388
    def get(key, default = nil)
      return default unless @globals.has_key?(key)
      return @globals[key]
    end

Sets the state of the ‘hide button border’ option

[Source]

# File lib/sshmenu.rb, line 1416
    def hide_border=(val)
      set('hide_border', val ? 1 : 0)
    end

Returns true if the ‘hide button border’ option is enabled

[Source]

# File lib/sshmenu.rb, line 1407
    def hide_border?
      if opt = get('hide_border')
        return opt != 0
      end
      return false
    end

Returns the user‘s home directory which will be determined either from the $HOME environment variable or from the user‘s entry in /etc/passwd.

[Source]

# File lib/sshmenu.rb, line 1264
    def home_dir
      return @home_dir unless @home_dir.nil?
      if ENV['HOME']
        return @home_dir = Pathname.new(ENV['HOME'])
      end
      require 'etc'
      if name = Etc.getlogin
        info = Etc.getpwnam(name)
        return @home_dir = Pathname.new(info.dir) if info.dir
      end
      raise "$HOME is not defined"
    end

Returns a Host item for the supplied name, either by locating the first host definition with a title match, or by using the name as a hostname

[Source]

# File lib/sshmenu.rb, line 1494
    def host_by_name(name)
      each_item() do |parents, item|
        return item if item.host? and item.title == name
      end
      return host_from_text(name)
    end

Creates and returns a Host item from a simple hostname string

[Source]

# File lib/sshmenu.rb, line 1482
    def host_from_text(text)
      item_class = mapper.get_class('app.model.item')
      return item_class.new_from_hash({
        'type'      => 'host',
        'title'     => text,
        'sshparams' => text
      })
    end

Called from the constructor to set up default class mappings for menu item classes and the setup wizard.

[Source]

# File lib/sshmenu.rb, line 1252
    def inject_defaults
      mapper.inject(
        'app.model.item'     => SSHMenu::Item,
        'app.model.hostitem' => SSHMenu::HostItem,
        'app.model.menuitem' => SSHMenu::MenuItem,
        'app.model.autoconf' => SSHMenu::SetupWizard
      )
    end

Reads the config file and creates an in-memory representation of the configuration. May be called multiple times during the life of the process (e.g.: if the file is modified). Any config read from the file will replace in-memory config data.

[Source]

# File lib/sshmenu.rb, line 1329
    def load
      if not_configured?
        save
        return
      end

      mtime = File.mtime(@filename)
      if !@timestamp.nil?
        return if mtime == @timestamp
      end

      config   = YAML.load_file(@filename) || {}

      @globals = config['global']  || {}
      @classes = config['classes'] || {}

      a = config['items'] || config['item'] || []
      set_items_from_array(a)

      @globals['tooltip'] ||= DefaultTooltip

      @timestamp = mtime
    end

Reads the ‘classes’ section from the config file. If a ‘require’ key is defined, the specified file is ‘required’. Any remaining keys are passed to the SSHMenu::ClassMapper. The remainder of the config file is ignored by this routine.

[Source]

# File lib/sshmenu.rb, line 1303
    def load_classes
      classes = nil
      begin
        config  = YAML.load_file(@filename)
        classes = config['classes'] or return
      rescue
        return
      end
      if source_file = classes.delete('require')
        begin
          require source_file
        rescue Exception => detail
          raise "Error in 'require': #{detail}"
        end
      end
      classes.each do |k,v|
        cls = eval "class #{v}\nend\n#{v}" # turn string into a Class
        mapper.inject(k => cls)
      end
    end

Called to copy the config file to .sshmenu.bak before the original is overwritten.

[Source]

# File lib/sshmenu.rb, line 1381
    def make_backup_copy
      return unless File.exists?(@filename)
      File.syscopy(@filename, @filename.to_s + '.bak')
    end

Accessor for the SSHMenu::ClassMapper singleton object

[Source]

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

Sets the state of the ‘Open all windows’ option

[Source]

# File lib/sshmenu.rb, line 1446
    def menus_open_all=(val)
      set('menus_open_all', val ? 1 : 0)
    end

Returns true if the ‘Open all windows’ option is enabled

[Source]

# File lib/sshmenu.rb, line 1437
    def menus_open_all?
      if opt = get('menus_open_all')
        return opt != 0
      end
      return false
    end

Sets the state of the ‘tear-off menus’ option

[Source]

# File lib/sshmenu.rb, line 1431
    def menus_tearoff=(val)
      set('menus_tearoff', val ? 1 : 0)
    end

Returns true if the ‘tear-off menus’ option is enabled

[Source]

# File lib/sshmenu.rb, line 1422
    def menus_tearoff?
      if opt = get('menus_tearoff')
        return opt != 0
      end
      return false
    end

Returns true if the .sshmenu config file has not been created yet.

[Source]

# File lib/sshmenu.rb, line 1286
    def not_configured?
      return !File.exists?(@filename)
    end

Serialises the menu items and global settings to hashes and writes them all back out to the config file in YAML format.

[Source]

# File lib/sshmenu.rb, line 1364
    def save
      make_backup_copy if back_up_config?
      fh = File.new(@filename, 'w')
      config = {
        'global'  => @globals,
        'classes' => @classes,
        'items'   => @menu_items.map { |i| i.to_h }
      }
      fh.print YAML.dump(config)
      fh.close
      mtime = File.mtime(@filename)
      @timestamp = mtime
    end

Sets the value of an attribute in the ‘globals’ config section

[Source]

# File lib/sshmenu.rb, line 1395
    def set(key, value)
      @globals[key] = value
    end

Used to override the default config file (e.g.: call from SSHMenu::App#set_config_file during commandline option parsing).

[Source]

# File lib/sshmenu.rb, line 1280
    def set_config_file(file)
      @filename = file
    end

Helper routine to translate the array of menu items from the config file into SSHMenu::Item objects.

[Source]

# File lib/sshmenu.rb, line 1356
    def set_items_from_array(a)
      item_class = mapper.get_class('app.model.item')
      @menu_items = item_class.new_from_array(a)
    end

Sets the state of the ‘show text entry’ option

[Source]

# File lib/sshmenu.rb, line 1461
    def show_entry=(val)
      set('show_entry', val ? 1 : 0)
    end

Returns true if the ‘show text entry’ option is enabled

[Source]

# File lib/sshmenu.rb, line 1452
    def show_entry?
      if opt = get('show_entry')
        return opt != 0
      end
      return false
    end

Returns the value of the ‘tooltip’ global attribute

[Source]

# File lib/sshmenu.rb, line 1401
    def tooltip_text
      return @globals['tooltip']
    end

[Validate]