Sha256: d8c3b19e9775afe08bb9ebd3edc1e4ff1fca572e573cfa7de36eb289229ebe22
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
require "method_callbacks/finder" require "method_callbacks/version" module MethodCallbacks ALIAS_PREFIX = "__method_callback_alias_to_original" def self.included(base) base.extend(ClassMethods) end module ClassMethods def after_method(method_name, *callback_names) define(method_name, :after, callback_names) define_with_block(method_name, :after, &Proc.new) if block_given? end def around_method(method_name, *callback_names) define(method_name, :around, callback_names) end def before_method(method_name, *callback_names) define(method_name, :before, callback_names) define_with_block(method_name, :before, &Proc.new) if block_given? end private def define(method_name, type, callback_names) find_or_new(method_name).define(type, callback_names) end def define_with_block(method_name, type) find_or_new(method_name).define_with_block(type, &Proc.new) end def find(method_name) finder(method_name).find end def finder(method_name) @_finder ||= {} @_finder[method_name] ||= MethodCallbacks::Finder.new(method_name) end def find_or_new(method_name) finder(method_name).find_or_new end def method_added(method_name) redefine_method(find(method_name)) super end def redefine_method(method) return if !method || method.locked? method.lock! && alias_method("#{ALIAS_PREFIX}_#{method.name}", method.name) define_method(method.name) do method.execute(:before, self) return_value = method.execute(:around, self) do send("#{ALIAS_PREFIX}_#{method.name}") end method.execute(:after, self) return_value end method.unlock! end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
method_callbacks-1.0.0 | lib/method_callbacks.rb |