Sha256: bf324f801614bc560166a38a4b98fe56cbca728c803451b3bd655112e9fab819

Contents?: true

Size: 990 Bytes

Versions: 1

Compression:

Stored size: 990 Bytes

Contents

# frozen_string_literal: true

require 'media_types/scheme/errors'

module MediaTypes
  class Scheme
    class OutputTypeGuard
      class << self
        def call(*args, **opts, &block)
          new(*args, **opts).call(&block)
        end
      end

      def initialize(output, options, rules:)
        self.output = output
        self.options = options
        self.expected_type = rules.expected_type
      end

      def call
        return unless expected_type && !(expected_type === output) # rubocop:disable Style/CaseEquality
        raise_type_error!(type: output.class, backtrace: options.backtrace)
      end

      private

      attr_accessor :output, :options, :expected_type

      def raise_type_error!(type:, backtrace:)
        raise OutputTypeMismatch, format(
          'Expected %<expected>s, got %<actual>s at %<backtrace>s',
          expected: expected_type,
          actual: type,
          backtrace: backtrace.join('->')
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
media_types-2.3.0 lib/media_types/scheme/output_type_guard.rb