2011-04-30 04:16:12 +02:00
|
|
|
#
|
|
|
|
# delete.rb
|
|
|
|
#
|
|
|
|
|
|
|
|
# TODO(Krzysztof Wilczynski): We need to add support for regular expression ...
|
|
|
|
|
|
|
|
module Puppet::Parser::Functions
|
|
|
|
newfunction(:delete, :type => :rvalue, :doc => <<-EOS
|
2012-11-18 07:27:15 +01:00
|
|
|
Deletes all instances of a given element from an array, substring from a
|
|
|
|
string, or key from a hash.
|
2011-07-30 00:09:30 +02:00
|
|
|
|
|
|
|
*Examples:*
|
|
|
|
|
2012-11-18 07:27:15 +01:00
|
|
|
delete(['a','b','c','b'], 'b')
|
|
|
|
Would return: ['a','c']
|
2011-07-30 00:09:30 +02:00
|
|
|
|
2012-11-18 07:27:15 +01:00
|
|
|
delete({'a'=>1,'b'=>2,'c'=>3}, 'b')
|
|
|
|
Would return: {'a'=>1,'c'=>3}
|
|
|
|
|
|
|
|
delete('abracadabra', 'bra')
|
|
|
|
Would return: 'acada'
|
2011-04-30 04:16:12 +02:00
|
|
|
EOS
|
|
|
|
) do |arguments|
|
2011-06-29 22:21:55 +02:00
|
|
|
|
|
|
|
if (arguments.size != 2) then
|
2011-07-30 00:09:30 +02:00
|
|
|
raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
|
2012-11-18 07:27:15 +01:00
|
|
|
"given #{arguments.size} for 2.")
|
2011-06-29 22:21:55 +02:00
|
|
|
end
|
|
|
|
|
2012-11-18 07:27:15 +01:00
|
|
|
collection = arguments[0]
|
2011-06-30 00:37:37 +02:00
|
|
|
item = arguments[1]
|
|
|
|
|
2012-11-18 07:27:15 +01:00
|
|
|
case collection
|
|
|
|
when Array, Hash
|
|
|
|
collection.delete item
|
|
|
|
when String
|
|
|
|
collection.gsub! item, ''
|
|
|
|
else
|
|
|
|
raise(TypeError, "delete(): First argument must be an Array, " +
|
|
|
|
"String, or Hash. Given an argument of class #{collection.class}.")
|
|
|
|
end
|
|
|
|
collection
|
2011-04-30 04:16:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 et :
|