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, &block) ⇒ 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) ⇒ 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, &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.
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.
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.
121 122 123 |
# File 'lib/dry/types/default.rb', line 121 def callable? false end |
#constrained ⇒ Default
Build a constrained type
65 66 67 |
# File 'lib/dry/types/default.rb', line 65 def constrained(...) type.constrained(...).default(value) end |
#default? ⇒ true
72 73 74 |
# File 'lib/dry/types/default.rb', line 72 def default? true end |
#try(input) ⇒ Result::Success
81 82 83 |
# File 'lib/dry/types/default.rb', line 81 def try(input) success(call(input)) end |
#valid?(value = Undefined) ⇒ Boolean
88 89 90 |
# File 'lib/dry/types/default.rb', line 88 def valid?(value = Undefined) Undefined.equal?(value) || super end |