Module: Pakyow::Support::Hookable

Defined in:
lib/pakyow/support/hookable.rb

Overview

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).

Examples:

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

Defined Under Namespace

Modules: ClassMethods, CommonMethods, InstanceMethods

Constant Summary collapse

PRIORITIES =

Known hook priorities.

{ default: 0, high: 1, low: -1 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
# 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

Instance Method Details

#known_event?(event) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

63
64
65
# File 'lib/pakyow/support/hookable.rb', line 63

def known_event?(event)
  self.class.known_event?(event.to_sym)
end