Skip to content

Commit dd71c02

Browse files
author
Joseph Yaworski
committed
Add a delete_regex function
To maintain backwards compatibility, add a delete_regex function instead of modifying delete itself.
1 parent ecfdbb2 commit dd71c02

5 files changed

Lines changed: 110 additions & 12 deletions

File tree

README.markdown

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,24 @@ 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. Arrays and hashes may also match on regular expressions by providing a full regular expression.
266+
Deletes all instances of a given element from an array, substring from a string, or key from a hash.
267267

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'].
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(['ab', 'b'], 'b')` returns ['ab'].
269269

270270
*Type*: rvalue.
271271

272272
#### `delete_at`
273273

274274
Deletes a determined indexed value from an array. For example, `delete_at(['a','b','c'], 1)` returns ['a','c']. *Type*: rvalue.
275275

276+
#### `delete_regex`
277+
278+
Deletes all instances of a given element from an array or hash that match a provided regular expression. A string will be treated as a one-item array.
279+
280+
For example, `delete_regex(['a','b','c','b'], 'b')` returns ['a','c']; `delete_regex({'a' => 1,'b' => 2,'c' => 3},['b','c'])` returns {'a'=> 1}, `delete_regex(['abf', 'ab', 'ac'], '^ab.*')` returns ['ac']. `delete_regex(['ab', 'b'], 'b')` returns ['ab'].
281+
282+
*Type*: rvalue.
283+
276284
#### `delete_values`
277285

278286
Deletes all instances of a given value from a hash. For example, `delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B')` returns {'a'=>'A','c'=>'C','B'=>'D'} *Type*: rvalue.

lib/puppet/parser/functions/delete.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ module Puppet::Parser::Functions
2020
2121
delete('abracadabra', 'bra')
2222
Would return: 'acada'
23-
24-
delete(['abracadabra'], '^.*bra.*$')
25-
Would return: []
26-
27-
delete(['abracadabra'], '^.*jimbob.*$')
28-
Would return: ['abracadabra']
2923
EOS
3024
) do |arguments|
3125

@@ -36,7 +30,7 @@ module Puppet::Parser::Functions
3630
Array(arguments[1]).each do |item|
3731
case collection
3832
when Array, Hash
39-
collection.reject! { |coll_item| (coll_item =~ %r{\b#{item}\b}) }
33+
collection.delete item
4034
when String
4135
collection.gsub! item, ''
4236
else
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# delete_regex.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:delete_regex, :type => :rvalue, :doc => <<-EOS
7+
deletes all instances of a given element that match a regular expression
8+
from an array or key from a hash. Multiple regular expressions are assumed
9+
to be matched as an OR.
10+
11+
*Examples:*
12+
13+
delete_regex(['a','b','c','b'], 'b')
14+
Would return: ['a','c']
15+
16+
delete_regex(['a','b','c','b'], ['b', 'c'])
17+
Would return: ['a']
18+
19+
delete_regex({'a'=>1,'b'=>2,'c'=>3}, 'b')
20+
Would return: {'a'=>1,'c'=>3}
21+
22+
delete_regex({'a'=>1,'b'=>2,'c'=>3}, '^a$')
23+
Would return: {'b'=>2,'c'=>3}
24+
25+
EOS
26+
) do |arguments|
27+
28+
raise(Puppet::ParseError, "delete_regex(): Wrong number of arguments "+
29+
"given #{arguments.size} for 2") unless arguments.size == 2
30+
31+
collection = arguments[0].dup
32+
Array(arguments[1]).each do |item|
33+
case collection
34+
when Array, Hash, String
35+
collection.reject! { |coll_item| (coll_item =~ %r{\b#{item}\b}) }
36+
else
37+
raise(TypeError, "delete_regex(): First argument must be an Array, " +
38+
"Hash, or String. Given an argument of class #{collection.class}.")
39+
end
40+
end
41+
collection
42+
end
43+
end
44+
45+
# vim: set ts=2 sw=2 et :
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'spec_helper'
2+
3+
describe 'delete_regex' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
6+
it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
7+
it { is_expected.to run.with_params([], 'two') }
8+
it { is_expected.to run.with_params({}, 'two') }
9+
it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) }
10+
it { is_expected.to run.with_params([], 'two', 'three', 'four').and_raise_error(Puppet::ParseError) }
11+
it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) }
12+
13+
describe 'deleting from an array' do
14+
it { is_expected.to run.with_params([], '').and_return([]) }
15+
it { is_expected.to run.with_params([], 'two').and_return([]) }
16+
it { is_expected.to run.with_params(['two'], 'two').and_return([]) }
17+
it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) }
18+
it { is_expected.to run.with_params(['one', 'two', 'three'], '^t.*').and_return(['one']) }
19+
it { is_expected.to run.with_params(['ab', 'b', 'c', 'b'], 'b').and_return(['ab', 'c']) }
20+
it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) }
21+
it { is_expected.to run.with_params(['one', 'two', 'three'], 'e').and_return(['one', 'two', 'three']) }
22+
it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
23+
it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) }
24+
it { is_expected.to run.with_params(['abracadabra'], 'abr').and_return(['abracadabra']) }
25+
it { is_expected.to run.with_params(['abracadabra'], '^.*jimbob.*$').and_return(['abracadabra']) }
26+
end
27+
28+
describe 'deleting from an array' do
29+
it { is_expected.to run.with_params({}, '').and_return({}) }
30+
it { is_expected.to run.with_params({}, 'key').and_return({}) }
31+
it { is_expected.to run.with_params({'key' => 'value'}, 'key').and_return({}) }
32+
it { is_expected.to run \
33+
.with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, 'key2') \
34+
.and_return( {'key1' => 'value1', 'key3' => 'value3'})
35+
}
36+
it { is_expected.to run \
37+
.with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['key1', 'key2']) \
38+
.and_return( {'key3' => 'value3'})
39+
}
40+
end
41+
42+
it "should leave the original array intact" do
43+
argument1 = ['one','two','three']
44+
original1 = argument1.dup
45+
subject.call([argument1,'two'])
46+
expect(argument1).to eq(original1)
47+
end
48+
it "should leave the original hash intact" do
49+
argument1 = {'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}
50+
original1 = argument1.dup
51+
subject.call([argument1,'key2'])
52+
expect(argument1).to eq(original1)
53+
end
54+
end

spec/functions/delete_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
it { is_expected.to run.with_params([], 'two').and_return([]) }
1414
it { is_expected.to run.with_params(['two'], 'two').and_return([]) }
1515
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']) }
1716
it { is_expected.to run.with_params(['ab', 'b', 'c', 'b'], 'b').and_return(['ab', 'c']) }
1817
it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) }
1918
it { is_expected.to run.with_params(['one', 'two', 'three'], 'e').and_return(['one', 'two', 'three']) }
2019
it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
2120
it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) }
2221
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']) }
2522
end
2623

2724
describe 'deleting from a string' do

0 commit comments

Comments
 (0)