cheap_advice
Add/remove advice around, before or after methods in Ruby.
Example
class MyClass
def foo
42
end
end
class MyOtherClass
def
43
end
end
a = MyClass.new
b = MyOtherClass.new
trace_advice = CheapAdvice.new(:around) do | ar, body |
ar.advice[:log].puts "#{Time.now.iso8601(6)} #{ar.rcvr.class} #{ar.method} #{ar.rcvr.object_id}"
body.call
ar.advice[:log].puts "#{Time.now.iso8601(6)} #{ar.rcvr.class} #{ar.method} #{ar.rcvr.object_id} => #{ar.result.inspect}"
end
trace_advice[:log] = File.open("trace.log", "a+")
trace_advice.advise!(MyClass, :foo)
trace_advice.advise!(MyOtherClass, :bar)
a.foo
b.
trace_advice.disable!
a.foo
b.
trace.log will contain something like:
2011-01-17T16:33:27.882122-06:00 MyClass foo 69872883178280
2011-01-17T16:33:27.882262-06:00 MyClass foo 69872883178280 => 42
2011-01-17T16:33:27.882319-06:00 MyOtherClass bar 69872883178200
2011-01-17T16:33:27.882367-06:00 MyOtherClass bar 69872883178200 => 43
Other Features
-
CheapAdvice::Configuration provides a generic mechanism to configure multiple advices on different methods.
-
CheapAdvice::Trace provides an example advice factory for generic method tracing/logging. See examples/ex02.rb.
Issues
-
Rails-type autoloading can confuse CheapAdvice.
Contributing to cheap_advice
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
-
Fork the project
-
Start a feature/bugfix branch
-
Commit and push until you are happy with your contribution
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright © 2008-2011 Kurt Stephens. See LICENSE.txt for further details.