Sha256: fc73b96c5e2e7aecae05daf5390ac43a3ed657b2f452e96921498cd0fa0e8a56

Contents?: true

Size: 1.81 KB

Versions: 8

Compression:

Stored size: 1.81 KB

Contents

module Unparser
  class Emitter

    # Emitter for begin nodes
    class Begin < self

      children :body

    private

      # Emit inner nodes
      #
      # @return [undefined]
      #
      # @api private
      #
      def emit_inner
        childs = children
        max = childs.length - 1
        childs.each_with_index do |child, index|
          visit(child)
          nl if index < max
        end
      end

      # Emitter for implicit begins
      class Implicit < self

        handle :begin

      private

        # Perform dispatch
        #
        # @return [undefined]
        #
        # @api private
        #
        def dispatch
          if parentheses?
            parentheses { emit_inner }
          else
            emit_inner
          end
        end

        # Test if begin node needs to be enclosed within parentheses
        #
        # @return [true]
        #   if parentheses are needed
        #
        # @return [false]
        #   otherwise
        #
        # @api private
        #
        def parentheses?
          children.length == 1 && children.first.type == :send && BINARY_OPERATORS.include?(children.first.children[1])
        end

      end # Implicit

      # Emitter for explicit begins
      class Explicit < self

        handle :kwbegin

      private

        # Perform dispatch
        #
        # @return [undefined]
        #
        # @api private
        #
        def dispatch
          write(K_BEGIN)
          emit_body
          k_end
        end

        # Emit body
        #
        # @return [undefined]
        #
        # @api private
        #
        def emit_body
          if NOINDENT.include?(body.type)
            emit_inner
          else
            indented { emit_inner }
          end
        end

      end # Explicit

    end # Begin
  end # Emitter
end # Unparser

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
unparser-0.0.18 lib/unparser/emitter/begin.rb
unparser-0.0.16 lib/unparser/emitter/begin.rb
unparser-0.0.15 lib/unparser/emitter/begin.rb
unparser-0.0.14 lib/unparser/emitter/begin.rb
unparser-0.0.13 lib/unparser/emitter/begin.rb
unparser-0.0.12 lib/unparser/emitter/begin.rb
unparser-0.0.11 lib/unparser/emitter/begin.rb
unparser-0.0.10 lib/unparser/emitter/begin.rb