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