Class: Dry::Types::Safe

Inherits:
Object
  • Object
show all
Includes:
Builder, Decorator, Type
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, #|

Methods included from Decorator

#__new__, #constrained?, #default?, #initialize, #method_missing, #optional, #respond_to_missing?, #valid?

Methods included from Options

#initialize, #meta, #pristine, #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: []

Parameters:

  • input (Object)

Returns:

  • (Object)


14
15
16
17
18
19
20
21
22
# File 'lib/dry/types/safe.rb', line 14

def call(input)
  result = try(input)

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

#decorate?(response) ⇒ Boolean (private)

Parameters:

Returns:

  • (Boolean)


54
55
56
# File 'lib/dry/types/safe.rb', line 54

def decorate?(response)
  super || response.kind_of?(Constructor)
end

#safeSafe

Returns:



46
47
48
# File 'lib/dry/types/safe.rb', line 46

def safe
  self
end

#to_ast(meta: true) ⇒ Object

See Also:



40
41
42
# File 'lib/dry/types/safe.rb', line 40

def to_ast(meta: true)
  [:safe, [type.to_ast(meta: meta), EMPTY_HASH]]
end

#try(input, &block) {|failure| ... } ⇒ Result, Logic::Result

Parameters:

  • input (Object)
  • block (#call, nil)

Yield Parameters:

  • failure (Failure)

Yield Returns:

Returns:



30
31
32
33
34
35
# File 'lib/dry/types/safe.rb', line 30

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