Sha256: a11c3964982bdfc8f1171afa52cc42be88ac7e7472721b34eaf0124dbd4fff9f

Contents?: true

Size: 975 Bytes

Versions: 5

Compression:

Stored size: 975 Bytes

Contents

# frozen_string_literal: true

require 'media_types/scheme/errors'
require 'media_types/object'

module MediaTypes
  class Scheme
    class OutputEmptyGuard
      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.rules = rules
      end

      def call
        return unless MediaTypes::Object.new(output).empty?
        throw(:end, true) if allow_empty?
        raise_empty!(backtrace: options.backtrace)
      end

      private

      attr_accessor :output, :options, :rules

      def allow_empty?
        rules.allow_empty? || rules.required.empty?
      end

      def raise_empty!(backtrace:)
        raise EmptyOutputError, format('Expected output, got empty at %<backtrace>s', backtrace: backtrace.join('->'))
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
media_types-0.5.4 lib/media_types/scheme/output_empty_guard.rb
media_types-0.5.3 lib/media_types/scheme/output_empty_guard.rb
media_types-0.5.2 lib/media_types/scheme/output_empty_guard.rb
media_types-0.5.1 lib/media_types/scheme/output_empty_guard.rb
media_types-0.5.0 lib/media_types/scheme/output_empty_guard.rb