Class: Ariadne::Forms::BufferRewriter

Inherits:
Ripper
  • Object
show all
Defined in:
lib/ariadne/forms/buffer_rewriter.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.rewrite(code) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ariadne/forms/buffer_rewriter.rb', line 10

def rewrite(code)
  parser = new(code, "(code)", 0)
  parser.parse

  line_offsets = calc_line_offsets(code)

  code.dup.tap do |result|
    parser.var_refs.reverse_each do |lineno, stop|
      line_offset = line_offsets[lineno]
      stop += line_offset
      stop -= 1 if stop < code.length
      start = stop - "@output_buffer".length
      result[start...stop] = "output_buffer"
    end
  end
end

Instance Method Details

#on_var_ref(var) ⇒ Object



40
41
42
43
44
# File 'lib/ariadne/forms/buffer_rewriter.rb', line 40

def on_var_ref(var)
  return unless var == "@output_buffer"

  var_refs << [lineno, column]
end

#var_refsObject



46
47
48
# File 'lib/ariadne/forms/buffer_rewriter.rb', line 46

def var_refs
  @var_refs ||= []
end