summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions
diff options
context:
space:
mode:
authorPaweł Tomulik <ptomulik@meil.pw.edu.pl>2013-07-29 11:30:47 +0200
committerPaweł Tomulik <ptomulik@meil.pw.edu.pl>2013-08-05 22:12:26 +0200
commit206941520467a5cbf1ba4131c68c4814b5ab181a (patch)
tree038b83732eea904567b9188d055277475efdfd45 /lib/puppet/parser/functions
parente8dcc691934f11172e2a3b5064a375f6b025865b (diff)
downloadpuppet-stdlib-206941520467a5cbf1ba4131c68c4814b5ab181a.tar.gz
puppet-stdlib-206941520467a5cbf1ba4131c68c4814b5ab181a.tar.bz2
added delete_values() and delete_undef_values() functions
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r--lib/puppet/parser/functions/delete_undef_values.rb34
-rw-r--r--lib/puppet/parser/functions/delete_values.rb27
2 files changed, 61 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
diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb
new file mode 100644
index 0000000..2fcbd16
--- /dev/null
+++ b/lib/puppet/parser/functions/delete_values.rb
@@ -0,0 +1,27 @@
+module Puppet::Parser::Functions
+ newfunction(:delete_values, :type => :rvalue, :doc => <<-EOS
+Deletes all instances of a given value from a hash.
+
+*Examples:*
+
+ delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B')
+
+Would return: {'a'=>'A','c'=>'C','B'=>'D'}
+
+ EOS
+ ) do |arguments|
+
+ raise(Puppet::ParseError,
+ "delete_values(): Wrong number of arguments given " +
+ "(#{arguments.size} of 2)") if arguments.size != 2
+
+ hash = arguments[0]
+ item = arguments[1]
+
+ if not hash.is_a?(Hash)
+ raise(TypeError, "delete_values(): First argument must be a Hash. " + \
+ "Given an" " argument of class #{hash.class}.")
+ end
+ hash.delete_if { |key, val| item == val }
+ end
+end