Class: RorVsWild::Locator

Inherits:
Object
  • Object
show all
Defined in:
lib/rorvswild/locator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_path = Dir.pwd) ⇒ Locator

Returns a new instance of Locator.


5
6
7
# File 'lib/rorvswild/locator.rb', line 5

def initialize(current_path = Dir.pwd)
  @current_path = File.join(current_path, "")
end

Instance Attribute Details

#current_pathObject (readonly)

Returns the value of attribute current_path


3
4
5
# File 'lib/rorvswild/locator.rb', line 3

def current_path
  @current_path
end

Instance Method Details

#find_most_relevant_file_and_line(locations) ⇒ Object


9
10
11
12
# File 'lib/rorvswild/locator.rb', line 9

def find_most_relevant_file_and_line(locations)
  location = find_most_relevant_location(locations)
  [relative_path(location.path), location.lineno]
end

#find_most_relevant_file_and_line_from_array_of_strings(stack) ⇒ Object


30
31
32
33
34
# File 'lib/rorvswild/locator.rb', line 30

def find_most_relevant_file_and_line_from_array_of_strings(stack)
  location = stack.find { |str| relevant_path?(str) }
  location ||= stack.find { |str| !str.start_with?(rorvswild_lib_path) }
  relative_path(location || stack.first).split(":".freeze)
end

#find_most_relevant_file_and_line_from_exception(exception) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rorvswild/locator.rb', line 18

def find_most_relevant_file_and_line_from_exception(exception)
  # Exception#backtrace_locations is faster but exists since 2.1.0.
  # Sometime Exception#backtrace_locations returns nil for an unknow reason. So we fallback to the old way.
  if exception.respond_to?(:backtrace_locations) && locations = exception.backtrace_locations
    find_most_relevant_file_and_line(locations)
  elsif (backtrace = exception.backtrace) && backtrace.size > 0
    find_most_relevant_file_and_line_from_array_of_strings(backtrace)
  else
    ["No backtrace".freeze, 1]
  end
end

#find_most_relevant_location(locations) ⇒ Object


14
15
16
# File 'lib/rorvswild/locator.rb', line 14

def find_most_relevant_location(locations)
  locations.find { |l| relevant_path?(l.path) } || locations.find { |l| !l.path.start_with?(rorvswild_lib_path) } || locations.first
end

#irrelevant_path?(path) ⇒ Boolean

Returns:

  • (Boolean)

44
45
46
# File 'lib/rorvswild/locator.rb', line 44

def irrelevant_path?(path)
  path.start_with?(*lib_paths)
end

#lib_pathsObject


48
49
50
# File 'lib/rorvswild/locator.rb', line 48

def lib_paths
  @lib_paths ||= initialize_lib_paths
end

#relative_path(path) ⇒ Object


36
37
38
# File 'lib/rorvswild/locator.rb', line 36

def relative_path(path)
  path.start_with?(current_path) ? path.sub(current_path, "".freeze) : path
end

#relevant_path?(path) ⇒ Boolean

Returns:

  • (Boolean)

40
41
42
# File 'lib/rorvswild/locator.rb', line 40

def relevant_path?(path)
  path.start_with?(current_path) && !irrelevant_path?(path)
end

#rorvswild_lib_pathObject


52
53
54
# File 'lib/rorvswild/locator.rb', line 52

def rorvswild_lib_path
  @rorvswild_lib_path ||= File.dirname(File.expand_path(__FILE__))
end