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

Hooks now have names, also improved some terminology.

Naming hooks makes them easier to reshuffle and customize.
This commit is contained in:
John Mair 2011-11-16 02:49:11 +13:00
parent 7cc5176d93
commit 263d621625
3 changed files with 66 additions and 34 deletions

View file

@ -8,7 +8,7 @@ require 'pry/hooks'
class Pry
# The default hooks - display messages when beginning and ending Pry sessions.
DEFAULT_HOOKS = Pry::Hooks.new.add_hook(:before_session) do |out, target, _pry_|
DEFAULT_HOOKS = Pry::Hooks.new.add_hook(:before_session, :default) do |out, target, _pry_|
# ensure we're actually in a method
file = target.eval('__FILE__')

View file

@ -5,17 +5,18 @@ class Pry
@hooks = {}
end
# Add a new callable to be executed for the `name` hook.
# @param [Symbol] name The name of the hook.
# Add a new hook to be executed for the `name` even.
# @param [Symbol] name The name of the event.
# @param [Symbol] hook_function_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] ||= []
def add_hook(name, hook_function_name, callable=nil, &block)
@hooks[name] ||= {}
if block
@hooks[name] << block
@hooks[name][hook_function_name] = block
elsif callable
@hooks[name] << callable
@hooks[name][hook_function_name] = callable
else
raise ArgumentError, "Must provide a block or callable."
end
@ -23,29 +24,46 @@ class Pry
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.
# Execute the list of hooks for the `name` event.
# @param [Symbol] name The name of the event.
# @param [Array] args The arguments to pass to each hook function.
# @return [Hash] The return values for each of the hook functions.
def exec_hook(name, *args, &block)
Array(@hooks[name]).map { |v| v.call(*args, &block) }
@hooks[name] ||= {}
Hash[@hooks[name].each.map { |k, v| [k, v.call(*args, &block)] }]
end
# Return the number of callables registered for the `name` hook.
# @param [Symbol] name The name of the hook.
# Return the number of hook functions registered for the `name` event.
# @param [Symbol] name The name of the event.
# @return [Fixnum] The number of hook functions for the `name` event.
def hook_count(name)
@hooks[name] ||= []
@hooks[name] ||= {}
@hooks[name].size
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] = []
# Return the hash of hook names / hook functions for a
# given event.
# @param [Symbol] name The name of the event.
# @return [Hash]
def get_hook(name, hook_function_name)
@hooks[name] ||= {}
@hooks[name][hook_function_name]
end
# Clear all hooks.
def reset
@hooks = {}
# Delete a hook for an event.
# @param [Symbol] name The name of the event.
# @param [Symbol] hook_function_name The name of the hook.
# to delete.
# @return [#call] The deleted hook.
def delete_hook(name, hook_function_name)
@hooks[name] ||= {}
@hooks[name].delete(hook_function_name)
end
# Clear all hooks functions for a given event.
# @param [String] name The name of the event.
def clear(name)
@hooks[name] = {}
end
end

View file

@ -2,12 +2,12 @@ require 'helper'
describe Pry::Hooks do
before do
@hooks = Pry::Hooks.new
@hooks = Pry::Hooks.new
end
describe "adding a new hook" do
it 'should not execute hook while adding it' do
@hooks.add_hook(:test_hook) { @test_var = true }
@hooks.add_hook(:test_hook, :my_name) { @test_var = true }
@test_var.should == nil
end
@ -16,44 +16,58 @@ describe Pry::Hooks do
end
it 'should create a new hook with a block' do
@hooks.add_hook(:test_hook) { }
@hooks.add_hook(:test_hook, :my_name) { }
@hooks.hook_count(:test_hook).should == 1
end
it 'should create a new hook with a callable' do
@hooks.add_hook(:test_hook, proc { })
@hooks.add_hook(:test_hook, :my_name, proc { })
@hooks.hook_count(:test_hook).should == 1
end
it 'should use just block if given both block and callable' do
@hooks.add_hook(:test_hook, proc { }) { }
@hooks.add_hook(:test_hook, :my_name, proc { }) { }
@hooks.hook_count(:test_hook).should == 1
end
it 'should raise if not given a block or any other object' do
lambda { @hooks.add_hook(:test_hook) }.should.raise ArgumentError
lambda { @hooks.add_hook(:test_hook, :my_name) }.should.raise ArgumentError
end
it 'should create a hook with multiple callables' do
@hooks.add_hook(:test_hook) {}
@hooks.add_hook(:test_hook) {}
@hooks.add_hook(:test_hook, :my_name) {}
@hooks.add_hook(:test_hook, :my_name2) {}
@hooks.hook_count(:test_hook).should == 2
end
end
describe "deleting a hook" do
it 'should successfully delete a hook function' do
@hooks.add_hook(:test_hook, :my_name) {}
@hooks.delete_hook(:test_hook, :my_name)
@hooks.hook_count(:test_hook).should == 0
end
it 'should return the deleted hook function' do
@hooks.add_hook(:test_hook, :my_name) {}
@hooks.delete_hook(:test_hook, :my_name)
@hooks.hook_count(:test_hook).should == 0
end
end
describe "executing a hook" do
before do
@test_var = nil
end
it 'should execute block hook' do
@hooks.add_hook(:test_hook) { @test_var = true }
@hooks.add_hook(:test_hook, :my_name) { @test_var = true }
@hooks.exec_hook(:test_hook)
@test_var.should == true
end
it 'should execute proc hook' do
@hooks.add_hook(:test_hook, proc { @test_var = true })
@hooks.add_hook(:test_hook, :my_name, proc { @test_var = true })
@hooks.exec_hook(:test_hook)
@test_var.should == true
end
@ -67,7 +81,7 @@ describe Pry::Hooks do
end
end
@hooks.add_hook(:test_hook, callable)
@hooks.add_hook(:test_hook, :my_name, callable)
@hooks.exec_hook(:test_hook)
callable.test_var.should == true
end
@ -75,8 +89,8 @@ describe Pry::Hooks do
it 'should execute multiple callables for a hook if more than one is defined' do
x = nil
y = nil
@hooks.add_hook(:test_hook) { x = true }
@hooks.add_hook(:test_hook) { y = true }
@hooks.add_hook(:test_hook, :my_name2) { x = true }
@hooks.add_hook(:test_hook, :my_name) { y = true }
@hooks.exec_hook(:test_hook)
x.should == true
y.should == true