Sha256: fa221b4bb47611cee990dabe02d4bf89a4b8a60116832cf74e15ab2e1e34b9b0

Contents?: true

Size: 1.23 KB

Versions: 7

Compression:

Stored size: 1.23 KB

Contents

require 'dry/types/decorator'

module Dry
  module Types
    class Safe
      include Type
      include Dry::Equalizer(:type)
      include Decorator
      include Builder
      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 Definition#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

7 entries across 7 versions & 1 rubygems

Version Path
dry-types-0.14.1 lib/dry/types/safe.rb
dry-types-0.14.0 lib/dry/types/safe.rb
dry-types-0.13.4 lib/dry/types/safe.rb
dry-types-0.13.3 lib/dry/types/safe.rb
dry-types-0.13.2 lib/dry/types/safe.rb
dry-types-0.13.1 lib/dry/types/safe.rb
dry-types-0.13.0 lib/dry/types/safe.rb