Sha256: 85c37a9b00f7682e24253fa4e7412da2bf80d7e28e7b313954573fd584ae18bb
Contents?: true
Size: 902 Bytes
Versions: 364
Compression:
Stored size: 902 Bytes
Contents
class Object def self.method_hook(*args) options = args.extract_options! return unless (options[:before].present? or options[:after].present?) args.each do |method_name| old_method = instance_method(method_name) rescue next define_method(method_name) do |*args| # invoke before callback if options[:before].present? options[:before].is_a?(Proc) ? options[:before].call(method_name, self): send(options[:before], method_name) end # you can modify the code to call after callback # only when the old method returns true etc.. old_method.bind(self).call(*args) # invoke after callback if options[:after].present? options[:after].is_a?(Proc) ? options[:after].call(method_name, self): send(options[:after], method_name) end end end end end
Version data entries
364 entries across 364 versions & 4 rubygems