Skip to content

Instantly share code, notes, and snippets.

@abedra
Created April 2, 2012 21:42
Show Gist options
  • Save abedra/2287439 to your computer and use it in GitHub Desktop.
Save abedra/2287439 to your computer and use it in GitHub Desktop.
class Inspector
# :nodoc:
def self._collect_events_for_method_call(&block)
events = []
set_trace_func lambda { |event, file, line, id, binding, classname|
events << { :event => event, :file => file, :line => line, :id => id, :binding => binding, :classname => classname }
}
begin
yield
ensure
set_trace_func(nil)
end
events
end
# :nodoc:
def self._trace_the_method_call(&block)
events = _collect_events_for_method_call(&block)
valid_event_types = ['call', 'c-call', 'return']
events.each do |event|
next unless valid_event_types.include?(event[:event])
case event[:classname].to_s
when 'ActiveRecord::Base'
return events[-3]
else
return event if event[:event].include?('call')
end
end
end
def self.where_is_this_defined(&block)
trace = _trace_the_method_call(&block)
return "Unable to determine where the method was defined" unless trace
if trace[:event] == 'c-call'
"#{trace[:classname]} method :#{trace[:id]} defined in STDLIB"
else
"#{trace[:classname]} method :#{trace[:id]} defined in #{trace[:file]}:#{trace[:line]}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment