Mercurial > repos > other > Puppet
comparison modules/stdlib/lib/puppet/parser/functions/delete_undef_values.rb @ 37:addb0ea390a1 puppet-3.6
Update Puppet "stdlib" module
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 14 Mar 2015 20:09:45 +0000 |
parents | |
children | c42fb28cff86 |
comparison
equal
deleted
inserted
replaced
36:37675581a273 | 37:addb0ea390a1 |
---|---|
1 module Puppet::Parser::Functions | |
2 newfunction(:delete_undef_values, :type => :rvalue, :doc => <<-EOS | |
3 Returns a copy of input hash or array with all undefs deleted. | |
4 | |
5 *Examples:* | |
6 | |
7 $hash = delete_undef_values({a=>'A', b=>'', c=>undef, d => false}) | |
8 | |
9 Would return: {a => 'A', b => '', d => false} | |
10 | |
11 $array = delete_undef_values(['A','',undef,false]) | |
12 | |
13 Would return: ['A','',false] | |
14 | |
15 EOS | |
16 ) do |args| | |
17 | |
18 raise(Puppet::ParseError, | |
19 "delete_undef_values(): Wrong number of arguments given " + | |
20 "(#{args.size})") if args.size < 1 | |
21 | |
22 unless args[0].is_a? Array or args[0].is_a? Hash | |
23 raise(Puppet::ParseError, | |
24 "delete_undef_values(): expected an array or hash, got #{args[0]} type #{args[0].class} ") | |
25 end | |
26 result = args[0].dup | |
27 if result.is_a?(Hash) | |
28 result.delete_if {|key, val| val.equal? :undef} | |
29 elsif result.is_a?(Array) | |
30 result.delete :undef | |
31 end | |
32 result | |
33 end | |
34 end |