forked from flyerhzm/bullet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_trace_filter.rb
71 lines (62 loc) · 1.82 KB
/
stack_trace_filter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true
module Bullet
module StackTraceFilter
VENDOR_PATH = '/vendor'
def caller_in_project
vendor_root = Bullet.app_root + VENDOR_PATH
bundler_path = Bundler.bundle_path.to_s
select_caller_locations do |location|
caller_path = location_as_path(location)
caller_path.include?(Bullet.app_root) && !caller_path.include?(vendor_root) && !caller_path.include?(bundler_path) ||
Bullet.stacktrace_includes.any? { |include_pattern| pattern_matches?(location, include_pattern) }
end
end
def excluded_stacktrace_path?
Bullet.stacktrace_excludes.any? do |exclude_pattern|
caller_in_project.any? { |location| pattern_matches?(location, exclude_pattern) }
end
end
private
def pattern_matches?(location, pattern)
path = location_as_path(location)
case pattern
when Array
pattern_path = pattern.first
filter = pattern.last
return false unless pattern_matches?(location, pattern_path)
case filter
when Range
filter.include?(location.lineno)
when Integer
filter == location.lineno
when String
filter == location.base_label
end
when String
path.include?(pattern)
when Regexp
path =~ pattern
end
end
def location_as_path(location)
ruby_19? ? location : location.absolute_path.to_s
end
def select_caller_locations
if ruby_19?
caller.select do |caller_path|
yield caller_path
end
else
caller_locations.select do |location|
yield location
end
end
end
def ruby_19?
if @ruby_19.nil?
@ruby_19 = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0')
end
@ruby_19
end
end
end