class Ragent::Commands

Public Class Methods

new(ragent) click to toggle source
# File lib/ragent/commands.rb, line 6
def initialize(ragent)
  @ragent = ragent
  @commands = {}
  add_help_command
end

Public Instance Methods

add(command) click to toggle source
# File lib/ragent/commands.rb, line 12
def add(command)
  if command.sub?
    @commands[command.main] ||= {}
    @commands[command.main][command.sub] = command
  else
    @commands[command.main] ||= command
  end
  info "registered command: #{command.help}"
end
lookup(main, sub, options) click to toggle source
# File lib/ragent/commands.rb, line 22
def lookup(main, sub, options)
  debug "checkig command: #{main},#{sub},#{options}"
  cmd = @commands[main]
  if cmd

    if cmd.is_a?(Hash) && sub
      sub_cmd = @commands[main][sub]
      if sub_cmd
        debug "command found (#{main},#{sub})"
      else
        debug "command not found (#{main},#{sub})"
      end
      return sub_cmd
    else
      debug "command found (#{main})"
      return cmd
    end
  else
    debug 'command not found'
  end
  nil
end

Private Instance Methods

add_help_command() click to toggle source
# File lib/ragent/commands.rb, line 57
def add_help_command
  add(Ragent::Command.new(main: 'help',
                          recipient: self,
                          method: :help_command))
end
help_command(_options = {}) click to toggle source
# File lib/ragent/commands.rb, line 47
def help_command(_options = {})
  @commands.values.map do |subs|
    if subs.is_a?(Hash)
      subs.values.map(&:help)
    else
      subs.help
    end
  end.flatten.join("\n")
end