You can get a callback for any method created with Module # method_added , similar to the old method, and then define a new method that calls before_filter. Here is my (extremely) crude first concept:
module Filter def before_filter name @@filter = name end def method_added name return if @filtering # Don't add filters to original_ methods return if @@filter == name # Don't filter filters return if name == :initialize @filtering = true alias_method :"original_#{name}", name define_method name do |*args| self.send @@filter, name self.send :"original_#{name}", *args end @filtering = false end end class FilterTest extend Filter before_filter :prepare_logs def baz puts "#{@msg_prefix} message goes here" end def prepare_logs name @msg_prefix = "#{self.class}::#{name}" end end ft = FilterTest.new ft.baz
Using __method__ , as you were in create_prefix , you will get the filter method name, not the original method, so you need to pass the method name to. There may be other solutions to make this a little cleaner.
zaius source share