Class: ErrorToCommunicate::FormatTerminal

Inherits:
Object
  • Object
show all
Defined in:
lib/error_to_communicate/format_terminal.rb

Defined Under Namespace

Classes: Code

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ FormatTerminal

Returns a new instance of FormatTerminal.



10
11
12
13
14
15
# File 'lib/error_to_communicate/format_terminal.rb', line 10

def initialize(attributes)
  self.cwd         = attributes.fetch :cwd
  self.theme       = attributes.fetch :theme
  self.heuristic   = attributes.fetch :heuristic
  self.format_code = FormatTerminal::Code.new theme: theme, cwd: cwd # defined below
end

Instance Attribute Details

#cwdObject

Returns the value of attribute cwd.



9
10
11
# File 'lib/error_to_communicate/format_terminal.rb', line 9

def cwd
  @cwd
end

#format_codeObject

Returns the value of attribute format_code.



9
10
11
# File 'lib/error_to_communicate/format_terminal.rb', line 9

def format_code
  @format_code
end

#heuristicObject

Returns the value of attribute heuristic.



9
10
11
# File 'lib/error_to_communicate/format_terminal.rb', line 9

def heuristic
  @heuristic
end

#heuristic_formatterObject

Returns the value of attribute heuristic_formatter.



9
10
11
# File 'lib/error_to_communicate/format_terminal.rb', line 9

def heuristic_formatter
  @heuristic_formatter
end

#themeObject

Returns the value of attribute theme.



9
10
11
# File 'lib/error_to_communicate/format_terminal.rb', line 9

def theme
  @theme
end

Class Method Details

.call(attributes) ⇒ Object



5
6
7
# File 'lib/error_to_communicate/format_terminal.rb', line 5

def self.call(attributes)
  new(attributes).call
end

Instance Method Details

#callObject

Really, it seems like #format should be #call, which implies that there are two objects in here. One that does the semantic formatting, and one that translates that to text. But I can’t quite see it yet, so going to wait until there are more requirements on this.



21
22
23
24
25
26
27
# File 'lib/error_to_communicate/format_terminal.rb', line 21

def call
  format [
    heuristic.semantic_summary,
    heuristic.semantic_info,
    heuristic.semantic_backtrace,
  ]
end

#format(semantic_content) ⇒ Object

TODO: not good enough, These need to be able to take things like:

:message, [“a”, [:explanation, “b”], “c”]

where “a”, and “c”, get coloured with the semantics of :message



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
# File 'lib/error_to_communicate/format_terminal.rb', line 33

def format(semantic_content)
  return semantic_content unless semantic_content.kind_of? Array

  meaning, content, *rest = semantic_content
  case meaning
  when Array        then semantic_content.map { |c| format c }.join
  when :summary     then format([:separator]) + format(content)
  when :heuristic   then format([:separator]) + format(content)
  when :backtrace   then
    if content.any?
      format([:separator]) + format(content)
    else
      format([:separator]) + format([:message, "No backtrace available"]) # TODO: Not tested, I hit this with capybara, when RSpec filtered everything out of the backtrace
    end
  when :separator   then theme.separator_line
  when :columns     then theme.columns     [content, *rest].map { |c| format c }
  when :classname   then theme.classname   format content
  when :message     then theme.message     format content
  when :explanation then theme.explanation format content
  when :context     then theme.context     format content
  when :details     then theme.details     format content
  when :code        then format_code.call  content
  when :null        then ''
  else raise "Wat is #{meaning.inspect}?"
  end
end