Module: Bombshell::Shell::ClassMethods
- Defined in:
- lib/bombshell/shell.rb
Overview
Class methods for your shell
Instance Method Summary collapse
-
#before_launch(&callback) ⇒ Object
Define a callback that will get called before your shell is instantiated (and therefore before it is handed over to IRB).
-
#bombshell_prompt ⇒ Object
Read your shell’s prompt.
-
#having_launched(&callback) ⇒ Object
Define a callback that will get called after your shell is instantiated, but before its binding is handed over to IRB.
-
#launch(*arguments) ⇒ Object
Launch the shell.
-
#prompt_with(p = nil, &prompt) ⇒ Object
Define your shell’s prompt.
Instance Method Details
#before_launch(&callback) ⇒ Object
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.
92 93 94 |
# File 'lib/bombshell/shell.rb', line 92 def before_launch(&callback) @bombshell_callbacks[:before_launch] << callback end |
#bombshell_prompt ⇒ Object
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.
123 124 125 |
# File 'lib/bombshell/shell.rb', line 123 def bombshell_prompt @bombshell_prompt end |
#having_launched(&callback) ⇒ Object
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.
99 100 101 |
# File 'lib/bombshell/shell.rb', line 99 def having_launched(&callback) @bombshell_callbacks[:having_launched] << callback end |
#launch(*arguments) ⇒ Object
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.
54 55 56 57 58 59 60 61 62 63 64 |
# 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 |
#prompt_with(p = nil, &prompt) ⇒ Object
Define your shell’s prompt.
You can either set your prompt to a static string, or use a Proc for a dynamic prompt.
112 113 114 |
# File 'lib/bombshell/shell.rb', line 112 def prompt_with(p = nil, &prompt) @bombshell_prompt = p || prompt end |