Class: Dry::Types::Safe
- Inherits:
-
Object
- Object
- Dry::Types::Safe
- Defined in:
- lib/dry/types/safe.rb
Instance Attribute Summary
Attributes included from Decorator
Attributes included from Options
Instance Method Summary collapse
- #call(input) ⇒ Object (also: #[])
- #decorate?(response) ⇒ Boolean private
- #safe ⇒ Safe
- #to_ast(meta: true) ⇒ Object
- #try(input, &block) {|failure| ... } ⇒ Result, Logic::Result
Methods included from Builder
#constrained, #constrained_type, #constructor, #default, #enum, #maybe, #optional, #|
Methods included from Decorator
#constrained?, #constructor, #default?, #initialize, #method_missing, #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: []
13 14 15 16 17 18 19 20 21 |
# File 'lib/dry/types/safe.rb', line 13 def call(input) result = try(input) if result.respond_to?(:input) result.input else input end end |
#decorate?(response) ⇒ Boolean (private)
53 54 55 |
# File 'lib/dry/types/safe.rb', line 53 def decorate?(response) super || response.kind_of?(Constructor) end |
#to_ast(meta: true) ⇒ Object
39 40 41 |
# File 'lib/dry/types/safe.rb', line 39 def to_ast(meta: true) [:safe, [type.to_ast, ? self. : EMPTY_HASH]] end |
#try(input, &block) {|failure| ... } ⇒ Result, Logic::Result
29 30 31 32 33 34 |
# File 'lib/dry/types/safe.rb', line 29 def try(input, &block) type.try(input, &block) rescue TypeError, ArgumentError => e result = failure(input, e.) block ? yield(result) : result end |