Module: Olelo::Hooks::ClassMethods

Defined in:
lib/olelo/hooks.rb

Overview

Extends class with hook functionality

Instance Method Summary collapse

Instance Method Details

#after(name, priority = 99) { ... } ⇒ void

This method returns an undefined value.

Register before hook

The hook will be invoked by Olelo::Hooks#with_hooks.

Parameters:

  • name (Symbol, String)

    of hook

  • priority (Integer) (defaults to: 99)

Yields:

  • Hook block with arguments matching the hook invocation



137
138
139
# File 'lib/olelo/hooks.rb', line 137

def after(name, priority = 99, &block)
  hook("AFTER #{name}", priority, &block)
end

#before(name, priority = 99) { ... } ⇒ void

This method returns an undefined value.

Register before hook

The hook will be invoked by Olelo::Hooks#with_hooks.

Parameters:

  • name (Symbol, String)

    of hook

  • priority (Integer) (defaults to: 99)

Yields:

  • Hook block with arguments matching the hook invocation



123
124
125
# File 'lib/olelo/hooks.rb', line 123

def before(name, priority = 99, &block)
  hook("BEFORE #{name}", priority, &block)
end

#has_around_hooks(*names) ⇒ Object



81
82
83
84
85
# File 'lib/olelo/hooks.rb', line 81

def has_around_hooks(*names)
  names.each do |name|
    has_hooks "BEFORE #{name}", "AFTER #{name}"
  end
end

#has_hooks(*names) ⇒ Object



87
88
89
90
91
92
# File 'lib/olelo/hooks.rb', line 87

def has_hooks(*names)
  names.map(&:to_sym).each do |name|
    raise "#{self} already has hook '#{name}'" if hooks.include?(name)
    hooks[name] = []
  end
end

#hook(name, priority = 99) { ... } ⇒ void

This method returns an undefined value.

Register hook for class

The hook will be invoked by Olelo::Hooks#invoke_hook. Hooks with lower priority are called first.

Parameters:

  • name (Symbol, String)

    of hook

  • priority (Integer) (defaults to: 99)

Yields:

  • Hook block with arguments matching the hook invocation



104
105
106
107
108
109
110
111
# File 'lib/olelo/hooks.rb', line 104

def hook(name, priority = 99, &block)
  list = hooks[name.to_sym]
  raise "#{self} has no hook '#{name}'" if !list
  method = "HOOK #{name} #{list.size}"
  define_method(method, &block)
  list << [priority, instance_method(method)]
  list.sort_by!(&:first)
end

#hooksHash

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.

Hash of registered hooks

Returns:

  • (Hash)

    of hooks



77
78
79
# File 'lib/olelo/hooks.rb', line 77

def hooks
  @hooks ||= {}
end