summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAdrien Thebo <git@somethingsinistral.net>2013-08-05 15:29:08 -0700
committerAdrien Thebo <git@somethingsinistral.net>2013-08-05 15:29:08 -0700
commit5544be9276f446bfc5014861ed288b1c102b2e42 (patch)
tree038b83732eea904567b9188d055277475efdfd45 /lib
parente8dcc691934f11172e2a3b5064a375f6b025865b (diff)
parent206941520467a5cbf1ba4131c68c4814b5ab181a (diff)
downloadpuppet-stdlib-5544be9276f446bfc5014861ed288b1c102b2e42.tar.gz
puppet-stdlib-5544be9276f446bfc5014861ed288b1c102b2e42.tar.bz2
Merge pull request #166 from ptomulik/delete_undef_values
Add delete_values() and delete_undef_values() functions
Diffstat (limited to 'lib')
-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