Class: Dry::Types::Safe

Inherits:
Object
  • Object
show all
Includes:
Builder, Decorator
Defined in:
lib/dry/types/safe.rb

Instance Attribute Summary

Attributes included from Decorator

#type

Attributes included from Options

#options

Instance Method Summary collapse

Methods included from Builder

#constrained, #constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|

Methods included from Decorator

#constrained?, #constructor, #default?, #initialize, #respond_to_missing?, #valid?

Methods included from Options

#initialize, #meta, #with

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator

Instance Method Details

#call(input) ⇒ Object Also known as: []



10
11
12
13
14
15
16
17
18
# File 'lib/dry/types/safe.rb', line 10

def call(input)
  result = try(input)

  if result.respond_to?(:input)
    result.input
  else
    input
  end
end

#try(input, &block) ⇒ Object



21
22
23
24
25
26
# File 'lib/dry/types/safe.rb', line 21

def try(input, &block)
  type.try(input, &block)
rescue TypeError, ArgumentError => e
  result = failure(input, e.message)
  block ? yield(result) : result
end