Method: Html2rss::AttributePostProcessors::Base.assert_type

Defined in:
lib/html2rss/attribute_post_processors/base.rb

.assert_type(value, types = [], name, context:) ⇒ Object

Asserts that the value is of the expected type(s)

Parameters:

  • value (Object)

    the value to check

  • types (Array<Class>, Class) (defaults to: [])

    the expected type(s)

  • name (String)

    the name of the option being checked

  • context (Item::Context)

    the context

Raises:

  • (InvalidType)

    if the value is not of the expected type(s)



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/html2rss/attribute_post_processors/base.rb', line 31

def self.assert_type(value, types = [], name, context:)
  types = [types] unless types.is_a?(Array)

  return if types.any? { |type| value.is_a?(type) }

  options = context[:options] if context.is_a?(Hash)
  options ||= { file: File.basename(caller_locations(1, 1).first.absolute_path) }

  raise InvalidType, format('The type of `%<name>s` must be %<types>s, but is: %<type>s in: %<options>s',
                            name:, types: types.join(' or '), type: value.class, options: options.inspect),
        [], cause: nil
end