Module: Dry::Types::Type

Included in:
Composition, Constrained, Default, Enum, Lax, Maybe, Nominal, Schema::Key
Defined in:
lib/dry/types/type.rb

Overview

Common Type module denoting an object is a Type

Instance Method Summary collapse

Instance Method Details

#call(input = Undefined) ⇒ Object #call(input = Undefined) {|output| ... } ⇒ Object Also known as: []

Apply type to a value

Overloads:

  • #call(input = Undefined) ⇒ Object

    Possibly unsafe coercion attempt. If a value doesn’t match the type, an exception will be raised.

    Parameters:

    • input (Object) (defaults to: Undefined)

    Returns:

    • (Object)
  • #call(input = Undefined) {|output| ... } ⇒ Object

    When a block is passed, #call will never throw an exception on failed coercion, instead it will call the block.

    Parameters:

    • input (Object) (defaults to: Undefined)

    Yield Parameters:

    • output (Object)

      Partially coerced value

    Returns:

    • (Object)


43
44
45
46
47
48
49
# File 'lib/dry/types/type.rb', line 43

def call(input = Undefined, &block)
  if block_given?
    call_safe(input, &block)
  else
    call_unsafe(input)
  end
end

#valid?(input = Undefined) ⇒ Boolean Also known as: ===

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether a value is a valid member of the type

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/dry/types/type.rb', line 18

def valid?(input = Undefined)
  call_safe(input) { return false }
  true
end