Sha256: 55ace95c62f8bd00ab457937eb6532b7ee242bcda7cf599b04e6a87f0a2cd0f1

Contents?: true

Size: 791 Bytes

Versions: 4

Compression:

Stored size: 791 Bytes

Contents

# frozen_string_literal: true

module MediaTypes
  class Scheme
    class Attribute
      def initialize(type, allow_nil: false)
        self.type = type
        self.allow_nil = allow_nil

        freeze
      end

      def validate!(output, options, **_opts)
        if output.nil?
          return true if allow_nil
        end

        return true if type === output # rubocop:disable Style/CaseEquality
        raise ValidationError,
              format(
                'Expected %<type>s, got %<actual>s at [%<backtrace>s]',
                type: type,
                actual: output.inspect,
                backtrace: options.backtrace.join('->')
              )
      end

      private

      attr_accessor :allow_nil, :type

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
media_types-0.1.3 lib/media_types/scheme/attribute.rb
media_types-0.1.2 lib/media_types/scheme/attribute.rb
media_types-0.1.1 lib/media_types/scheme/attribute.rb
media_types-0.1.0 lib/media_types/scheme/attribute.rb