class Warp::Dir::Commander

Attributes

command_map[RW]
commands[R]

Public Class Methods

new() click to toggle source
# File lib/warp/dir/commander.rb, line 13
def initialize
  @commands ||= Set.new # a pre-caution, normally it would already by defined by now
  @command_map   = {}
end

Public Instance Methods

find(command_name) click to toggle source
# File lib/warp/dir/commander.rb, line 32
def find(command_name)
  command = lookup(command_name)
  if command.nil?
    raise ::Warp::Dir::Errors::InvalidCommand.new(command_name)
  end
  command
end
installed_commands() click to toggle source
# File lib/warp/dir/commander.rb, line 23
def installed_commands
  @commands.map(&:command_name)
end
lookup(command_name) click to toggle source
# File lib/warp/dir/commander.rb, line 27
def lookup(command_name)
  reindex!
  command_map[command_name]
end
register(command) click to toggle source
# File lib/warp/dir/commander.rb, line 18
def register(command)
  @commands << command if command
  self
end
reindex!() click to toggle source
# File lib/warp/dir/commander.rb, line 46
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
run(command_name, *args) click to toggle source
# File lib/warp/dir/commander.rb, line 40
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
validate!() click to toggle source
# File lib/warp/dir/commander.rb, line 61
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