Class: RailsInfo::StackTrace

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_info/stack_trace.rb

Instance Method Summary collapse

Constructor Details

#initialize(stack_trace, options = {}) ⇒ StackTrace

Returns a new instance of StackTrace.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rails_info/stack_trace.rb', line 2

def initialize(stack_trace, options = {})
  @request = options[:request]
  @exception = options[:exception]
  @rails_root = options[:rails_root] || Rails.root.to_s
  @lines_of_code_around = options[:lines_of_code_around] || 5
  @show_all = options[:show_all]
  
  if stack_trace.is_a?(String) && stack_trace.match('#')
    # parse RSpec style stack trace "# ./app/observers/assignment_observer.rb:65:in `after_save'"
    line_separator = if stack_trace.match(/\r\n/) 
      "\r\n"
    elsif stack_trace.match(/\\n/)
      /\\n/
    else 
      "\n"
    end
    
    stack_trace = stack_trace.split(line_separator).map{|line|line.gsub('#', '').gsub('./', rails_root + '/')}.map(&:strip)
  elsif stack_trace.is_a?(String) && !stack_trace.strip.blank?
    raise NotImplementedError.new(stack_trace.inspect)
  end
  
  @stack_trace = stack_trace
end

Instance Method Details

#exceptionObject



27
28
29
# File 'lib/rails_info/stack_trace.rb', line 27

def exception
  @exception
end

#hashObject



50
51
52
53
54
55
56
57
58
# File 'lib/rails_info/stack_trace.rb', line 50

def hash
  return @hash if @hash
  
  @hash = {}
  
  parse_stack_trace
  
  @hash
end

#lines_of_code_aroundObject



46
47
48
# File 'lib/rails_info/stack_trace.rb', line 46

def lines_of_code_around
  @lines_of_code_around
end

#messageObject



42
43
44
# File 'lib/rails_info/stack_trace.rb', line 42

def message
  @exception.message
end

#rails_rootObject



68
69
70
# File 'lib/rails_info/stack_trace.rb', line 68

def rails_root
  @rails_root
end

#requestObject



60
61
62
# File 'lib/rails_info/stack_trace.rb', line 60

def request
  @request
end

#responseObject



64
65
66
# File 'lib/rails_info/stack_trace.rb', line 64

def response
  @response
end

#titleObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_info/stack_trace.rb', line 31

def title
  text = @exception.class.to_s
  
  if @request && @request.parameters['controller']
    action = @request.parameters['action'].present? ? "##{@request.parameters['action']}" : ''
    text += " in #{@request.parameters['controller'].camelize}Controller#{action}"
  end
  
  text
end