module Bombshell::Shell::ClassMethods
Class methods for your shell
Public Instance Methods
Define a callback that will get called before your shell is instantiated (and therefore before it is handed over to IRB
).
This is a great place to dynamically define additional instance methods on your class. If your callback proc asks for blockvars, Bombshell
will give it as many command-line parameters as it needs to. Within the callback, self
is your shell class. @see having_launched
@example
class MyShell < Bombshell::Environment include Bombshell::Shell before_launch do define_method :foo puts 'bar' end end end
@example
class MyShell < Bombshell::Environment include Bombshell::Shell before_launch do |first_command_line_parameter, second_command_line_parameter| define_method :display_first_command_line_parameter puts first_command_line_parameter end end end
# File lib/bombshell/shell.rb, line 92 def before_launch(&callback) @bombshell_callbacks[:before_launch] << callback end
Read your shell’s prompt
Note that this method returns an unrendered prompt. That is, if it’s defined as a Proc, you’ll get a Proc back. If you want to get the rendered prompt, use #_prompt
on your shell instance. @see prompt_with
@see Bombshell::Shell
#_prompt
# File lib/bombshell/shell.rb, line 123 def bombshell_prompt @bombshell_prompt end
Define a callback that will get called after your shell is instantiated, but before its binding is handed over to IRB
. Within the callback, self
is your shell instance. @see before_launch
# File lib/bombshell/shell.rb, line 99 def having_launched(&callback) @bombshell_callbacks[:having_launched] << callback end
Launch the shell.
You should generally call Bombshell.launch(MyShell)
instead of MyShell.launch
directly, as the former approach will handle exits correctly and pass command-line parameters as arguments. @param [Array] arguments An array of arguments that, eventually, callbacks
might use. If you're using the <tt>Bombshell.launch</tt> wrapper, these will be command-line parameters.
# File lib/bombshell/shell.rb, line 54 def launch(*arguments) @bombshell_callbacks[:before_launch].each do |callback| callback.call(*arguments.first(callback.arity > -1 ? callback.arity : 0)) end number_of_requested_arguments = instance_method(:initialize).arity < 0 ? arguments.length : instance_method(:initialize).arity shell = new(*arguments.first(number_of_requested_arguments)) @bombshell_callbacks[:having_launched].each do |callback| shell.instance_eval &callback end ::IRB.start_session(shell.get_binding) end
Define your shell’s prompt.
You can either set your prompt to a static string, or use a Proc for a dynamic prompt. @param [String] p a string to be used as your shell’s prompt. @param [Proc] prompt a Proc to be rendered whenever your shell’s prompt needs to be
displayed. Within the callback, <tt>self</tt> is your shell <i>class</i>. If your proc asks for a blockvar, it will be given your shell <i>instance</i>.
@see Bombshell::Shell
#_prompt @see bombshell_prompt
# File lib/bombshell/shell.rb, line 112 def prompt_with(p = nil, &prompt) @bombshell_prompt = p || prompt end