module MethodHooks
Constants
- VERSION
Public Class Methods
extended(base)
click to toggle source
# File lib/method_hooks.rb, line 8 def self.extended(base) base.send :include, InstanceMethods end
Private Instance Methods
after(*method_names, &block)
click to toggle source
# File lib/method_hooks.rb, line 85 def after(*method_names, &block) if block.nil? callback_method = method_names.pop block = proc { method(callback_method).call } end method_names.each do |method_name| after_callbacks[method_name] << block end subclasses.each do |subclass| subclass.after(method_names, &block) end end
after_callbacks()
click to toggle source
# File lib/method_hooks.rb, line 51 def after_callbacks @after_callbacks ||= Hash.new { |hash, key| hash[key] = Array.new } end
around(*method_names, &block)
click to toggle source
# File lib/method_hooks.rb, line 70 def around(*method_names, &block) if block.nil? callback_method = method_names.pop block = proc { |method| method(callback_method).call(method) } end method_names.each do |method_name| around_callbacks[method_name] << block end subclasses.each do |subclass| descendant.around(method_names, &block) end end
around_callbacks()
click to toggle source
# File lib/method_hooks.rb, line 47 def around_callbacks @around_callbacks ||= Hash.new { |hash, key| hash[key] = Array.new } end
before(*method_names, &block)
click to toggle source
# File lib/method_hooks.rb, line 55 def before(*method_names, &block) if block.nil? callback_method = method_names.pop block = proc { method(callback_method).call } end method_names.each do |method_name| before_callbacks[method_name] << block end subclasses.each do |subclass| descendant.before(method_names, &block) end end
before_callbacks()
click to toggle source
# File lib/method_hooks.rb, line 43 def before_callbacks @before_callbacks ||= Hash.new { |hash, key| hash[key] = Array.new } end
inherited(child_class)
click to toggle source
Calls superclass method
# File lib/method_hooks.rb, line 14 def inherited(child_class) child_class.instance_variable_set(:@new_method, true) child_class.instance_variable_set(:@before_callbacks, before_callbacks.deep_dup) child_class.instance_variable_set(:@around_callbacks, around_callbacks.deep_dup) child_class.instance_variable_set(:@after_callbacks, after_callbacks.deep_dup) super end
method_added(method_name)
click to toggle source
Calls superclass method
# File lib/method_hooks.rb, line 23 def method_added(method_name) super return if @new_method == false || [:initialize, :call_before_callbacks, :call_around_callbacks, :call_after_callbacks].include?(method_name) method = instance_method(method_name) undef_method(method_name) @new_method = false define_method(method_name) do |*args, &block| call_before_callbacks(method_name) return_value = call_around_callbacks(method_name) { method.bind(self).call(*args, &block) } call_after_callbacks(method_name) return_value end @new_method = true end