Class: Dry::Types::Default

Inherits:
Object
  • Object
show all
Includes:
Builder, Decorator, Printable, Type
Defined in:
lib/dry/types/default.rb

Overview

Default types are useful when a missing value should be replaced by a default one

Direct Known Subclasses

Callable

Defined Under Namespace

Classes: Callable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value) ⇒ Default

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.

Returns a new instance of Default.

Parameters:

  • type (Type)
  • value (Object)


53
54
55
56
# File 'lib/dry/types/default.rb', line 53

def initialize(type, value, **)
  super
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator

Instance Attribute Details

#optionsHash (readonly) Originally defined in module Options

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.

Returns:

#typeType (readonly) Originally defined in module Decorator

Returns:

#valueObject (readonly) Also known as: evaluate

Returns:

  • (Object)


32
33
34
# File 'lib/dry/types/default.rb', line 32

def value
  @value
end

Class Method Details

.[](value) ⇒ Class

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.

Returns Dry::Types::Default or Callable.

Parameters:

  • value (Object, #call)

Returns:



41
42
43
44
45
46
47
# File 'lib/dry/types/default.rb', line 41

def self.[](value)
  if value.respond_to?(:call)
    Callable
  else
    self
  end
end

Instance Method Details

#&(other) ⇒ Intersection, Intersection::Constrained Originally defined in module Builder

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.

Compose two types into an Intersection type

Parameters:

Returns:

#>(other) ⇒ Implication, Implication::Constrained Originally defined in module Builder

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.

Compose two types into an Implication type

Parameters:

Returns:

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

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)

#call_safe(input = Undefined, &block) ⇒ Object

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.

Returns value passed through Dry::Types::Decorator#type or Builder#default value.

Parameters:

  • input (Object) (defaults to: Undefined)

Returns:



110
111
112
113
114
115
116
# File 'lib/dry/types/default.rb', line 110

def call_safe(input = Undefined, &block)
  if input.equal?(Undefined)
    evaluate
  else
    Undefined.default(type.call_safe(input, &block)) { evaluate }
  end
end

#call_unsafe(input = Undefined) ⇒ Object

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.

Returns value passed through Dry::Types::Decorator#type or Builder#default value.

Parameters:

  • input (Object) (defaults to: Undefined)

Returns:



97
98
99
100
101
102
103
# File 'lib/dry/types/default.rb', line 97

def call_unsafe(input = Undefined)
  if input.equal?(Undefined)
    evaluate
  else
    Undefined.default(type.call_unsafe(input)) { evaluate }
  end
end

#callable?false

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.

Returns:

  • (false)


121
122
123
# File 'lib/dry/types/default.rb', line 121

def callable?
  false
end

#constrainedDefault

Build a constrained type

Parameters:

Returns:



65
66
67
# File 'lib/dry/types/default.rb', line 65

def constrained(...)
  type.constrained(...).default(value)
end

#constrained?Boolean Originally defined in module Decorator

Returns:

  • (Boolean)

#constrained_typeClass Originally defined in module Builder

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.

Returns:

  • (Class)

#constructor(constructor = nil, **options, &block) ⇒ Constructor Also known as: append, prepend, >>, << Originally defined in module Builder

Define a constructor for the type

Parameters:

  • constructor (#call, nil) (defaults to: nil)
  • options (Hash)
  • block (#call, nil)

Returns:

#constructor_typeClass Originally defined in module Builder

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.

Returns:

  • (Class)

#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default Originally defined in module Builder

Turn a type into a type with a default value

Parameters:

  • input (Object) (defaults to: Undefined)
  • block (#call, nil)
  • [Boolean] (Hash)

    a customizable set of options

Returns:

Raises:

#default?true

Returns:

  • (true)


72
73
74
# File 'lib/dry/types/default.rb', line 72

def default?
  true
end

#enum(*values) ⇒ Enum Originally defined in module Builder

Define an enum on top of the existing type

Parameters:

Returns:

#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor Originally defined in module Builder

Use the given value on type mismatch

Parameters:

  • value (Object) (defaults to: Undefined)
  • fallback (#call, nil)
  • [Boolean] (Hash)

    a customizable set of options

Returns:

#laxLax Originally defined in module Builder

Turn a type into a lax type that will rescue from type-errors and return the original input

Returns:

#maybeMaybe Originally defined in module Builder

Turn a type into a maybe type

Returns:

#optionalSum Originally defined in module Builder

Turn a type into an optional type

Returns:

#respond_to_missing?(meth, include_private = false) ⇒ Boolean Originally defined in module Decorator

Parameters:

  • meth (Symbol)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)

#to_procProc Originally defined in module Decorator

Wrap the type with a proc

Returns:

  • (Proc)

#to_sString Also known as: inspect Originally defined in module Printable

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.

Returns:

  • (String)

#try(input) ⇒ Result::Success

Parameters:

  • input (Object)

Returns:



81
82
83
# File 'lib/dry/types/default.rb', line 81

def try(input)
  success(call(input))
end

#valid?(value = Undefined) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/dry/types/default.rb', line 88

def valid?(value = Undefined)
  Undefined.equal?(value) || super
end

#with(**new_options) ⇒ Type Originally defined in module Options

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.

Parameters:

  • new_options (Hash)

Returns:

#|(other) ⇒ Sum, Sum::Constrained Originally defined in module Builder

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.

Compose two types into a Sum type

Parameters:

Returns: