module Cmd::ClassMethods

Public Class Methods

custom_exception_handlers() click to toggle source
# File lib/cmd.rb, line 106
def custom_exception_handlers
  @@handlers
end
docs() click to toggle source
# File lib/cmd.rb, line 38
def docs
  @@docs
end
handle(exception, handler) click to toggle source

Set what to do in the event that the given exception is raised.

handle StackOverflowError, :handle_stack_overflow
# File lib/cmd.rb, line 47
def handle(exception, handler)
  @@handlers[exception.to_s] = handler
end
prompt() click to toggle source

Returns the evaluation of expression passed to prompt_with. Result has to_s called on it as Readline expects a String for its prompt. XXX This could probably be more robust

# File lib/cmd.rb, line 73
def prompt
  case @@prompt
  when Symbol 
      self.send @@prompt
  when Proc   
      @@prompt.call
  else         
      @@prompt
  end.to_s
end
shortcut_table() click to toggle source
# File lib/cmd.rb, line 96
def shortcut_table
  @@shortcut_table
end
shortcuts() click to toggle source
# File lib/cmd.rb, line 101
def shortcuts
  @@shortcuts
end

Public Instance Methods

define_collect_method(prefix) click to toggle source

Defines a method which returns all defined methods which start with the passed in prefix followed by an underscore. Used to define methods to collect things such as all defined 'complete' and 'do' methods.

# File lib/cmd.rb, line 114
def define_collect_method(prefix)
  method = 'collect_' + prefix
  unless self.respond_to?(method) 
    define_method(method) do
      self.methods.grep(/^#{prefix}_/).map {|meth| meth[prefix.size + 1..-1]}
    end
  end
end
doc(command, docstring = nil) { || ... } click to toggle source

Set documentation for a command

doc :help, 'Display this help.'
def do_help
  # etc
end
# File lib/cmd.rb, line 33
def doc(command, docstring = nil)
  docstring = docstring ? docstring : yield
  @@docs[command.to_s] = docstring
end
prompt_with(*p, &block) click to toggle source

Sets what the prompt is. Accepts a String, a block or a Symbol.

Block

prompt_with { Time.now }

Symbol

prompt_with :set_prompt

String

prompt_with "#{self.class.name}> "
# File lib/cmd.rb, line 66
def prompt_with(*p, &block)
  @@prompt = block_given? ? block : p.first
end
shortcut(short, command) click to toggle source

Create a command short cut

shortcut '?', 'help'
def do_help
  # etc
end
# File lib/cmd.rb, line 91
def shortcut(short, command)
  (@@shortcuts[command.to_s] ||= []).push short
  @@shortcut_table[short] = command.to_s
end

Private Instance Methods

custom_exception_handlers() click to toggle source
# File lib/cmd.rb, line 106
def custom_exception_handlers
  @@handlers
end
docs() click to toggle source
# File lib/cmd.rb, line 38
def docs
  @@docs
end
handle(exception, handler) click to toggle source

Set what to do in the event that the given exception is raised.

handle StackOverflowError, :handle_stack_overflow
# File lib/cmd.rb, line 47
def handle(exception, handler)
  @@handlers[exception.to_s] = handler
end
prompt() click to toggle source

Returns the evaluation of expression passed to prompt_with. Result has to_s called on it as Readline expects a String for its prompt. XXX This could probably be more robust

# File lib/cmd.rb, line 73
def prompt
  case @@prompt
  when Symbol 
      self.send @@prompt
  when Proc   
      @@prompt.call
  else         
      @@prompt
  end.to_s
end
shortcut_table() click to toggle source
# File lib/cmd.rb, line 96
def shortcut_table
  @@shortcut_table
end
shortcuts() click to toggle source
# File lib/cmd.rb, line 101
def shortcuts
  @@shortcuts
end