Last active
August 29, 2015 14:04
-
-
Save amitittyerah/1d593bb3ada1adb9c118 to your computer and use it in GitHub Desktop.
Audit logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AuditableMethodLogger | |
def log(klass, method_name) | |
# Extend the class | |
klass.class_eval do | |
# WARNING - this method will get overritten everytime! | |
alias_method :method, method_name | |
# WARNING - this method is a new method to alias the passed | |
alias_method "#{method_name}_passed", method_name | |
# Dyanmically define a method name or in this case, override | |
define_method method_name do |*args, &block| | |
puts "#{Time.now}: Called #{method_name}" | |
send :method, *args, &block | |
send "#{method_name}_passed", *args, &block | |
end | |
end | |
end | |
end | |
class Something | |
attr_accessor :arg | |
def do_something | |
puts arg | |
end | |
def do_something_else(*args) | |
args.each {|arg| puts arg} | |
end | |
end | |
something = Something.new | |
something.arg = "Testing" | |
audit = AuditableMethodLogger.new | |
audit.log(Something, :do_something) | |
audit.log(Something, :do_something_else) | |
something.do_something | |
something.do_something_else("Hello", "world") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment