Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / functions / is_a.rb
1 # @summary
2 #   Boolean check to determine whether a variable is of a given data type.
3 #   This is equivalent to the `=~` type checks.
4 #
5 # @example Example Usage:
6 #   # check a data type
7 #     foo = 3
8 #     $bar = [1,2,3]
9 #     $baz = 'A string!'
10 #
11 #     if $foo.is_a(Integer) {
12 #       notify  { 'foo!': }
13 #     }
14 #     if $bar.is_a(Array) {
15 #       notify { 'bar!': }
16 #     }
17 #     if $baz.is_a(String) {
18 #       notify { 'baz!': }
19 #     }
20 #
21 # See the documentation for "The Puppet Type System" for more information about types.
22 # See the `assert_type()` function for flexible ways to assert the type of a value.
23 #
24 Puppet::Functions.create_function(:is_a) do
25   # @param value
26   #   The value to be checked
27   #
28   # @param type
29   #   The expected type
30   #
31   # @return [Boolean]
32   #   Return's `true` or `false`.
33   dispatch :is_a do
34     param 'Any', :value
35     param 'Type', :type
36   end
37
38   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.
39     # See puppet's lib/puppet/pops/evaluator/evaluator_impl.rb eval_MatchExpression
40     Puppet::Pops::Types::TypeCalculator.instance?(type, value)
41   end
42 end