Sha256: 0b13f4cfe06509c79a9bd5cd10a4da2926c74105044d3fe6de020ce7f8016f94
Contents?: true
Size: 1.1 KB
Versions: 3
Compression:
Stored size: 1.1 KB
Contents
require 'dry/types/decorator' require 'dry/types/constraints' require 'dry/types/constrained/coercible' module Dry module Types class Constrained include Dry::Equalizer(:type, :options, :rule) include Decorator include Builder attr_reader :rule def initialize(type, options) super @rule = options.fetch(:rule) end def call(input) try(input) do |result| raise ConstraintError.new(result, input) end.input end alias_method :[], :call def try(input, &block) result = rule.(input) if result.success? type.try(input, &block) else failure = failure(input, result) block ? yield(failure) : failure end end def valid?(value) rule.(value).success? && type.valid?(value) end def constrained(options) with(rule: rule & Types.Rule(options)) end def constrained? true end private def decorate?(response) super || response.kind_of?(Constructor) end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
dry-types-0.9.2 | lib/dry/types/constrained.rb |
dry-types-0.9.1 | lib/dry/types/constrained.rb |
dry-types-0.9.0 | lib/dry/types/constrained.rb |