summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions/delete_undef_values.rb
diff options
context:
space:
mode:
authorvarac <varacanero@zeromail.org>2013-08-27 18:03:56 +0200
committervarac <varacanero@zeromail.org>2013-08-27 18:03:56 +0200
commit473896f2ca22f5003ea47542ad8026819d2d8613 (patch)
tree297936a37a0cf217a76a5cee368f7f91430d9f02 /lib/puppet/parser/functions/delete_undef_values.rb
parent966f3b349a60b3997e58af1095bbd96671952fac (diff)
parent2a78cbfaad2acc61d6d8bab715db00c9890adbd7 (diff)
Merge branch 'master' of git://github.com/puppetlabs/puppetlabs-stdlib into leap_master
Diffstat (limited to 'lib/puppet/parser/functions/delete_undef_values.rb')
-rw-r--r--lib/puppet/parser/functions/delete_undef_values.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb
new file mode 100644
index 0000000..4eecab2
--- /dev/null
+++ b/lib/puppet/parser/functions/delete_undef_values.rb
@@ -0,0 +1,34 @@
+module Puppet::Parser::Functions
+ newfunction(:delete_undef_values, :type => :rvalue, :doc => <<-EOS
+Returns a copy of input hash or array with all undefs deleted.
+
+*Examples:*
+
+ $hash = delete_undef_values({a=>'A', b=>'', c=>undef, d => false})
+
+Would return: {a => 'A', b => '', d => false}
+
+ $array = delete_undef_values(['A','',undef,false])
+
+Would return: ['A','',false]
+
+ EOS
+ ) do |args|
+
+ raise(Puppet::ParseError,
+ "delete_undef_values(): Wrong number of arguments given " +
+ "(#{args.size})") if args.size < 1
+
+ result = args[0]
+ if result.is_a?(Hash)
+ result.delete_if {|key, val| val.equal? :undef}
+ elsif result.is_a?(Array)
+ result.delete :undef
+ else
+ raise(Puppet::ParseError,
+ "delete_undef_values(): Wrong argument type #{args[0].class} " +
+ "for first argument")
+ end
+ result
+ end
+end