Module: Functional::TypeCheck

Defined in:
lib/functional/type_check.rb

Overview

Supplies type-checking helpers whenever included.

See Also:

Since:

  • 1.0.0

Class Method Summary collapse

Class Method Details

.Child!(value, *types) ⇒ Class

Is the given class a subclass or exact match of one or more of the modules and/or classes in the given list?

Parameters:

  • value (Class)

    the class to interrogate

  • types (Class)

    zero or more classes to check the value against

Returns:

  • (Class)

    the value class

Raises:

  • (Functional::TypeError)

    when the check fails

Since:

  • 1.0.0



92
93
94
95
96
# File 'lib/functional/type_check.rb', line 92

def Child!(value, *types)
  Child?(value, *types) or
    TypeCheck.error(value, 'is not child', types)
  value
end

.Child?(value, *types) ⇒ Boolean

Is the given class a subclass or exact match of one or more of the modules and/or classes in the given list?

Parameters:

  • value (Class)

    the class to interrogate

  • types (Class)

    zero or more classes to check the value against the value against

Returns:

  • (Boolean)

    true on success

Since:

  • 1.0.0



78
79
80
81
# File 'lib/functional/type_check.rb', line 78

def Child?(value, *types)
  Type?(value, Class) &&
    types.any? { |t| value <= t }
end

.Match!(value, *types) ⇒ Object

Is the given value object is an instance of or descendant of one of the classes/modules in the given list? Raises an exception on failure.

Performs the check using the === operator.

Parameters:

  • value (Object)

    the object to interrogate

  • types (Module)

    zero or more modules and/or classes to check the value against

Returns:

  • (Object)

    the value object

Raises:

  • (Functional::TypeError)

    when the check fails

Since:

  • 1.0.0



64
65
66
67
68
# File 'lib/functional/type_check.rb', line 64

def Match!(value, *types)
  Match?(value, *types) or
    TypeCheck.error(value, 'is not matching', types)
  value
end

.Match?(value, *types) ⇒ Boolean

Is the given value object is an instance of or descendant of one of the classes/modules in the given list?

Performs the check using the === operator.

Parameters:

  • value (Object)

    the object to interrogate

  • types (Module)

    zero or more modules and/or classes to check the value against

Returns:

  • (Boolean)

    true on success

Since:

  • 1.0.0



47
48
49
# File 'lib/functional/type_check.rb', line 47

def Match?(value, *types)
  types.any? { |t| t === value }
end

.Type!(value, *types) ⇒ Object

Performs an is_a? check of the given value object against the given list of modules and/or classes. Raises an exception on failure.

Parameters:

  • value (Object)

    the object to interrogate

  • types (Module)

    zero or more modules and/or classes to check the value against

Returns:

  • (Object)

    the value object

Raises:

  • (Functional::TypeError)

    when the check fails

Since:

  • 1.0.0



31
32
33
34
35
# File 'lib/functional/type_check.rb', line 31

def Type!(value, *types)
  Type?(value, *types) or
    TypeCheck.error(value, 'is not', types)
  value
end

.Type?(value, *types) ⇒ Boolean

Performs an is_a? check of the given value object against the given list of modules and/or classes.

Parameters:

  • value (Object)

    the object to interrogate

  • types (Module)

    zero or more modules and/or classes to check the value against

Returns:

  • (Boolean)

    true on success

Since:

  • 1.0.0



17
18
19
# File 'lib/functional/type_check.rb', line 17

def Type?(value, *types)
  types.any? { |t| value.is_a? t }
end