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:



19
20
21
22
23
# File 'lib/media_types/scheme/attribute.rb', line 19

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

  freeze
end

Instance Method Details

#inspectObject



36
37
38
# File 'lib/media_types/scheme/attribute.rb', line 36

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

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

Raises:



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

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