Sha256: 9796df2d00a9729cd8c0bc949e68063a2c30ed19e95decb39ccd892555b3463a
Contents?: true
Size: 1.42 KB
Versions: 1
Compression:
Stored size: 1.42 KB
Contents
# frozen_string_literal: true module RASN1 module Types # Mixin to had constraints on a RASN1 type. # Should not be used directly but through {Model.define_type}. # @version 0.11.0 # @author Sylvain Daubert module Constrained module ClassMethods # Setter for constraint # @param [Proc,nil] constraint # @return [Proc,nil] def constraint=(constraint) @constraint = constraint end # Check if a constraint is really defined # @return [Boolean] def constrained? @constraint.is_a?(Proc) end # Check constraint, if defined # @param [Object] value the value of the type to check # @raise [ConstraintError] constraint is not verified def check_constraint(value) return unless constrained? raise ConstraintError.new(self) unless @constraint.call(value) end end class << self attr_reader :constraint def included(base) base.extend ClassMethods end end # Redefined +#value=+ to check constraint before assigning +val+ # @see Types::Base#value= # @raise [ConstraintError] constraint is not verified def value=(val) self.class.check_constraint(val) super end def der_to_value(der, ber: false) super self.class.check_constraint(@value) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rasn1-0.11.0 | lib/rasn1/types/constrained.rb |