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, &block) ⇒ 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, 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

  • expected_type (PAnyType)

    Expected type

  • type_to_check (PAnyType)

    Type to check against the required type

Returns:

  • The type_to_check argument



17
18
19
20
# File 'lib/puppet/pops/types/type_asserter.rb', line 17

def self.assert_assignable(subject, expected_type, type_to_check, &block)
  report_type_mismatch(subject, expected_type, type_to_check, 'is incorrect', &block) unless expected_type.assignable?(type_to_check)
  type_to_check
end

.assert_instance_of(subject, expected_type, value, nil_ok = false, &block) ⇒ 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, 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

  • 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



33
34
35
36
37
38
# File 'lib/puppet/pops/types/type_asserter.rb', line 33

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