Class: SeeingIsBelieving::Binary::FormatComment

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

Overview

not sure I like this name, it formats comments that show results e.g. “# => [1, 2, 3]”

line_length is the length of the line this comment is being appended to

For examples of what the options are, and how they all fit together, see spec/binary/format_comment.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_length, separator, result, options) ⇒ FormatComment

Returns a new instance of FormatComment.



15
16
17
18
19
20
# File 'lib/seeing_is_believing/binary/format_comment.rb', line 15

def initialize(line_length, separator, result, options)
  self.line_length = line_length
  self.separator   = separator
  self.result      = result
  self.options     = options
end

Class Method Details

.call(*args) ⇒ Object



11
12
13
# File 'lib/seeing_is_believing/binary/format_comment.rb', line 11

def self.call(*args)
  new(*args).call
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/seeing_is_believing/binary/format_comment.rb', line 22

def call
  @formatted ||= begin
    formatted = escape_non_printable result, chars_not_to_escape
    formatted = truncate "#{separator}#{formatted}", max_result_length
    formatted = "#{' '*padding_length}#{formatted}"
    formatted = truncate formatted, max_line_length
    formatted = '' unless formatted.sub(/^ */, '').start_with? separator
    formatted
  end
end