Sha256: ea79c25955022717a30fe46065225b07abc610761ec0f08bb6de7bbbb9ff531e

Contents?: true

Size: 1005 Bytes

Versions: 4

Compression:

Stored size: 1005 Bytes

Contents

require 'dry/types/decorator'

module Dry
  module Types
    class Safe
      include Type
      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,nil] block
      # @yieldparam [Failure] failure
      # @yieldreturn [Result]
      # @return [Result,Logic::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

4 entries across 4 versions & 1 rubygems

Version Path
dry-types-0.10.3 lib/dry/types/safe.rb
dry-types-0.10.2 lib/dry/types/safe.rb
dry-types-0.10.1 lib/dry/types/safe.rb
dry-types-0.10.0 lib/dry/types/safe.rb