Permalink
Browse files

refactor caller_files

- supports external modification of ignores list (e.g. to include polyglot)
- provides additional caller_locations method to include line numbers (e.g. for render backtraces)
  • Loading branch information...
1 parent d6ceeb0 commit 9d67449452141aa4f47ffbc1a8fc3e560bb528c0 S. Brent Faulkner committed with rtomayko Apr 5, 2009
Showing with 14 additions and 8 deletions.
  1. +14 −8 lib/sinatra/base.rb
View
@@ -899,18 +899,24 @@ def metadef(message, &block)
send :define_method, message, &block
end
+ CALLERS_TO_IGNORE = [
+ /lib\/sinatra.*\.rb$/, # all sinatra code
+ /\(.*\)/, # generated code
+ /custom_require\.rb$/, # rubygems require hacks
+ /active_support/, # active_support require hacks
+ ] unless self.const_defined?('CALLERS_TO_IGNORE')
+
# Like Kernel#caller but excluding certain magic entries and without
# line / method information; the resulting array contains filenames only.
def caller_files
- ignore = [
- /lib\/sinatra.*\.rb$/, # all sinatra code
- /\(.*\)/, # generated code
- /custom_require\.rb$/, # rubygems require hacks
- /active_support/, # active_support require hacks
- ]
+ caller_locations.
+ map { |file,line| file }
+ end
+
+ def caller_locations
caller(1).
- map { |line| line.split(/:\d/, 2).first }.
- reject { |file| ignore.any? { |pattern| file =~ pattern } }
+ map { |line| line.split(/:(?=\d|in )/)[0,2] }.
+ reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
end
end

0 comments on commit 9d67449

Please sign in to comment.