Sha256: bcbc620a1373eeb459dff1df51ec5d9cd4d1a19425e560230a7159859f62ccf8

Contents?: true

Size: 1.89 KB

Versions: 13

Compression:

Stored size: 1.89 KB

Contents

require 'set'
require 'warp/dir/errors'
require 'warp/dir/formatter'
require 'singleton'
module Warp
  module Dir
    class Commander
      include Singleton

      attr_reader :commands
      attr_accessor :command_map

      def initialize
        @commands ||= Set.new # a pre-caution, normally it would already by defined by now
        @command_map   = {}
      end

      def register(command)
        @commands << command if command
        self
      end

      def installed_commands
        @commands.map(&:command_name)
      end

      def lookup(command_name)
        reindex!
        command_map[command_name]
      end

      def find(command_name)
        command = lookup(command_name)
        if command.nil?
          raise ::Warp::Dir::Errors::InvalidCommand.new(command_name)
        end
        command
      end

      def run(command_name, *args)
        cmd = find command_name
        raise ::Warp::Dir::Errors::InvalidCommand.new(command_name) unless cmd.is_a?(warp::Dir::Command)
        cmd.new(*args).run
      end

      def reindex!
        commands.each do |command|
          if command.respond_to?(:aliases)
            command.aliases.each do |an_alias|
              if self.command_map[an_alias] && !self.command_map[an_alias] == command
                raise Warp::Dir::Errors::InvalidCommand.new("Duplicate alias for command #{command}")
              end
              self.command_map[an_alias] = command
            end
          end
          self.command_map[command.command_name] = command
        end
        self
      end

      def validate!
        self.commands.delete_if do |subclass|
          if !subclass.respond_to?(:abstract_class?) && !subclass.method_defined?(:run)
            raise ::Warp::Dir::Errors::InvalidCommand.new(subclass)
          end
          subclass.respond_to?(:abstract_class?) || !subclass.method_defined?(:run)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
warp-dir-1.7.0 lib/warp/dir/commander.rb
warp-dir-1.6.2 lib/warp/dir/commander.rb
warp-dir-1.6.1 lib/warp/dir/commander.rb
warp-dir-1.6.0 lib/warp/dir/commander.rb
warp-dir-1.5.0 lib/warp/dir/commander.rb
warp-dir-1.3.0 lib/warp/dir/commander.rb
warp-dir-1.2.0 lib/warp/dir/commander.rb
warp-dir-1.1.5 lib/warp/dir/commander.rb
warp-dir-1.1.4 lib/warp/dir/commander.rb
warp-dir-1.1.3 lib/warp/dir/commander.rb
warp-dir-1.1.2 lib/warp/dir/commander.rb
warp-dir-1.1.1 lib/warp/dir/commander.rb
warp-dir-1.1.0 lib/warp/dir/commander.rb