Class: Dry::Types::Default
- Inherits:
-
Object
- Object
- Dry::Types::Default
- 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
Defined Under Namespace
Classes: Callable
Instance Attribute Summary collapse
- #value ⇒ Object (also: #evaluate) readonly
Attributes included from Decorator
Attributes included from Options
Class Method Summary collapse
- .[](value) ⇒ Class private
Instance Method Summary collapse
-
#call_safe(input = Undefined) ⇒ Object
private
Value passed through Dry::Types::Decorator#type or Builder#default value.
-
#call_unsafe(input = Undefined) ⇒ Object
private
Value passed through Dry::Types::Decorator#type or Builder#default value.
- #callable? ⇒ false private
-
#constrained ⇒ Default
Build a constrained type.
- #default? ⇒ true
-
#initialize(type, value) ⇒ Default
constructor
private
A new instance of Default.
- #try(input = Undefined) ⇒ Result::Success
- #valid?(value = Undefined) ⇒ Boolean
Methods included from Printable
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
Methods included from Type
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.
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
#value ⇒ Object (readonly) Also known as: evaluate
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.
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) ⇒ 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.
108 109 110 111 112 113 114 |
# File 'lib/dry/types/default.rb', line 108 def call_safe(input = Undefined, &) if input.equal?(Undefined) evaluate else Undefined.default(type.call_safe(input, &)) { 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.
95 96 97 98 99 100 101 |
# File 'lib/dry/types/default.rb', line 95 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.
119 |
# File 'lib/dry/types/default.rb', line 119 def callable? = false |
#constrained ⇒ Default
Build a constrained type
65 |
# File 'lib/dry/types/default.rb', line 65 def constrained(...) = type.constrained(...).default(value) |
#default? ⇒ true
70 |
# File 'lib/dry/types/default.rb', line 70 def default? = true |
#try(input = Undefined) ⇒ Result::Success
77 78 79 80 81 82 83 |
# File 'lib/dry/types/default.rb', line 77 def try(input = Undefined, &) if input.equal?(Undefined) success(evaluate) else type.try(input) end end |
#valid?(value = Undefined) ⇒ Boolean
88 |
# File 'lib/dry/types/default.rb', line 88 def valid?(value = Undefined) = Undefined.equal?(value) || super |