Module: Puppet::Pops::Types::TypeAsserter

Defined in:
lib/puppet/pops/types/type_asserter.rb

Class Method Summary collapse

Class Method Details

.assert_assignable(subject, expected_type, type_to_check) ⇒ Object

Asserts that a type_to_check is assignable to required_type and raises a Puppet::ParseError if that’s not the case

Parameters:

  • subject (String)

    String to be prepended to the exception message

  • expected_type (PAnyType)

    Expected type

  • type_to_check (PAnyType)

    Type to check against the required type

Returns:

  • The type_to_check argument



14
15
16
17
# File 'lib/puppet/pops/types/type_asserter.rb', line 14

def self.assert_assignable(subject, expected_type, type_to_check)
  report_type_mismatch(subject, expected_type, type_to_check) unless expected_type.assignable?(type_to_check)
  type_to_check
end

.assert_instance_of(subject, expected_type, value, nil_ok = false) ⇒ Object

Asserts that a value is an instance of a given type and raises a Puppet::ParseError if that’s not the case

Parameters:

  • subject (String)

    String to be prepended to the exception message

  • expected_type (PAnyType)

    Expected type for the value

  • value (Object)

    Value to check

  • nil_ok (Boolean) (defaults to: false)

    Can be true to allow nil value. Optional and defaults to false

Returns:

  • The value argument



29
30
31
32
33
34
# File 'lib/puppet/pops/types/type_asserter.rb', line 29

def self.assert_instance_of(subject, expected_type, value, nil_ok = false)
  unless value.nil? && nil_ok
    report_type_mismatch(subject, expected_type, TypeCalculator.singleton.infer_set(value).generalize) unless expected_type.instance?(value)
  end
  value
end