Class: CptHook::Hookable

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/cpt_hook/hookable.rb

Overview

A class for extending other classes / modules with before / after hooks.

Instance Method Summary collapse

Constructor Details

#initialize(obj, method_hooks, additional_contexts = []) ⇒ Hookable

Returns a new instance of Hookable.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cpt_hook/hookable.rb', line 9

def initialize(obj, method_hooks, additional_contexts = [])
  super(obj)
  additional_contexts = [additional_contexts] unless additional_contexts.is_a?(Array)
  _add_hooks(:before, method_hooks, additional_contexts)
  _add_hooks(:after, method_hooks, additional_contexts)

  method_hooks.hooked_methods.each do |method|
    define_singleton_method(method) do |*args, &block|
      self.send("before_#{method}") if self.respond_to? "before_#{method}"
      val = super(*args, &block)
      self.send("after_#{method}") if self.respond_to? "after_#{method}"
      val
    end
  end
end

Instance Method Details

#classObject



25
26
27
# File 'lib/cpt_hook/hookable.rb', line 25

def class
  __getobj__.class
end