Class | Pry::Hooks |
In: |
lib/pry/hooks.rb
|
Parent: | Object |
Implements a hooks system for Pry. A hook is a callable that is associated with an event. A number of events are currently provided by Pry, these include: `:when_started`, `:before_session`, `:after_session`. A hook must have a name, and is connected with an event by the `Pry::Hooks#add_hook` method. @example Adding a hook for the `:before_session` event.
Pry.config.hooks.add_hook(:before_session, :say_hi) do puts "hello" end
Converts a hash to a `Pry::Hooks` instance. All hooks defined this way are anonymous. This functionality is primarily to provide backwards-compatibility with the old hash-based hook system in Pry versions < 0.9.8 @param [Hash] hash The hash to convert to `Pry::Hooks`. @return [Pry::Hooks] The resulting `Pry::Hooks` instance.
Add a new hook to be executed for the `name` even. @param [Symbol] event_name The name of the event. @param [Symbol] hook_name The name of the hook. @param [call] callable The callable. @yield The block to use as the callable (if `callable` parameter not provided) @return [Pry:Hooks] Returns the receiver. @example
Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
Remove all events and hooks, clearing out the Pry::Hooks instance completely. @example
my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" } my_hooks.clear_all
Delete a hook for an event. @param [Symbol] event_name The name of the event. @param [Symbol] hook_name The name of the hook.
to delete.
@return [call] The deleted hook. @example
my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" } my_hooks.delete_hook(:before_session, :say_hi)
Clear all hooks functions for a given event. @param [String] event_name The name of the event. @example
my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" } my_hooks.delete_hook(:before_session)
Execute the list of hooks for the `event_name` event. @param [Symbol] event_name The name of the event. @param [Array] args The arguments to pass to each hook function. @return [Object] The return value of the last executed hook. @example
my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" } my_hooks.exec_hook(:before_session) #=> OUTPUT: "hi!"
Return a specific hook for a given event. @param [Symbol] event_name The name of the event. @param [Symbol[ hook_name The name of the hook @return [call] The requested hook. @example
my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" } my_hooks.get_hook(:before_session, :say_hi).call #=> "hi!"
Return the hash of hook names / hook functions for a given event. (Note that modifying the returned hash does not alter the hooks, use add_hook/delete_hook for that). @param [Symbol] event_name The name of the event. @return [Hash] The hash of hook names / hook functions. @example
my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" } my_hooks.get_hooks(:before_session) #=> {:say_hi=>#<Proc:0x00000101645e18@(pry):9>}
Return the number of hook functions registered for the `event_name` event. @param [Symbol] event_name The name of the event. @return [Fixnum] The number of hook functions for `event_name`. @example
my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" } my_hooks.count(:before_session) #=> 1
Return a new `Pry::Hooks` instance containing a merge of the contents of two `Pry:Hooks` instances, @param [Pry::Hooks] other The `Pry::Hooks` instance to merge @return [Pry::Hooks] The new hash. @example
hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" } Pry::Hooks.new.merge(hooks)
Destructively merge the contents of two `Pry:Hooks` instances. @param [Pry::Hooks] other The `Pry::Hooks` instance to merge @return [Pry:Hooks] Returns the receiver. @example
hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" } Pry::Hooks.new.merge!(hooks)