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}
|
|
|
|
|
2014-12-19 19:41:07 +01:00
|
|
|
delete({'a'=>1,'b'=>2,'c'=>3}, ['b','c'])
|
|
|
|
Would return: {'a'=>1}
|
|
|
|
|
2012-11-18 07:27:15 +01:00
|
|
|
delete('abracadabra', 'bra')
|
|
|
|
Would return: 'acada'
|
2014-12-19 19:26:01 +01:00
|
|
|
EOS
|
2011-04-30 04:16:12 +02:00
|
|
|
) 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 "+
|
2014-12-19 19:26:01 +01:00
|
|
|
"given #{arguments.size} for 2.")
|
2011-06-29 22:21:55 +02:00
|
|
|
end
|
|
|
|
|
bug # 20681 delete() function should not remove elements from original list
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.
2013-09-09 15:27:54 +02:00
|
|
|
collection = arguments[0].dup
|
2014-12-19 19:26:01 +01:00
|
|
|
Array(arguments[1]).each do |item|
|
|
|
|
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
|
2012-11-18 07:27:15 +01:00
|
|
|
end
|
|
|
|
collection
|
2011-04-30 04:16:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 et :
|