c14cbf31e2
The setup: list with 3 elements, delete one: $test_list = [‘a’, ‘b’, ‘c’] $test_deleted = delete($test_list, ‘a’) Print out the elements in ‘test_deleted’: notify { ‘group_output2’: withpath => true, name => “$cfeng::test_deleted”, } Notice: /Stage[main]/Syslog/Notify[group_output2]/message: bc Good! Run-on output shows that ‘a’ was deleted Print out the elements in ‘test_list’: notify { ‘group_output1’: withpath => true, name => “$cfeng::test_list”, } Notice: /Stage[main]/Syslog/Notify[group_output1]/message: bc WHAT!? 'a' was deleted from ‘test_list’ as well! Expected abc as output! This behaviour is confirmed for string, hash and array. This is fixed on this commit, I had added two spec tests to cover that cases. bug #20681 spec test for delete() function. I had forgot in the last commit the spec test for hash in the delete function. bug # 20681 delete() function change aproach. Instead of rejecting elements from the original list, we use collection = arguments[0].dup . then latter we could continue to use delete and gsub! on collection without impact on original argument. this is a better solution than the previous one, and works on ruby 1.8.7, 1.9.3 and 2.0.0. The previous solution does not work on ruby 1.8.7. delete function remove typo whitespace. fix typo whitespaces.
46 lines
1.1 KiB
Ruby
46 lines
1.1 KiB
Ruby
#
|
|
# delete.rb
|
|
#
|
|
|
|
# TODO(Krzysztof Wilczynski): We need to add support for regular expression ...
|
|
|
|
module Puppet::Parser::Functions
|
|
newfunction(:delete, :type => :rvalue, :doc => <<-EOS
|
|
Deletes all instances of a given element from an array, substring from a
|
|
string, or key from a hash.
|
|
|
|
*Examples:*
|
|
|
|
delete(['a','b','c','b'], 'b')
|
|
Would return: ['a','c']
|
|
|
|
delete({'a'=>1,'b'=>2,'c'=>3}, 'b')
|
|
Would return: {'a'=>1,'c'=>3}
|
|
|
|
delete('abracadabra', 'bra')
|
|
Would return: 'acada'
|
|
EOS
|
|
) do |arguments|
|
|
|
|
if (arguments.size != 2) then
|
|
raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
|
|
"given #{arguments.size} for 2.")
|
|
end
|
|
|
|
collection = arguments[0].dup
|
|
item = arguments[1]
|
|
|
|
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
|
|
end
|
|
end
|
|
|
|
# vim: set ts=2 sw=2 et :
|