1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

hooks refactor

This commit is contained in:
John Mair 2011-10-17 14:09:12 +13:00
parent e42d0aba09
commit 49153dfa2b
3 changed files with 62 additions and 12 deletions

View file

@ -4,10 +4,11 @@
require 'pp'
require 'pry/helpers/base_helpers'
require 'pry/hooks'
class Pry
# The default hooks - display messages when beginning and ending Pry sessions.
DEFAULT_HOOKS = {
:before_session => proc do |out, target, _pry_|
DEFAULT_HOOKS = Pry::Hooks.new.add_hook(:before_session) do |out, target, _pry_|
# ensure we're actually in a method
file = target.eval('__FILE__')
@ -16,7 +17,6 @@ class Pry
_pry_.process_line("whereami 5", "", target)
end
end
}
# The default print
DEFAULT_PRINT = proc do |output, value|

45
lib/pry/hooks.rb Normal file
View file

@ -0,0 +1,45 @@
class Pry
class Hooks
def initialize
@hooks = {}
end
# Add a new callable to be executed for the `name` hook.
# @param [Symbol] name The name of the hook.
# @param [#call] callable The callable.
# @yield The block to use as the callable (if `callable` parameter not provided)
def add_hook(name, callable=nil, &block)
@hooks[name] ||= []
if block
@hooks[name] << block
elsif callable
@hooks[name] << callable
else
raise ArgumentError, "Must provide a block or callable."
end
self
end
# Execute the list of callables for the `name` hook.
# @param [Symbol] name The name of the hook to execute.
# @param [Array] args The arguments to pass to each callable.
def exec_hook(name, *args, &block)
Array(@hooks[name]).each { |v| v.call(*args, &block) }
end
# Clear the list of callables for the `name` hook.
# @param [Symbol] The name of the hook to delete.
def delete_hook(name)
@hooks[name] = []
end
# Clear all hooks.
def reset
@hooks = {}
end
end
end

View file

@ -149,7 +149,8 @@ class Pry
# Initialize the repl session.
# @param [Binding] target The target binding for the session.
def repl_prologue(target)
exec_hook :before_session, output, target, self
hooks.exec_hook :before_session, output, target, self
# exec_hook :before_session, output, target, self
initialize_special_locals(target)
@input_array << nil # add empty input so _in_ and _out_ match
@ -161,7 +162,7 @@ class Pry
# Clean-up after the repl session.
# @param [Binding] target The target binding for the session.
def repl_epilogue(target)
exec_hook :after_session, output, target, self
hooks.exec_hook :after_session, output, target, self
Pry.active_sessions -= 1
binding_stack.pop
@ -230,11 +231,13 @@ class Pry
result
rescue CommandError => e
output.puts "Error: #{e.message}"
result = e.message
@suppress_output = true
rescue RescuableException => e
set_last_exception(e, target)
result = set_last_exception(e, target)
ensure
update_input_history(code)
hooks.exec_hook :after_eval, result, self
end
# Perform a read.
@ -254,6 +257,7 @@ class Pry
val = ""
loop do
val = retrieve_line(eval_string, target)
hooks.exec_hook :after_retrieve_line, val, self
process_line(val, eval_string, target)
break if valid_expression?(eval_string)
@ -261,6 +265,7 @@ class Pry
@suppress_output = true if eval_string =~ /;\Z/ || eval_string.empty?
hooks.exec_hook :after_read, eval_string, self
eval_string
end