Sha256: 2b519a90d1c5617a693853ed1ee6b8cb5241198c0f3785b9d3f4b40f554cfde3

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'dry/types/decorator'

module Dry
  module Types
    class Safe
      include Type
      include Decorator
      include Builder
      include Printable
      include Dry::Equalizer(:type, inspect: false)

      private :options, :meta

      # @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

      # @api public
      #
      # @see Nominal#to_ast
      def to_ast(meta: true)
        [:safe, [type.to_ast(meta: meta), EMPTY_HASH]]
      end

      # @api public
      # @return [Safe]
      def safe
        self
      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

1 entries across 1 versions & 1 rubygems

Version Path
dry-types-0.15.0 lib/dry/types/safe.rb