Module: Mentor::Outputs

Instance Method Summary collapse

Methods included from OutputHelper

#home_to_tilde, #indent_lines, #lines_from_file, #terminal_width, #valid_var_name

Instance Method Details

#absolute_base_dirObject



7
8
9
# File 'lib/helpers/outputs.rb', line 7

def absolute_base_dir
  Dir.pwd + '/'
end

#app_dirObject



11
12
13
14
15
16
17
# File 'lib/helpers/outputs.rb', line 11

def app_dir
  Mentor.tp.path.               # Full path
    sub(absolute_base_dir, ''). # Remove path up until this file
    split('/')[0..-2].          # Split path into parts and exclude filename
    join('/') +                 # Join with '/'
    '/'                         # Add one more '/' to end of it
end

#backtrace_linesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/helpers/outputs.rb', line 19

def backtrace_lines

  # Mentor.tp.raised_exception.backtrace disappears after accessing it once
  # So we just do it once and then save the result to @@backtrace_lines
  if self.class.class_variable_defined?(:@@backtrace_lines)
    return @@backtrace_lines
  end

  bt_lines = Mentor.tp.raised_exception.
             backtrace[1..-1].                            # Remove first
             grep_v(/bin\/mentor/).                       # Remove mentor involvement
             map { |line| line.sub(Dir.pwd + '/', '') }. # Remove path before current dir
             map { |line| 'from ' + line }                # Add 'from'

  return bt_lines if bt_lines.empty?

  # Make it appear so backtrace came from `<main>' instead of being required by mentor
  bt_lines[-1].gsub!("`<top (required)>'", "`<main>'")

  @@backtrace_lines = bt_lines

end

#calling_methodObject



42
43
44
# File 'lib/helpers/outputs.rb', line 42

def calling_method
  (Mentor.tp.method_id || '<main>').to_s
end

#did_you_mean_textObject



52
53
54
# File 'lib/helpers/outputs.rb', line 52

def did_you_mean_text
  'Did you mean?'
end

#did_you_mean_wordObject



46
47
48
49
50
# File 'lib/helpers/outputs.rb', line 46

def did_you_mean_word
  if Mentor.tp.raised_exception.respond_to? :corrections
    Mentor.tp.raised_exception.corrections.first&.to_s
  end
end

#error_linenoObject



60
61
62
# File 'lib/helpers/outputs.rb', line 60

def error_lineno
  Mentor.tp.lineno.to_s
end

#error_lineno_padded(width) ⇒ Object



56
57
58
# File 'lib/helpers/outputs.rb', line 56

def error_lineno_padded(width)
  "=> #{@lineno}".rjust(width)
end

#file_nameObject



64
65
66
# File 'lib/helpers/outputs.rb', line 64

def file_name
  Mentor.tp.path.split('/').last
end

#horizontal_lineObject



68
69
70
# File 'lib/helpers/outputs.rb', line 68

def horizontal_line
  '' * terminal_width
end

#instance_methodsObject



72
73
74
75
# File 'lib/helpers/outputs.rb', line 72

def instance_methods
  Mentor.tp.raised_exception.receiver.class.instance_methods -
  Mentor.tp.raised_exception.receiver.class.superclass.instance_methods
end

#lineno_subtle_padded(width) ⇒ Object



77
78
79
# File 'lib/helpers/outputs.rb', line 77

def lineno_subtle_padded(width)
  @lineno.to_s.rjust(width)
end

#messageObject



81
82
83
84
85
86
87
# File 'lib/helpers/outputs.rb', line 81

def message
  if Mentor.tp.raised_exception.respond_to? :original_message
    Mentor.tp.raised_exception.original_message
  else
    Mentor.tp.raised_exception.message
  end
end

#method_classObject



99
100
101
102
103
# File 'lib/helpers/outputs.rb', line 99

def method_class
  if Mentor.tp.raised_exception.respond_to? :receiver
    Mentor.tp.raised_exception.receiver.class.to_s
  end
end

#method_class_pluralObject



105
106
107
# File 'lib/helpers/outputs.rb', line 105

def method_class_plural
  pluralize method_class
end

#method_class_superclassObject



109
110
111
# File 'lib/helpers/outputs.rb', line 109

def method_class_superclass
  Mentor.tp.raised_exception.receiver.class.superclass
end

#method_class_superclassesObject



113
114
115
# File 'lib/helpers/outputs.rb', line 113

def method_class_superclasses
  to_sentence(Mentor.tp.raised_exception.receiver.class.ancestors - typical_classes)
end

#method_nameObject



89
90
91
92
93
94
95
96
97
# File 'lib/helpers/outputs.rb', line 89

def method_name
  if Mentor.tp.raised_exception.respond_to? :spell_checker
    if Mentor.tp.raised_exception.spell_checker.respond_to?(:method_name)
      Mentor.tp.raised_exception.spell_checker.method_name.to_s
    else
      raise "cannot determine method name"
    end
  end
end

#relative_base_dirObject



117
118
119
# File 'lib/helpers/outputs.rb', line 117

def relative_base_dir
  home_to_tilde(absolute_base_dir)
end

#ruby_error_classObject



121
122
123
# File 'lib/helpers/outputs.rb', line 121

def ruby_error_class
  Mentor.tp.raised_exception.class.to_s
end

#ruby_error_textObject



125
126
127
# File 'lib/helpers/outputs.rb', line 125

def ruby_error_text
  'Ruby Error:'
end

#var_for_methodObject



129
130
131
132
133
134
135
136
# File 'lib/helpers/outputs.rb', line 129

def var_for_method
  culprit_line = lines_from_file[Mentor.tp.lineno]
  var = culprit_line[/#{valid_var_name}\.#{method_name}/].to_s.chomp(".#{method_name}")
  if var.empty?
    culprit_line[/#{valid_var_name} method_name/].to_s.chomp(".#{method_name}")
  end
  var
end