Sha256: f418b6c923114821473ebe4187d1a5a85c86fed7dbe7491ce5bbabc0a5c94c57

Contents?: true

Size: 1.01 KB

Versions: 11

Compression:

Stored size: 1.01 KB

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 a %<expected>s, got a %<actual>s at %<backtrace>s',
          expected: expected_type,
          actual: type,
          backtrace: backtrace.join('->')
        )
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
media_types-2.0.0 lib/media_types/scheme/output_type_guard.rb
media_types-1.0.0 lib/media_types/scheme/output_type_guard.rb
media_types-0.6.2 lib/media_types/scheme/output_type_guard.rb
media_types-0.6.1 lib/media_types/scheme/output_type_guard.rb
media_types-0.6.0 lib/media_types/scheme/output_type_guard.rb
media_types-0.5.5 lib/media_types/scheme/output_type_guard.rb
media_types-0.5.4 lib/media_types/scheme/output_type_guard.rb
media_types-0.5.3 lib/media_types/scheme/output_type_guard.rb
media_types-0.5.2 lib/media_types/scheme/output_type_guard.rb
media_types-0.5.1 lib/media_types/scheme/output_type_guard.rb
media_types-0.5.0 lib/media_types/scheme/output_type_guard.rb