Skip to content

Instantly share code, notes, and snippets.

@ddollar
Created December 11, 2008 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddollar/34880 to your computer and use it in GitHub Desktop.
Save ddollar/34880 to your computer and use it in GitHub Desktop.
# 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