Class: Pry::CommandProcessor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pry/command_processor.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CommandProcessor) initialize(pry_instance)

A new instance of CommandProcessor



9
10
11
# File 'lib/pry/command_processor.rb', line 9

def initialize(pry_instance)
  @pry_instance = pry_instance
end

Instance Attribute Details

- (Object) pry_instance

Returns the value of attribute pry_instance



7
8
9
# File 'lib/pry/command_processor.rb', line 7

def pry_instance
  @pry_instance
end

Instance Method Details

- (Array) command_matched(val, target)

Determine whether a Pry command was matched and return command data and argument string. This method should not need to be invoked directly.

Parameters:

  • val (String)

    The line of input.

  • target (Binding)

    The binding to perform string interpolation against.

Returns:

  • (Array)

    The command data and arg string pair



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pry/command_processor.rb', line 55

def command_matched(val, target)
  _, cmd_data = commands.commands.find do |name, data|

    command_regex = /^#{convert_to_regex(name)}(?!\S)/

    if data.options[:interpolate]
      # If interpolation fails then the command cannot be matched,
      # so early exit.
      begin
        interp_val = interpolate_string(val, target)
      rescue NameError
        next
      end

      val.replace interp_val if command_regex =~ interp_val
    else
      command_regex =~ val
    end
  end

  [cmd_data, (Regexp.last_match ? Regexp.last_match.captures : nil), (Regexp.last_match ? Regexp.last_match.end(0) : nil)]
end

- (String) convert_to_regex(obj)

Convert the object to a form that can be interpolated into a Regexp cleanly.

Returns:

  • (String)

    The string to interpolate into a Regexp



27
28
29
30
31
32
33
34
# File 'lib/pry/command_processor.rb', line 27

def convert_to_regex(obj)
  case obj
  when String
    Regexp.escape(obj)
  else
    obj
  end
end

- (Object) execute_command(target, command, options, *args)

Execute a Pry command. This method should not need to be invoked directly.

Parameters:

  • target (Binding)

    The target of the Pry session.

  • command (String)

    The name of the command to be run.

  • options (Hash)

    The options to set on the Commands object.

  • args (Array)

    The command arguments.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/pry/command_processor.rb', line 117

def execute_command(target, command, options, *args)
  context = CommandContext.new

  # set some useful methods to be used by the action blocks
  context.opts        = options
  context.target      = target
  context.output      = output
  context.captures    = options[:captures]
  context.eval_string = options[:eval_string]
  context.arg_string  = options[:arg_string]
  context.command_set = commands

  context.command_processor = self

  ret = commands.run_command(context, command, *args)

  # Tick, tock, im getting rid of this shit soon.
  options[:val].replace("")

  ret
end

- (String) interpolate_string(str, target)

Revaluate the string (str) and perform interpolation.

Parameters:

  • str (String)

    The string to reevaluate with interpolation.

  • target (Binding)

    The context where the string should be interpolated in.

Returns:

  • (String)

    The reevaluated string with interpolations applied (if any).



42
43
44
45
46
# File 'lib/pry/command_processor.rb', line 42

def interpolate_string(str, target)
  dumped_str = str.dump
  dumped_str.gsub!(/\\\#\{/, '#{')
  target.eval(dumped_str)
end

- (Object) process_commands(val, eval_string, target)

Process Pry commands. Pry commands are not Ruby methods and are evaluated prior to Ruby expressions. Commands can be modified/configured by the user: see Pry::Commands This method should not need to be invoked directly - it is called by Pry#r.

Parameters:

  • val (String)

    The current line of input.

  • eval_string (String)

    The cumulative lines of input for multi-line input.

  • target (Binding)

    The receiver of the commands.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pry/command_processor.rb', line 87

def process_commands(val, eval_string, target)

  # no command was matched, so return to caller
  command, captures, pos = command_matched(val, target)
  return if !command
  arg_string = val[pos..-1]

  # remove the one leading space if it exists
  arg_string.slice!(0) if arg_string.start_with?(" ")

  args = arg_string ? Shellwords.shellwords(arg_string) : []

  options = {
    :val => val,
    :arg_string => arg_string,
    :eval_string => eval_string,
    :nesting => nesting,
    :commands => commands.commands,
    :captures => captures
  }

  execute_command(target, command.name, options, *(captures + args))
end

- (Boolean) valid_command?(val, target = binding)

Is the string a valid command?

Parameters:

  • val (String)

    The string passed in from the Pry prompt.

  • target (Binding) (defaults to: binding)

    The context where the string should be interpolated in.

Returns:

  • (Boolean)

    Whether the string is a valid command.



20
21
22
# File 'lib/pry/command_processor.rb', line 20

def valid_command?(val, target=binding)
  !!(command_matched(val, target)[0])
end