module Functional::TypeCheck

Supplies type-checking helpers whenever included.

@see ruby-concurrency.github.io/concurrent-ruby/Concurrent/Actor/TypeCheck.html TypeCheck in Concurrent Ruby

Public Class Methods

Child!(value, *types) click to toggle source

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

@param [Class] value the class to interrogate @param [Class] types zero or more classes to check the value against @return [Class] the value class

@raise [Functional::TypeError] when the check fails

# 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
Child?(value, *types) click to toggle source

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

@param [Class] value the class to interrogate @param [Class] types zero or more classes to check the value against

the value against

@return [Boolean] true on success

# File lib/functional/type_check.rb, line 76
def Child?(value, *types)
  Type?(value, Class) &&
    types.any? { |t| value <= t }
end
Match!(value, *types) click to toggle source

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.

@param [Object] value the object to interrogate @param [Module] types zero or more modules and/or classes to check

the value against

@return [Object] the value object

@raise [Functional::TypeError] when the check fails

# 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
Match?(value, *types) click to toggle source

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.

@param [Object] value the object to interrogate @param [Module] types zero or more modules and/or classes to check

the value against

@return [Boolean] true on success

# File lib/functional/type_check.rb, line 45
def Match?(value, *types)
  types.any? { |t| t === value }
end
Type!(value, *types) click to toggle source

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

@param [Object] value the object to interrogate @param [Module] types zero or more modules and/or classes to check

the value against

@return [Object] the value object

@raise [Functional::TypeError] when the check fails

# File lib/functional/type_check.rb, line 29
def Type!(value, *types)
  Type?(value, *types) or
    TypeCheck.error(value, 'is not', types)
  value
end
Type?(value, *types) click to toggle source

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

@param [Object] value the object to interrogate @param [Module] types zero or more modules and/or classes to check

the value against

@return [Boolean] true on success

# File lib/functional/type_check.rb, line 15
def Type?(value, *types)
  types.any? { |t| value.is_a? t }
end

Private Class Methods

error(value, message, types) click to toggle source

Create a {Functional::TypeError} object from the given data.

@param [Object] value the class/method that was being interrogated @param [String] message the message fragment to inject into the error @param [Object] types list of modules and/or classes that were being

checked against the value object

@raise [Functional::TypeError] the formatted exception object

# File lib/functional/type_check.rb, line 107
def self.error(value, message, types)
  raise TypeError,
    "Value (#{value.class}) '#{value}' #{message} any of: #{types.join('; ')}."
end

Private Instance Methods

Child!(value, *types) click to toggle source

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

@param [Class] value the class to interrogate @param [Class] types zero or more classes to check the value against @return [Class] the value class

@raise [Functional::TypeError] when the check fails

# 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
Child?(value, *types) click to toggle source

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

@param [Class] value the class to interrogate @param [Class] types zero or more classes to check the value against

the value against

@return [Boolean] true on success

# File lib/functional/type_check.rb, line 76
def Child?(value, *types)
  Type?(value, Class) &&
    types.any? { |t| value <= t }
end
Match!(value, *types) click to toggle source

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.

@param [Object] value the object to interrogate @param [Module] types zero or more modules and/or classes to check

the value against

@return [Object] the value object

@raise [Functional::TypeError] when the check fails

# 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
Match?(value, *types) click to toggle source

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.

@param [Object] value the object to interrogate @param [Module] types zero or more modules and/or classes to check

the value against

@return [Boolean] true on success

# File lib/functional/type_check.rb, line 45
def Match?(value, *types)
  types.any? { |t| t === value }
end
Type!(value, *types) click to toggle source

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

@param [Object] value the object to interrogate @param [Module] types zero or more modules and/or classes to check

the value against

@return [Object] the value object

@raise [Functional::TypeError] when the check fails

# File lib/functional/type_check.rb, line 29
def Type!(value, *types)
  Type?(value, *types) or
    TypeCheck.error(value, 'is not', types)
  value
end
Type?(value, *types) click to toggle source

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

@param [Object] value the object to interrogate @param [Module] types zero or more modules and/or classes to check

the value against

@return [Boolean] true on success

# File lib/functional/type_check.rb, line 15
def Type?(value, *types)
  types.any? { |t| value.is_a? t }
end