class Unparser::Emitter::XStr

Dynamic execute string literal emitter

Private Instance Methods

dispatch() click to toggle source
# File lib/unparser/emitter/xstr.rb, line 12
def dispatch
  if heredoc?
    emit_heredoc
  else
    emit_xstr
  end
end
emit_begin(component) click to toggle source
# File lib/unparser/emitter/xstr.rb, line 65
def emit_begin(component)
  write('#{')
  visit(unwrap_single_begin(component))
  write('}')
end
emit_heredoc() click to toggle source
# File lib/unparser/emitter/xstr.rb, line 24
def emit_heredoc
  write(%(<<~`HEREDOC`))
  buffer.indent
  nl
  children.each do |child|
    if n_str?(child)
      write(child.children.first)
    else
      emit_begin(child)
    end
  end
  buffer.unindent
  write("HEREDOC\n")
end
emit_string(value) click to toggle source
# File lib/unparser/emitter/xstr.rb, line 51
def emit_string(value)
  write(escape_xstr(value.children.first))
end
emit_xstr() click to toggle source
# File lib/unparser/emitter/xstr.rb, line 39
def emit_xstr
  write('`')
  children.each do |child|
    if n_begin?(child)
      emit_begin(child)
    else
      emit_string(child)
    end
  end
  write('`')
end
escape_xstr(input) click to toggle source
# File lib/unparser/emitter/xstr.rb, line 55
def escape_xstr(input)
  input.chars.map do |char|
    if char.eql?('`')
      '\\`'
    else
      char
    end
  end.join
end
heredoc?() click to toggle source
# File lib/unparser/emitter/xstr.rb, line 20
def heredoc?
  children.any? { |node| node.eql?(s(:str, '')) }
end