Module: Functional::TypeCheck

Defined in:
lib/functional/type_check.rb

Overview

Supplies type-checking helpers whenever included.

See Also:

Class Method Summary (collapse)

Class Method Details

+ (Class) Child!(value, *types)

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



90
91
92
93
94
# File 'lib/functional/type_check.rb', line 90

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

+ (Boolean) Child?(value, *types)

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



76
77
78
79
# File 'lib/functional/type_check.rb', line 76

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

+ (Object) Match!(value, *types)

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



62
63
64
65
66
# File 'lib/functional/type_check.rb', line 62

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

+ (Boolean) Match?(value, *types)

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



45
46
47
# File 'lib/functional/type_check.rb', line 45

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

+ (Object) Type!(value, *types)

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



29
30
31
32
33
# File 'lib/functional/type_check.rb', line 29

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

+ (Boolean) Type?(value, *types)

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



15
16
17
# File 'lib/functional/type_check.rb', line 15

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