Sha256: 0b81c871ca709e9f5f5f5f289a56fb3f90d347fb96e6a4a2041d97e455dc6682

Contents?: true

Size: 1.7 KB

Versions: 8

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Unparser
  class Emitter
    # Base class for primitive emitters
    class Primitive < self

      children :value

      # Emitter for primitives based on Object#inspect
      class Inspect < self

        handle :sym, :str

      private

        def dispatch
          write(value.inspect)
        end

      end # Inspect

      # Emitter for complex literals
      class Complex < self

        handle :complex

        RATIONAL_FORMAT = 'i'.freeze

        MAP =
          IceNine.deep_freeze(
            ::Float    => :float,
            ::Rational => :rational,
            ::Integer  => :int
          )

      private

        def dispatch
          emit_imaginary
          write(RATIONAL_FORMAT)
        end

        def emit_imaginary
          visit(imaginary_node)
        end

        def imaginary_node
          imaginary = value.imaginary
          s(MAP.fetch(imaginary.class), imaginary)
        end

      end # Rational

      # Emitter for rational literals
      class Rational < self

        handle :rational

        RATIONAL_FORMAT = 'r'.freeze

      private

        # rubocop:disable Lint/FloatComparison
        def dispatch
          integer = Integer(value)
          float   = value.to_f

          write_rational(integer.to_f.equal?(float) ? integer : float)
        end
        # rubocop:enable Lint/FloatComparison

        def write_rational(value)
          write(value.to_s, RATIONAL_FORMAT)
        end

      end # Rational

      # Emiter for numeric literals
      class Numeric < self

        handle :int

      private

        def dispatch
          write(value.inspect)
        end

      end # Numeric
    end # Primitive
  end # Emitter
end # Unparser

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
unparser-0.5.7 lib/unparser/emitter/primitive.rb
unparser-0.5.6 lib/unparser/emitter/primitive.rb
unparser-0.5.5 lib/unparser/emitter/primitive.rb
unparser-0.5.4 lib/unparser/emitter/primitive.rb
unparser-0.5.3 lib/unparser/emitter/primitive.rb
unparser-0.5.2 lib/unparser/emitter/primitive.rb
unparser-0.5.1 lib/unparser/emitter/primitive.rb
unparser-0.5.0 lib/unparser/emitter/primitive.rb