Created
October 11, 2010 13:09
-
-
Save edelbalso/620481 to your computer and use it in GitHub Desktop.
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 TraceableObject | |
def initialize(obj) | |
@obj = obj | |
end | |
def self.before_init(classname) | |
puts "-- s - #{classname}.initialize() ---" | |
end | |
def self.after_init(classname) | |
puts "-- f - #{classname}.initialize() ---" | |
end | |
def before_trace(sym) | |
puts "-- s - #{@obj.class}.#{sym}() ---" | |
end | |
def after_trace(sym) | |
puts "-- f - #{@obj.class}.#{sym}() ---" | |
end | |
def self.before_trace(className, sym) | |
puts "-- s - #{className}::#{sym}() ---" | |
end | |
def self.after_trace(className, sym) | |
puts "-- f - #{className}::#{sym}() ---" | |
end | |
def method_missing(sym, *args, &block) | |
before_trace(sym) | |
@obj.send sym, *args, &block | |
after_trace(sym) | |
end | |
def self.call_class_method(className, sym, *args, &block) | |
self.before_trace(className, sym) if Rcli.trace_app | |
retval = Object.const_get( className ).send sym, *args, &block | |
self.after_trace(className, sym) if Rcli.trace_app | |
retval | |
end | |
end | |
class TraceableFactory | |
def self.createTraceableObject( className, *args ) | |
TraceableObject.before_init(className) if Rcli.trace_app | |
obj = Object.const_get( className ).new(*args) | |
TraceableObject.after_init(className) if Rcli.trace_app | |
if Rcli.trace_app | |
return TraceableObject.new(obj,*args) | |
else | |
return obj | |
end | |
end | |
end | |
# So instead of MyClass.new( ...) , you use: | |
# | |
# TraceableFactory.createTraceableObject( 'MyClass', ... ) | |
# | |
# and now you can do debug code before and after each method call of your object. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment