module Pakyow::Support::Hookable

Makes it possible to define and call hooks on an object.

Hooks can be defined at the class or instance level. When calling hooks on an instance, hooks defined on the class will be called first.

By default, hooks are called in the order they are defined. Each hook can be assigned a relative priority to influence when it is to be called (relative to other hooks of the same type). Default hook priority is `0`, and can instead be set to `1` (high) or `-1` (low).

@example

class Fish
  include Pakyow::Support::Hookable
  events :swim

  def swim
    performing "swim" do
      puts "swimming"
    end
  end
end

Fish.before "swim" do
  puts "prepping"
end

fish = Fish.new

fish.after "swim" do
  puts "resting"
end

fish.swim
=> prepping
   swimming
   resting

Constants

PRIORITIES

Known hook priorities.

Public Class Methods

included(base) click to toggle source
# File lib/pakyow/support/hookable.rb, line 49
def self.included(base)
  base.include CommonMethods
  base.include InstanceMethods

  base.extend ClassMethods

  base.extend ClassState
  base.class_state :__events, default: [], inheritable: true, reader: false
  base.class_state :__hooks, default: [], inheritable: true, reader: false
  base.class_state :__hook_hash, default: { after: {}, before: {} }, inheritable: true
  base.class_state :__hook_pipeline, default: { after: {}, before: {} }, inheritable: true
end

Public Instance Methods

known_event?(event) click to toggle source

@api private

# File lib/pakyow/support/hookable.rb, line 63
def known_event?(event)
  self.class.known_event?(event.to_sym)
end