Skip to content

Commit 7335086

Browse files
committed
Merge pull request #591 from jyaworski/delete_regex
Add support for regular expressions to delete
2 parents e3a6e2c + 0d46515 commit 7335086

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ Takes a resource reference and an optional hash of attributes. Returns 'true' if
265265

266266
#### `delete`
267267

268-
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.
268+
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. 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']. *Type*: rvalue.
269269

270270
#### `delete_at`
271271

lib/puppet/parser/functions/delete.rb

Lines changed: 1 addition & 3 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
@@ -34,7 +32,7 @@ module Puppet::Parser::Functions
3432
Array(arguments[1]).each do |item|
3533
case collection
3634
when Array, Hash
37-
collection.delete item
35+
collection.delete_if { |coll_item| coll_item =~ %r{#{item}} }
3836
when String
3937
collection.gsub! item, ''
4038
else

spec/functions/delete_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
it { is_expected.to run.with_params([], 'two').and_return([]) }
1313
it { is_expected.to run.with_params(['two'], 'two').and_return([]) }
1414
it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) }
15+
it { is_expected.to run.with_params(['one', 'two', 'three'], '^t.*').and_return(['one']) }
1516
it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) }
1617
it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
1718
it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) }
@@ -32,7 +33,7 @@
3233
it { is_expected.to run.with_params('barfoobar', ['foo', 'barbar']).and_return('') }
3334
end
3435

35-
describe 'deleting from an array' do
36+
describe 'deleting from a hash' do
3637
it { is_expected.to run.with_params({}, '').and_return({}) }
3738
it { is_expected.to run.with_params({}, 'key').and_return({}) }
3839
it { is_expected.to run.with_params({'key' => 'value'}, 'key').and_return({}) }
@@ -44,6 +45,10 @@
4445
.with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['key1', 'key2']) \
4546
.and_return( {'key3' => 'value3'})
4647
}
48+
it { is_expected.to run \
49+
.with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['^key\d']) \
50+
.and_return({})
51+
}
4752
end
4853

4954
it "should leave the original array intact" do

0 commit comments

Comments
 (0)