Class: SeeingIsBelieving::Binary::AnnotateEveryLine

Inherits:
Object
  • Object
show all
Defined in:
lib/seeing_is_believing/binary/annotate_every_line.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, results, options = {}) ⇒ AnnotateEveryLine

Returns a new instance of AnnotateEveryLine.



10
11
12
13
14
15
# File 'lib/seeing_is_believing/binary/annotate_every_line.rb', line 10

def initialize(body, results, options={})
  @options         = options
  @body            = body
  @results         = results
  @interline_align = InterlineAlign.new(results)
end

Class Method Details

.call(body, results, options) ⇒ Object



6
7
8
# File 'lib/seeing_is_believing/binary/annotate_every_line.rb', line 6

def self.call(body, results, options)
  new(body, results, options).call
end

Instance Method Details

#callObject



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
# File 'lib/seeing_is_believing/binary/annotate_every_line.rb', line 17

def call
  @new_body ||= begin
    require 'seeing_is_believing/binary/comment_lines'
    require 'seeing_is_believing/binary/format_comment'
    exception_prefix = @options[:markers][:exception][:prefix]
    value_prefix     = @options[:markers][:value][:prefix]
    exceptions       = Hash.[] @results.exceptions.map { |e| [e.line_number, e] }

    alignment_strategy = @options[:alignment_strategy].new(@body)
    new_body = CommentLines.call @body do |line, line_number|
      exception = exceptions[line_number]
      options   = @options.merge pad_to: alignment_strategy.line_length_for(line_number)
      if exception
        result = sprintf "%s: %s", exception.class_name, exception.message.gsub("\n", '\n')
        FormatComment.call(line.size, exception_prefix, result, options)
      elsif @results[line_number].any?
        if @options[:interline_align]
          result = @interline_align.call line_number, @results[line_number].map { |result| result.gsub "\n", '\n' }
        else
          result = @results[line_number].map { |result| result.gsub "\n", '\n' }.join(', ')
        end
        FormatComment.call(line.size, value_prefix, result, options)
      else
        ''
      end
    end

    require 'seeing_is_believing/binary/annotate_end_of_file'
    AnnotateEndOfFile.add_stdout_stderr_and_exceptions_to new_body, @results, @options

    new_body
  end
end