Method: Wgit::Assertable#assert_types

Defined in:
lib/wgit/assertable.rb

#assert_types(obj, type_or_types, msg = nil) ⇒ Object Also known as: assert_type

Tests if the obj is_a? given type; raises an Exception if not.

Parameters:

  • obj (Object)

    The Object to test.

  • type_or_types (Type, Array<Type>)

    The type/types that obj must belong to or an exception is thrown.

  • msg (String) (defaults to: nil)

    The raised StandardError message, if provided.

Returns:

  • (Object)

    The given obj on successful assertion.

Raises:

  • (StandardError)

    If the assertion fails.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wgit/assertable.rb', line 31

def assert_types(obj, type_or_types, msg = nil)
  msg ||= format(DEFAULT_TYPE_FAIL_MSG, type_or_types, obj.class)
  match = if type_or_types.respond_to?(:any?)
            type_or_types.any? { |type| obj.is_a?(type) }
          else
            obj.is_a?(type_or_types)
          end
  raise msg unless match

  obj
end