Class: Dry::Types::Default
- Inherits:
-
Object
- Object
- Dry::Types::Default
- Defined in:
- lib/dry/types/default.rb
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
Instance Method Summary collapse
-
#call(input) ⇒ Object
(also: #[])
Value passed through Dry::Types::Decorator#type or Builder#default value.
- #constrained(*args) ⇒ Default
- #default? ⇒ true
-
#initialize(type, value) ⇒ Default
constructor
A new instance of Default.
- #try(input) ⇒ Success
Methods included from Builder
#constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|
Methods included from Decorator
#constrained?, #constructor, #decorate?, #method_missing, #respond_to_missing?, #valid?
Methods included from Options
Constructor Details
#initialize(type, value) ⇒ Default
Returns a new instance of Default.
37 38 39 40 |
# File 'lib/dry/types/default.rb', line 37 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
21 22 23 |
# File 'lib/dry/types/default.rb', line 21 def value @value end |
Class Method Details
.[](value) ⇒ Default, Dry::Types::Default::Callable
27 28 29 30 31 32 33 |
# File 'lib/dry/types/default.rb', line 27 def self.[](value) if value.respond_to?(:call) Callable else self end end |
Instance Method Details
#call(input) ⇒ Object Also known as: []
Returns value passed through Dry::Types::Decorator#type or Builder#default value.
61 62 63 64 65 66 67 68 |
# File 'lib/dry/types/default.rb', line 61 def call(input) if input.nil? evaluate else output = type[input] output.nil? ? evaluate : output end end |
#constrained(*args) ⇒ Default
44 45 46 |
# File 'lib/dry/types/default.rb', line 44 def constrained(*args) type.constrained(*args).default(value) end |
#default? ⇒ true
49 50 51 |
# File 'lib/dry/types/default.rb', line 49 def default? true end |
#try(input) ⇒ Success
55 56 57 |
# File 'lib/dry/types/default.rb', line 55 def try(input) success(call(input)) end |