Module: Bombshell::Shell::ClassMethods

Defined in:
lib/bombshell/shell.rb

Overview

Class methods for your shell

Instance Method Summary collapse

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.

Examples:

class MyShell < Bombshell::Environment
  include Bombshell::Shell
  before_launch do
    define_method :foo
      puts 'bar'
    end
  end
end
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

See Also:



92
93
94
# File 'lib/bombshell/shell.rb', line 92

def before_launch(&callback)
  @bombshell_callbacks[:before_launch] << callback
end

#bombshell_promptObject

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.

See Also:



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.

Parameters:

  • arguments (Array)

    An array of arguments that, eventually, callbacks might use. If you’re using the Bombshell.launch wrapper, these will be command-line parameters.



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.

Parameters:

  • p (String) (defaults to: nil)

    a string to be used as your shell’s prompt.

  • prompt (Proc)

    a Proc to be rendered whenever your shell’s prompt needs to be displayed. Within the callback, self is your shell class. If your proc asks for a blockvar, it will be given your shell instance.

See Also:



112
113
114
# File 'lib/bombshell/shell.rb', line 112

def prompt_with(p = nil, &prompt)
  @bombshell_prompt = p || prompt
end