Sha256: 12221606e65291d6ceb2972c663fa8185aaf836123d0a0638cfe000c06d7da1c

Contents?: true

Size: 968 Bytes

Versions: 2

Compression:

Stored size: 968 Bytes

Contents

require 'dry/types/decorator'

module Dry
  module Types
    class Safe
      include Dry::Equalizer(:type, :options)
      include Decorator
      include Builder

      # @param [Object] input
      # @return [Object]
      def call(input)
        result = try(input)

        if result.respond_to?(:input)
          result.input
        else
          input
        end
      end
      alias_method :[], :call

      # @param [Object] input
      # @param [#call] block
      # @yieldparam [Failure] failure
      # @yieldreturn [Result]
      # @return [Result]
      def try(input, &block)
        type.try(input, &block)
      rescue TypeError, ArgumentError => e
        result = failure(input, e.message)
        block ? yield(result) : result
      end

      private

      # @param [Object, Dry::Types::Constructor] response
      # @return [Boolean]
      def decorate?(response)
        super || response.kind_of?(Constructor)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-types-0.9.4 lib/dry/types/safe.rb
dry-types-0.9.3 lib/dry/types/safe.rb