Method: CptHook::Hookable#initialize
- Defined in:
- lib/cpt_hook/hookable.rb
#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 24 25 |
# 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.map {|h| h[:before] || h[:after]}.uniq.each do |method| define_singleton_method(method) do |*args, &block| self.send("before_#{method}") if self.respond_to? "before_#{method}" #val = __getobj__.send(method, *args, &block) val = super(*args, &block) self.send("after_#{method}") if self.respond_to? "after_#{method}" val end end end |