Sha256: 783d7d13d2a44cae28a25aec64c45d5bdc620bfeebec7483bf60d7d4178ed565

Contents?: true

Size: 1.3 KB

Versions: 18

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Dry
  module Types
    class Constrained
      # Common coercion-related API for constrained types
      #
      # @api public
      class Coercible < Constrained
        # @return [Object]
        #
        # @api private
        def call_unsafe(input)
          coerced = type.call_unsafe(input)
          result = rule.(coerced)

          if result.success?
            coerced
          else
            raise ConstraintError.new(result, input)
          end
        end

        # @return [Object]
        #
        # @api private
        def call_safe(input)
          coerced = type.call_safe(input) { return yield }

          if rule[coerced]
            coerced
          else
            yield(coerced)
          end
        end

        # @see Dry::Types::Constrained#try
        #
        # @api public
        def try(input, &block)
          result = type.try(input)

          if result.success?
            validation = rule.(result.input)

            if validation.success?
              result
            else
              failure = failure(result.input, ConstraintError.new(validation, input))
              block ? yield(failure) : failure
            end
          else
            block ? yield(result) : result
          end
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
dry-types-1.7.2 lib/dry/types/constrained/coercible.rb
dry-types-1.7.1 lib/dry/types/constrained/coercible.rb
dry-types-1.7.0 lib/dry/types/constrained/coercible.rb
dry-types-1.6.1 lib/dry/types/constrained/coercible.rb
dry-types-1.6.0 lib/dry/types/constrained/coercible.rb
dry-types-1.5.1 lib/dry/types/constrained/coercible.rb
dry-types-1.5.0 lib/dry/types/constrained/coercible.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/dry-types-1.4.0/lib/dry/types/constrained/coercible.rb
dry-types-1.4.0 lib/dry/types/constrained/coercible.rb
dry-types-1.3.1 lib/dry/types/constrained/coercible.rb
dry-types-1.3.0 lib/dry/types/constrained/coercible.rb
dry-types-1.2.2 lib/dry/types/constrained/coercible.rb
dry-types-1.2.1 lib/dry/types/constrained/coercible.rb
dry-types-1.2.0 lib/dry/types/constrained/coercible.rb
dry-types-1.1.1 lib/dry/types/constrained/coercible.rb
dry-types-1.1.0 lib/dry/types/constrained/coercible.rb
dry-types-1.0.1 lib/dry/types/constrained/coercible.rb
dry-types-1.0.0 lib/dry/types/constrained/coercible.rb