Sha256: 81443a0acb6ca76bc8c4fe63290e9ead11f6042dc02b973acbc781753d965ba4

Contents?: true

Size: 1.33 KB

Versions: 24

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Unparser
  class Emitter
    # Dynamic execute string literal emitter
    class XStr < self

      handle :xstr

    private

      def dispatch
        if heredoc?
          emit_heredoc
        else
          emit_xstr
        end
      end

      def heredoc?
        children.any? { |node| node.eql?(s(:str, '')) }
      end

      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

      def emit_xstr
        write('`')
        children.each do |child|
          if n_begin?(child)
            emit_begin(child)
          else
            emit_string(child)
          end
        end
        write('`')
      end

      def emit_string(value)
        write(escape_xstr(value.children.first))
      end

      def escape_xstr(input)
        input.chars.map do |char|
          if char.eql?('`')
            '\\`'
          else
            char
          end
        end.join
      end

      def emit_begin(component)
        write('#{')
        visit(unwrap_single_begin(component))
        write('}')
      end
    end # XStr
  end # Emitter
end # Unparser

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
unparser-0.6.15 lib/unparser/emitter/xstr.rb
unparser-0.6.14 lib/unparser/emitter/xstr.rb
unparser-0.6.13 lib/unparser/emitter/xstr.rb
unparser-0.6.12 lib/unparser/emitter/xstr.rb
unparser-0.6.10 lib/unparser/emitter/xstr.rb
unparser-0.6.9 lib/unparser/emitter/xstr.rb
unparser-0.6.8 lib/unparser/emitter/xstr.rb
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/unparser-0.6.7/lib/unparser/emitter/xstr.rb
unparser-0.6.7 lib/unparser/emitter/xstr.rb
unparser-0.6.6 lib/unparser/emitter/xstr.rb
unparser-0.6.5 lib/unparser/emitter/xstr.rb
unparser-0.6.4 lib/unparser/emitter/xstr.rb
unparser-0.6.3 lib/unparser/emitter/xstr.rb
unparser-0.6.2 lib/unparser/emitter/xstr.rb
unparser-0.6.1 lib/unparser/emitter/xstr.rb
unparser-0.6.0 lib/unparser/emitter/xstr.rb
unparser-0.5.7 lib/unparser/emitter/xstr.rb
unparser-0.5.6 lib/unparser/emitter/xstr.rb
unparser-0.5.5 lib/unparser/emitter/xstr.rb
unparser-0.5.4 lib/unparser/emitter/xstr.rb