Sha256: 84f8d30643687e97297d5ed7143a8a4ccb368783f7a56d72bdd75c1833acb259

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

module SSHKit
  class CommandMap
    class CommandHash
      def initialize(defaults = {})
        @storage = {}
        @defaults = defaults
      end

      def [](key)
        @storage[normalize_key(key)] ||= @defaults[key]
      end

      def []=(key, value)
        @storage[normalize_key(key)] = value
      end

      private

      def normalize_key(key)
        key.to_sym
      end
    end

    class PrefixProvider
      def initialize
        @storage = CommandHash.new
      end

      def [](command)
        @storage[command] ||= []
      end
    end

    TO_VALUE = ->(obj) { obj.respond_to?(:call) ? obj.call : obj }

    def initialize(value = nil)
      @map = CommandHash.new(value || defaults)
    end

    def [](command)
      if prefix[command].any?
        prefixes = prefix[command].map(&TO_VALUE)
        prefixes = prefixes.join(" ")

        "#{prefixes} #{command}"
      else
        TO_VALUE.(@map[command])
      end
    end

    def prefix
      @prefix ||= PrefixProvider.new
    end

    def []=(command, new_command)
      @map[command] = new_command
    end

    def clear
      @map = CommandHash.new(defaults)
    end

    def defaults
      Hash.new do |hash, command|
        if %w{if test time}.include? command.to_s
          hash[command] = command.to_s
        else
          hash[command] = "/usr/bin/env #{command}"
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sshkit-1.11.4 lib/sshkit/command_map.rb
sshkit-1.11.3 lib/sshkit/command_map.rb
sshkit-1.11.2 lib/sshkit/command_map.rb
sshkit-1.11.1 lib/sshkit/command_map.rb
sshkit-1.11.0 lib/sshkit/command_map.rb
sshkit-1.10.0 lib/sshkit/command_map.rb