Sha256: db5a7621d68f799a8f5fdbe214e06ca445eb82e64657cddcd44bcb9c5241b27c

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require 'dry/core/deprecations'
require 'dry/types/decorator'

module Dry
  module Types
    # Lax types rescue from type-related errors when constructors fail
    #
    # @api public
    class Lax
      include Type
      include Decorator
      include Builder
      include Printable
      include Dry::Equalizer(:type, inspect: false, immutable: true)

      undef :options, :constructor, :<<, :>>, :prepend, :append

      # @param [Object] input
      #
      # @return [Object]
      #
      # @api public
      def call(input)
        type.call_safe(input) { |output = input| output }
      end
      alias_method :[], :call
      alias_method :call_safe, :call
      alias_method :call_unsafe, :call

      # @param [Object] input
      # @param [#call,nil] block
      #
      # @yieldparam [Failure] failure
      # @yieldreturn [Result]
      #
      # @return [Result,Logic::Result]
      #
      # @api public
      def try(input, &block)
        type.try(input, &block)
      end

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

      # @return [Lax]
      #
      # @api public
      def lax
        self
      end

      private

      # @param [Object, Dry::Types::Constructor] response
      #
      # @return [Boolean]
      #
      # @api private
      def decorate?(response)
        super || response.is_a?(type.constructor_type)
      end
    end

    extend ::Dry::Core::Deprecations[:'dry-types']
    Safe = Lax
    deprecate_constant(:Safe)
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/dry-types-1.4.0/lib/dry/types/lax.rb
dry-types-1.4.0 lib/dry/types/lax.rb
dry-types-1.3.1 lib/dry/types/lax.rb
dry-types-1.3.0 lib/dry/types/lax.rb