Merge pull request #592 from jyaworski/fix_delete
Use reject instead of delete_if
This commit is contained in:
commit
f46c9fdbce
3 changed files with 19 additions and 8 deletions
|
@ -263,7 +263,11 @@ Takes a resource reference and an optional hash of attributes. Returns 'true' if
|
||||||
|
|
||||||
#### `delete`
|
#### `delete`
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
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'].
|
||||||
|
|
||||||
|
*Type*: rvalue.
|
||||||
|
|
||||||
#### `delete_at`
|
#### `delete_at`
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
# delete.rb
|
# delete.rb
|
||||||
#
|
#
|
||||||
|
|
||||||
# TODO(Krzysztof Wilczynski): We need to add support for regular expression ...
|
|
||||||
|
|
||||||
module Puppet::Parser::Functions
|
module Puppet::Parser::Functions
|
||||||
newfunction(:delete, :type => :rvalue, :doc => <<-EOS
|
newfunction(:delete, :type => :rvalue, :doc => <<-EOS
|
||||||
Deletes all instances of a given element from an array, substring from a
|
Deletes all instances of a given element from an array, substring from a
|
||||||
|
@ -22,19 +20,23 @@ string, or key from a hash.
|
||||||
|
|
||||||
delete('abracadabra', 'bra')
|
delete('abracadabra', 'bra')
|
||||||
Would return: 'acada'
|
Would return: 'acada'
|
||||||
|
|
||||||
|
delete(['abracadabra'], '^.*bra.*$')
|
||||||
|
Would return: []
|
||||||
|
|
||||||
|
delete(['abracadabra'], '^.*jimbob.*$')
|
||||||
|
Would return: ['abracadabra']
|
||||||
EOS
|
EOS
|
||||||
) do |arguments|
|
) do |arguments|
|
||||||
|
|
||||||
if (arguments.size != 2) then
|
raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
|
||||||
raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
|
"given #{arguments.size} for 2") unless arguments.size == 2
|
||||||
"given #{arguments.size} for 2.")
|
|
||||||
end
|
|
||||||
|
|
||||||
collection = arguments[0].dup
|
collection = arguments[0].dup
|
||||||
Array(arguments[1]).each do |item|
|
Array(arguments[1]).each do |item|
|
||||||
case collection
|
case collection
|
||||||
when Array, Hash
|
when Array, Hash
|
||||||
collection.delete item
|
collection.reject! { |coll_item| (coll_item =~ %r{\b#{item}\b}) }
|
||||||
when String
|
when String
|
||||||
collection.gsub! item, ''
|
collection.gsub! item, ''
|
||||||
else
|
else
|
||||||
|
|
|
@ -4,6 +4,7 @@ describe 'delete' do
|
||||||
it { is_expected.not_to eq(nil) }
|
it { is_expected.not_to eq(nil) }
|
||||||
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
|
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
|
||||||
it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
|
it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
|
||||||
|
it { is_expected.to run.with_params([], 'two') }
|
||||||
it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) }
|
it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) }
|
||||||
it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) }
|
it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) }
|
||||||
|
|
||||||
|
@ -12,11 +13,15 @@ describe 'delete' do
|
||||||
it { is_expected.to run.with_params([], 'two').and_return([]) }
|
it { is_expected.to run.with_params([], 'two').and_return([]) }
|
||||||
it { is_expected.to run.with_params(['two'], 'two').and_return([]) }
|
it { is_expected.to run.with_params(['two'], 'two').and_return([]) }
|
||||||
it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) }
|
it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) }
|
||||||
|
it { is_expected.to run.with_params(['one', 'two', 'three'], '^t.*').and_return(['one']) }
|
||||||
|
it { is_expected.to run.with_params(['ab', 'b', 'c', 'b'], 'b').and_return(['ab', 'c']) }
|
||||||
it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) }
|
it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) }
|
||||||
it { is_expected.to run.with_params(['one', 'two', 'three'], 'e').and_return(['one', 'two', 'three']) }
|
it { is_expected.to run.with_params(['one', 'two', 'three'], 'e').and_return(['one', 'two', 'three']) }
|
||||||
it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
|
it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
|
||||||
it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) }
|
it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) }
|
||||||
it { is_expected.to run.with_params(['one', 'two', 'three', 'two'], ['one', 'two']).and_return(['three']) }
|
it { is_expected.to run.with_params(['one', 'two', 'three', 'two'], ['one', 'two']).and_return(['three']) }
|
||||||
|
it { is_expected.to run.with_params(['abracadabra'], 'abr').and_return(['abracadabra']) }
|
||||||
|
it { is_expected.to run.with_params(['abracadabra'], '^.*jimbob.*$').and_return(['abracadabra']) }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'deleting from a string' do
|
describe 'deleting from a string' do
|
||||||
|
|
Loading…
Reference in a new issue