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
- #options ⇒ Hash included from Options readonly private
- #type ⇒ Type included from Decorator readonly
- #value ⇒ Object (also: #evaluate) readonly
Class Method Summary collapse
- .[](value) ⇒ Class private
Instance Method Summary collapse
-
#&(other) ⇒ Intersection, Intersection::Constrained
included
from Builder
private
Compose two types into an Intersection type.
-
#>(other) ⇒ Implication, Implication::Constrained
included
from Builder
private
Compose two types into an Implication type.
-
#call(input = Undefined, &block) ⇒ Object
(also: #[])
included
from Type
Apply type to a value.
-
#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.
- #constrained? ⇒ Boolean included from Decorator
- #constrained_type ⇒ Class included from Builder private
-
#constructor(constructor = nil, **options, &block) ⇒ Constructor
(also: #append, #prepend, #>>, #<<)
included
from Builder
Define a constructor for the type.
- #constructor_type ⇒ Class included from Builder private
-
#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default
included
from Builder
Turn a type into a type with a default value.
- #default? ⇒ true
-
#enum(*values) ⇒ Enum
included
from Builder
Define an enum on top of the existing type.
-
#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor
included
from Builder
Use the given value on type mismatch.
-
#initialize(type, value) ⇒ Default
constructor
private
A new instance of Default.
-
#lax ⇒ Lax
included
from Builder
Turn a type into a lax type that will rescue from type-errors and return the original input.
-
#maybe ⇒ Maybe
included
from Builder
Turn a type into a maybe type.
-
#optional ⇒ Sum
included
from Builder
Turn a type into an optional type.
- #respond_to_missing?(meth, include_private = false) ⇒ Boolean included from Decorator
-
#to_proc ⇒ Proc
included
from Decorator
Wrap the type with a proc.
- #to_s ⇒ String (also: #inspect) included from Printable private
- #try(input) ⇒ Result::Success
- #valid?(value = Undefined) ⇒ Boolean
- #with(**new_options) ⇒ Type included from Options private
-
#|(other) ⇒ Sum, Sum::Constrained
included
from Builder
private
Compose two types into a Sum 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
#options ⇒ Hash (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.
#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
#&(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
#>(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
#call(input = Undefined) ⇒ Object #call(input = Undefined) {|output| ... } ⇒ Object Also known as: [] Originally defined in module Type
Apply type to a value
#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 |
#constrained? ⇒ Boolean Originally defined in module Decorator
#constrained_type ⇒ Class 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.
#constructor(constructor = nil, **options, &block) ⇒ Constructor Also known as: append, prepend, >>, << Originally defined in module Builder
Define a constructor for the type
#constructor_type ⇒ Class 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.
#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default Originally defined in module Builder
Turn a type into a type with a default value
#default? ⇒ 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
#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor Originally defined in module Builder
Use the given value on type mismatch
#lax ⇒ Lax Originally defined in module Builder
Turn a type into a lax type that will rescue from type-errors and return the original input
#respond_to_missing?(meth, include_private = false) ⇒ Boolean Originally defined in module Decorator
#to_proc ⇒ Proc Originally defined in module Decorator
Wrap the type with a proc
#to_s ⇒ String 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.
#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 |
#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.
#|(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