Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / has_key.rb
1 #
2 # has_key.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:has_key, :type => :rvalue, :doc => <<-'DOC') do |args|
6     Determine if a hash has a certain key value.
7
8     Example:
9
10         $my_hash = {'key_one' => 'value_one'}
11         if has_key($my_hash, 'key_two') {
12           notice('we will not reach here')
13         }
14         if has_key($my_hash, 'key_one') {
15           notice('this will be printed')
16         }
17
18     Note: Since Puppet 4.0.0 this can be achieved in the Puppet language with the following equivalent expression:
19
20        $my_hash = {'key_one' => 'value_one'}
21        if 'key_one' in $my_hash {
22          notice('this will be printed')
23        }
24     DOC
25
26     unless args.length == 2
27       raise Puppet::ParseError, "has_key(): wrong number of arguments (#{args.length}; must be 2)"
28     end
29     unless args[0].is_a?(Hash)
30       raise Puppet::ParseError, "has_key(): expects the first argument to be a hash, got #{args[0].inspect} which is of type #{args[0].class}"
31     end
32     args[0].key?(args[1])
33   end
34 end