From 49153dfa2be280da249bd7e76334716bb23ec61e Mon Sep 17 00:00:00 2001 From: John Mair Date: Mon, 17 Oct 2011 14:09:12 +1300 Subject: [PATCH] hooks refactor --- lib/pry.rb | 18 ++++++++--------- lib/pry/hooks.rb | 45 +++++++++++++++++++++++++++++++++++++++++ lib/pry/pry_instance.rb | 11 +++++++--- 3 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 lib/pry/hooks.rb diff --git a/lib/pry.rb b/lib/pry.rb index 4d2a8606..ae6aec8a 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -4,19 +4,19 @@ 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_| - # ensure we're actually in a method - file = target.eval('__FILE__') + DEFAULT_HOOKS = Pry::Hooks.new.add_hook(:before_session) do |out, target, _pry_| + # ensure we're actually in a method + file = target.eval('__FILE__') - # /unknown/ for rbx - if file !~ /(\(.*\))|<.*>/ && file !~ /__unknown__/ && file != "" && file != "-e" - _pry_.process_line("whereami 5", "", target) - end + # /unknown/ for rbx + if file !~ /(\(.*\))|<.*>/ && file !~ /__unknown__/ && file != "" && file != "-e" + _pry_.process_line("whereami 5", "", target) end - } + end # The default print DEFAULT_PRINT = proc do |output, value| diff --git a/lib/pry/hooks.rb b/lib/pry/hooks.rb new file mode 100644 index 00000000..55984a69 --- /dev/null +++ b/lib/pry/hooks.rb @@ -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 diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 0ca25043..9552e20d 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -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