Module: Waw::Kern::Hooks
- Included in:
- App
- Defined in:
- lib/waw/kern/hooks.rb
Overview
Provides the hook support to the waw kernel.
This module installs a @hooks instance variable (array of hooks by abstract name). It also expects the root_folder and logger instance variables provided by other modules.
Instance Method Summary collapse
-
#add_start_hook(hook = nil, &block) ⇒ Object
Adds a start hook.
-
#add_unload_hook(hook = nil, &block) ⇒ Object
Adds an unload hook.
-
#execute_hooks(which) ⇒ Object
Executes all hooks installed under which.
-
#hooks(which) ⇒ Object
Returns installed hooks for a given abstract name which (:load, :start, :unload,…).
Instance Method Details
#add_start_hook(hook = nil, &block) ⇒ Object
Adds a start hook.
21 22 23 |
# File 'lib/waw/kern/hooks.rb', line 21 def add_start_hook(hook = nil, &block) hooks(:start) << (hook || block) end |
#add_unload_hook(hook = nil, &block) ⇒ Object
Adds an unload hook.
26 27 28 |
# File 'lib/waw/kern/hooks.rb', line 26 def add_unload_hook(hook = nil, &block) hooks(:unload) << (hook || block) end |
#execute_hooks(which) ⇒ Object
Executes all hooks installed under which. Hooks installed through the API are executed first. Hooks installed under root_folder/hooks/which are executed then.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/waw/kern/hooks.rb', line 33 def execute_hooks(which) # API installed hooks hooks(which).each do |hook| if hook.respond_to?(:run) hook.run(self) elsif hook.respond_to?(:call) hook.call(self) end end # the file ones now hooks_dir = File.join(root_folder, 'hooks', which.to_s) Dir[File.join(hooks_dir, '*.rb')].sort{|f1, f2| File.basename(f1) <=> File.basename(f2)}.each do |file| logger.info("Running waw #{which} hook #{file}...") ::Kernel.load(file) end end |
#hooks(which) ⇒ Object
Returns installed hooks for a given abstract name which (:load, :start, :unload,…). This method always returns an array, which can be empty if no hook is currently installed under which.
15 16 17 18 |
# File 'lib/waw/kern/hooks.rb', line 15 def hooks(which) @hooks = Hash.new{|h,k| h[k] = []} if @hooks.nil? @hooks[which] end |