Sha256: da61602adefbe568902c84bda38a99929d2e915e90142dbcf695e877e7fde68b

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

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

      # @api public
      #
      # @see Definition#to_ast
      def to_ast(meta: true)
        [:safe, [type.to_ast, meta ? self.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

6 entries across 6 versions & 1 rubygems

Version Path
dry-types-0.12.3 lib/dry/types/safe.rb
dry-types-0.12.2 lib/dry/types/safe.rb
dry-types-0.12.1 lib/dry/types/safe.rb
dry-types-0.12.0 lib/dry/types/safe.rb
dry-types-0.11.1 lib/dry/types/safe.rb
dry-types-0.11.0 lib/dry/types/safe.rb