class Stannum::Constraints::Type

A type constraint asserts that the object is of the expected type.

@example Using a Type constraint with a Class

constraint = Stannum::Constraints::Type.new(StandardError)

constraint.matches?(nil)               # => false
constraint.matches?(Object.new)        # => false
constraint.matches?(StandardError.new) # => true
constraint.matches?(RuntimeError.new)  # => true

@example Using a Type constraint with a Module

constraint = Stannum::Constraints::Type.new(Enumerable)

constraint.matches?(nil)                           #=> false
constraint.matches?(Object.new)                    #=> false
constraint.matches?(Array)                         #=> true
constraint.matches?(Object.new.extend(Enumerable)) #=> true

@example Using a Type constraint with a Class name

constraint = Stannum::Constraints::Type.new('StandardError')

constraint.matches?(nil)               # => false
constraint.matches?(Object.new)        # => false
constraint.matches?(StandardError.new) # => true
constraint.matches?(RuntimeError.new)  # => true

Constants

NEGATED_TYPE

The :type of the error generated for a matching object.

TYPE

The :type of the error generated for a non-matching object.

Public Class Methods

new(expected_type, optional: nil, required: nil, **options) click to toggle source

@param expected_type [Class, Module, String] The type the object is

expected to belong to. Can be a Class or a Module, or the name of a
class or module.

@param options [Hash<Symbol, Object>] Configuration options for the

constraint. Defaults to an empty Hash.
Calls superclass method Stannum::Constraints::Base::new
# File lib/stannum/constraints/type.rb, line 46
def initialize(expected_type, optional: nil, required: nil, **options)
  expected_type = resolve_expected_type(expected_type)

  super(
    expected_type: expected_type,
    **resolve_required_option(
      optional: optional,
      required: required,
      **options
    )
  )
end

Public Instance Methods

errors_for(actual, errors: nil) click to toggle source

(see Stannum::Constraints::Base#errors_for)

# File lib/stannum/constraints/type.rb, line 60
def errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument
  (errors || Stannum::Errors.new).add(type, **error_properties)
end
expected_type() click to toggle source

@return [Class, Module, String] the type the object is expected to belong

to.
# File lib/stannum/constraints/type.rb, line 66
def expected_type
  options[:expected_type]
end
match?(actual)
Alias for: matches?
matches?(actual) click to toggle source

Checks that the object is an instance of the expected type.

@return [true, false] true if the object is an instance of the expected

type, otherwise false.

@see Stannum::Constraint#matches?

# File lib/stannum/constraints/type.rb, line 76
def matches?(actual)
  matches_type?(actual)
end
Also aliased as: match?
negated_errors_for(actual, errors: nil) click to toggle source

(see Stannum::Constraints::Base#negated_errors_for)

# File lib/stannum/constraints/type.rb, line 82
def negated_errors_for(actual, errors: nil) # rubocop:disable Lint/UnusedMethodArgument
  (errors || Stannum::Errors.new).add(negated_type, **error_properties)
end
with_options(**options) click to toggle source

(see Stannum::Constraints::Base#with_options)

Calls superclass method Stannum::Constraints::Base#with_options
# File lib/stannum/constraints/type.rb, line 87
def with_options(**options)
  options = options.merge(required_by_default: required?)

  super(**resolve_required_option(**options))
end

Private Instance Methods

error_properties() click to toggle source
# File lib/stannum/constraints/type.rb, line 95
def error_properties
  { required: required?, type: expected_type }
end
matches_type?(actual) click to toggle source
# File lib/stannum/constraints/type.rb, line 99
def matches_type?(actual)
  actual.is_a?(expected_type) || (optional? && actual.nil?)
end
resolve_expected_type(type_or_name) click to toggle source
# File lib/stannum/constraints/type.rb, line 103
def resolve_expected_type(type_or_name)
  return type_or_name if type_or_name.is_a?(Module)

  return Object.const_get(type_or_name) if type_or_name.is_a?(String)

  raise ArgumentError,
    'expected type must be a Class or Module',
    caller[1..-1]
end