Class: SeeingIsBelieving::Binary::CleanBody

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, should_clean_values) ⇒ CleanBody

Returns a new instance of CleanBody.



15
16
17
18
# File 'lib/seeing_is_believing/binary/clean_body.rb', line 15

def initialize(code, should_clean_values)
  self.should_clean_values = should_clean_values
  self.code                = code
end

Class Method Details

.call(code, should_clean_values) ⇒ Object



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

def self.call(code, should_clean_values)
  new(code, should_clean_values).call
end

Instance Method Details

#callObject



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/clean_body.rb', line 20

def call
  buffer, parser, rewriter = ParserHelpers.initialize_parser code, 'strip_comments'
  comments                 = ParserHelpers.comments_from parser, buffer

  removed_comments      = { result: [], exception: [], stdout: [], stderr: [] }

  comments.each do |comment|
    case comment.text
    when /\A#\s*=>/
      if should_clean_values
        removed_comments[:result] << comment
        rewriter.remove comment.location.expression
      end
    when /\A#\s*~>/
      removed_comments[:exception] << comment
      rewriter.remove comment.location.expression
    when /\A#\s*>>/ then
      removed_comments[:stdout] << comment
      rewriter.remove comment.location.expression
    when /\A#\s*!>/ then
      removed_comments[:stderr] << comment
      rewriter.remove comment.location.expression
    end
  end

  remove_whitespace_preceeding_comments(buffer, rewriter, removed_comments)
  rewriter.process
rescue Parser::SyntaxError => e
  raise SyntaxError, e.message
end