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

Attributes included from Decorator

#type

Attributes included from Options

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Printable

#to_s

Methods included from Builder

#&, #>, #constrained_type, #constructor, #constructor_type, #default, #enum, #fallback, #lax, #maybe, #optional, #|

Methods included from Decorator

#constrained?, #respond_to_missing?, #to_proc

Methods included from Options

#with

Methods included from Type

#call

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

#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

#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

#default?true

Returns:

  • (true)


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

def default?
  true
end

#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