Sha256: e9b0213c952c047e12b1f63a0c18c9cb2a733a381a07c800e10e9670636d6c5f

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module MediaTypes
  class Scheme
    class Attribute

      ##
      # An attribute that expects a value of type +type+
      #
      # @see AllowNil
      # @see AnyOf
      #
      # @param [Class] type the class +it+ must be
      # @param [TrueClass, FalseClass] allow_nil if true, nil? is allowed
      #
      def initialize(type, allow_nil: false)
        self.type = allow_nil ? AllowNil(type) : type

        freeze
      end

      def validate!(output, options, **_opts)
        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

      def inspect
        "[Scheme::Attribute of #{type.inspect}]"
      end

      private

      attr_accessor :allow_nil, :type

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
media_types-0.5.1 lib/media_types/scheme/attribute.rb