Skip to content

Commit f46c9fd

Browse files
committed
Merge pull request #592 from jyaworski/fix_delete
Use reject instead of delete_if
2 parents f48747b + 540546b commit f46c9fd

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

README.markdown

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ Takes a resource reference and an optional hash of attributes. Returns 'true' if
263263

264264
#### `delete`
265265

266-
Deletes all instances of a given element from an array, substring from a string, or key from a hash. For example, `delete(['a','b','c','b'], 'b')` returns ['a','c']; `delete('abracadabra', 'bra')` returns 'acada'. `delete({'a' => 1,'b' => 2,'c' => 3},['b','c'])` returns {'a'=> 1}. *Type*: rvalue.
266+
Deletes all instances of a given element from an array, substring from a string, or key from a hash. Arrays and hashes may also match on regular expressions by providing a full regular expression.
267+
268+
For example, `delete(['a','b','c','b'], 'b')` returns ['a','c']; `delete('abracadabra', 'bra')` returns 'acada'. `delete({'a' => 1,'b' => 2,'c' => 3},['b','c'])` returns {'a'=> 1}, `delete(['abf', 'ab', 'ac'], '^ab.*')` returns ['ac']. `delete(['ab', 'b'], 'b')` returns ['ab'].
269+
270+
*Type*: rvalue.
267271

268272
#### `delete_at`
269273

lib/puppet/parser/functions/delete.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
# delete.rb
33
#
44

5-
# TODO(Krzysztof Wilczynski): We need to add support for regular expression ...
6-
75
module Puppet::Parser::Functions
86
newfunction(:delete, :type => :rvalue, :doc => <<-EOS
97
Deletes all instances of a given element from an array, substring from a
@@ -22,19 +20,23 @@ module Puppet::Parser::Functions
2220
2321
delete('abracadabra', 'bra')
2422
Would return: 'acada'
23+
24+
delete(['abracadabra'], '^.*bra.*$')
25+
Would return: []
26+
27+
delete(['abracadabra'], '^.*jimbob.*$')
28+
Would return: ['abracadabra']
2529
EOS
2630
) do |arguments|
2731

28-
if (arguments.size != 2) then
29-
raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
30-
"given #{arguments.size} for 2.")
31-
end
32+
raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
33+
"given #{arguments.size} for 2") unless arguments.size == 2
3234

3335
collection = arguments[0].dup
3436
Array(arguments[1]).each do |item|
3537
case collection
3638
when Array, Hash
37-
collection.delete item
39+
collection.reject! { |coll_item| (coll_item =~ %r{\b#{item}\b}) }
3840
when String
3941
collection.gsub! item, ''
4042
else

spec/functions/delete_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
it { is_expected.not_to eq(nil) }
55
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
66
it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
7+
it { is_expected.to run.with_params([], 'two') }
78
it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) }
89
it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) }
910

@@ -12,11 +13,15 @@
1213
it { is_expected.to run.with_params([], 'two').and_return([]) }
1314
it { is_expected.to run.with_params(['two'], 'two').and_return([]) }
1415
it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) }
16+
it { is_expected.to run.with_params(['one', 'two', 'three'], '^t.*').and_return(['one']) }
17+
it { is_expected.to run.with_params(['ab', 'b', 'c', 'b'], 'b').and_return(['ab', 'c']) }
1518
it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) }
1619
it { is_expected.to run.with_params(['one', 'two', 'three'], 'e').and_return(['one', 'two', 'three']) }
1720
it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
1821
it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) }
1922
it { is_expected.to run.with_params(['one', 'two', 'three', 'two'], ['one', 'two']).and_return(['three']) }
23+
it { is_expected.to run.with_params(['abracadabra'], 'abr').and_return(['abracadabra']) }
24+
it { is_expected.to run.with_params(['abracadabra'], '^.*jimbob.*$').and_return(['abracadabra']) }
2025
end
2126

2227
describe 'deleting from a string' do

0 commit comments

Comments
 (0)