Class: Typed::Builder::CoerceType
- Inherits:
-
Object
- Object
- Typed::Builder::CoerceType
- Includes:
- BaseType
- Defined in:
- lib/typed/builder.rb
Instance Method Summary collapse
-
#initialize(input_type, return_type, swallow, &coercion) ⇒ CoerceType
constructor
A new instance of CoerceType.
- #process(value) ⇒ Object
Methods included from BaseType
#call, #constrained, #constructor, #default, #enum, #instance, #missable, #nullable, #|
Constructor Details
#initialize(input_type, return_type, swallow, &coercion) ⇒ CoerceType
Returns a new instance of CoerceType.
206 207 208 209 210 211 |
# File 'lib/typed/builder.rb', line 206 def initialize(input_type, return_type, swallow, &coercion) @input_type = input_type @return_type = return_type @coercion = coercion @swallow = swallow end |
Instance Method Details
#process(value) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/typed/builder.rb', line 213 def process(value) # No coercion needed passthrough_result = return_type.process(value) return passthrough_result if passthrough_result.ok # Check input_type enables this coercion input_result = input_type.process(value) if input_result.ok coerced_value = begin coercion.call(input_result.value) rescue *swallow input_result.value end return return_type.process(coerced_value) end passthrough_result end |