Created
December 11, 2008 21:28
-
-
Save ddollar/34880 to your computer and use it in GitHub Desktop.
This file contains 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
# this is the plugin lib | |
module ActsAsTraceable | |
class TracedClass | |
def initialize(object) | |
@object = object | |
end | |
def method_missing(method, *args, &block) | |
puts "[TRACE] START: #{@object.class.to_s}##{method}(#{args.map { |arg| arg.inspect }.join(', ')})" | |
@object.send(method, *args, &block) | |
puts "[TRACE] END: #{@object.class.to_s}##{method}" | |
end | |
end | |
def acts_as_traceable | |
class << self | |
alias_method :__new__, :new | |
def new(*args) | |
TracedClass.new(self.__new__(*args)) | |
end | |
end | |
end | |
end | |
# this is the plugin init | |
class << Object | |
include ActsAsTraceable | |
end | |
# this is your class, just add acts_as_Traceable | |
class Foo | |
acts_as_traceable | |
def fred(foo, bar) | |
puts "before yield" | |
yield | |
puts "after yield" | |
end | |
end | |
# testing | |
Foo.new.fred("foo", Object.new) do | |
puts "during yield" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment