Class: MindControl::REPL

Inherits:
Object
  • Object
show all
Defined in:
lib/mind_control/repl.rb

Overview

Pry based REPL.

Defined Under Namespace

Classes: CoollineAdapter

Instance Method Summary collapse

Constructor Details

#initialize(target, options = {}) ⇒ REPL

Returns a new instance of REPL.

Parameters:

  • The receiver of the Pry session.

  • (defaults to: {})

    The optional configuration parameters for Pry.



16
17
18
19
# File 'lib/mind_control/repl.rb', line 16

def initialize( target, options = {} )
  @target  = target
  @options = options
end

Instance Method Details

#start(stdin, stdout) ⇒ Object

Start REPL session.

Parameters:

  • The object to use for input.

  • The object to use for output.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mind_control/repl.rb', line 27

def start( stdin, stdout )
  # NB: We cannot use Readline, because it always uses STDOUT / STDIN.
  input = CoollineAdapter.new( stdin, stdout )

  # Default command set
  commands = @options[ :commands ] || ::Pry::CommandSet.new.import( ::Pry::Commands )

  # Import our MindControl commands
  commands.import MindControl::PryCommands

  # Target can be callable
  target = @target.respond_to?( :call ) ? @target.call : @target

  # NB: input/input can't be changed via pry options!
  pry = ::Pry.new @options.merge( :commands => commands, :input => input, :output => stdout )

  # Store pry instance in thread-local context so that we can later determine
  # whether we are running inside MindControl session or not.
  ::Pry.current[ :mind_control_pry_instance ] = pry

  # Start session
  pry.repl target
end