diff options
author | Joseph Yaworski <joseph.yaworski@fireeye.com> | 2016-04-12 16:53:07 -0400 |
---|---|---|
committer | Joseph Yaworski <joseph.yaworski@fireeye.com> | 2016-05-04 11:32:10 -0400 |
commit | 540546b9b41745bbc4821f9966ae301dc0b5056a (patch) | |
tree | baa574d4e850aec389288bc943368bfb8de3dfe9 | |
parent | f48747b8af8fdad455cfd5b5d0d2c2abfd8c9415 (diff) |
Use reject instead of delete_if
-rw-r--r-- | README.markdown | 6 | ||||
-rw-r--r-- | lib/puppet/parser/functions/delete.rb | 16 | ||||
-rwxr-xr-x | spec/functions/delete_spec.rb | 5 |
3 files changed, 19 insertions, 8 deletions
diff --git a/README.markdown b/README.markdown index 1ef6509..298b852 100644 --- a/README.markdown +++ b/README.markdown @@ -263,7 +263,11 @@ Takes a resource reference and an optional hash of attributes. Returns 'true' if #### `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` diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index f548b44..814e1ad 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -2,8 +2,6 @@ # delete.rb # -# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... - module Puppet::Parser::Functions newfunction(:delete, :type => :rvalue, :doc => <<-EOS 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') Would return: 'acada' + + delete(['abracadabra'], '^.*bra.*$') + Would return: [] + + delete(['abracadabra'], '^.*jimbob.*$') + Would return: ['abracadabra'] EOS ) do |arguments| - if (arguments.size != 2) then - raise(Puppet::ParseError, "delete(): Wrong number of arguments "+ - "given #{arguments.size} for 2.") - end + raise(Puppet::ParseError, "delete(): Wrong number of arguments "+ + "given #{arguments.size} for 2") unless arguments.size == 2 collection = arguments[0].dup Array(arguments[1]).each do |item| case collection when Array, Hash - collection.delete item + collection.reject! { |coll_item| (coll_item =~ %r{\b#{item}\b}) } when String collection.gsub! item, '' else diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb index cf696ac..fd2a8ad 100755 --- a/spec/functions/delete_spec.rb +++ b/spec/functions/delete_spec.rb @@ -4,6 +4,7 @@ describe 'delete' do 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([], 'two') } 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) } @@ -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'], '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'], '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(['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(['abracadabra'], 'abr').and_return(['abracadabra']) } + it { is_expected.to run.with_params(['abracadabra'], '^.*jimbob.*$').and_return(['abracadabra']) } end describe 'deleting from a string' do |