module Puppet::Pops::Types::TypeAsserter
Public Class Methods
Asserts that a type_to_check is assignable to required_type and raises a {Puppet::ParseError} if that's not the case
@param subject [String,Array] String to be prepended to the exception message or Array where the first element is
a format string and the rest are arguments to that format string
@param expected_type [PAnyType] Expected type @param type_to_check [PAnyType] Type
to check against the required type @return The type_to_check argument
@api public
# File lib/puppet/pops/types/type_asserter.rb 15 def self.assert_assignable(subject, expected_type, type_to_check, &block) 16 report_type_mismatch(subject, expected_type, type_to_check, 'is incorrect', &block) unless expected_type.assignable?(type_to_check) 17 type_to_check 18 end
Asserts that a value is an instance of a given type and raises a {Puppet::ParseError} if that's not the case
@param subject [String,Array] String to be prepended to the exception message or Array where the first element is
a format string and the rest are arguments to that format string
@param expected_type [PAnyType] Expected type for the value @param value [Object] Value to check @param nil_ok [Boolean] Can be true to allow nil value. Optional and defaults to false @return The value argument
@api public
# File lib/puppet/pops/types/type_asserter.rb 31 def self.assert_instance_of(subject, expected_type, value, nil_ok = false, &block) 32 unless value.nil? && nil_ok 33 report_type_mismatch(subject, expected_type, TypeCalculator.singleton.infer_set(value), &block) unless expected_type.instance?(value) 34 end 35 value 36 end
Private Class Methods
# File lib/puppet/pops/types/type_asserter.rb 38 def self.report_type_mismatch(subject, expected_type, actual_type, what = 'has wrong type') 39 subject = yield(subject) if block_given? 40 subject = subject[0] % subject[1..-1] if subject.is_a?(Array) 41 raise TypeAssertionError.new( 42 TypeMismatchDescriber.singleton.describe_mismatch("#{subject} #{what},", expected_type, actual_type), expected_type, actual_type) 43 end