Class: MediaTypes::Scheme::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/media_types/scheme/attribute.rb

Instance Method Summary collapse

Constructor Details

#initialize(type, allow_nil: false) ⇒ Attribute

An attribute that expects a value of type type

Parameters:

  • type (Class)

    the class it must be

  • allow_nil (TrueClass, FalseClass) (defaults to: false)

    if true, nil? is allowed

See Also:



16
17
18
19
20
21
# File 'lib/media_types/scheme/attribute.rb', line 16

def initialize(type, allow_nil: false)
  self.type = type
  self.allow_nil = allow_nil

  freeze
end

Instance Method Details

#validate!(output, options, **_opts) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/media_types/scheme/attribute.rb', line 23

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