is_a.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. # Boolean check to determine whether a variable is of a given data type. This is equivalent to the `=~` type checks.
  2. #
  3. # @example how to check a data type
  4. # # check a data type
  5. # foo = 3
  6. # $bar = [1,2,3]
  7. # $baz = 'A string!'
  8. #
  9. # if $foo.is_a(Integer) {
  10. # notify { 'foo!': }
  11. # }
  12. # if $bar.is_a(Array) {
  13. # notify { 'bar!': }
  14. # }
  15. # if $baz.is_a(String) {
  16. # notify { 'baz!': }
  17. # }
  18. #
  19. # See the documentation for "The Puppet Type System" for more information about types.
  20. # See the `assert_type()` function for flexible ways to assert the type of a value.
  21. #
  22. Puppet::Functions.create_function(:is_a) do
  23. dispatch :is_a do
  24. param 'Any', :value
  25. param 'Type', :type
  26. end
  27. def is_a(value, type) # rubocop:disable Style/PredicateName : Used in to many other places to rename at this time, attempting to refactor caused Rubocop to crash.
  28. # See puppet's lib/puppet/pops/evaluator/evaluator_impl.rb eval_MatchExpression
  29. Puppet::Pops::Types::TypeCalculator.instance?(type, value)
  30. end
  31. end