summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions
diff options
context:
space:
mode:
authorJoseph Yaworski <joseph.yaworski@fireeye.com>2016-05-11 13:21:24 -0400
committerJoseph Yaworski <joseph.yaworski@fireeye.com>2016-05-13 13:25:35 -0400
commitdd71c0288052dd3a96e730ff198f5c0a8d640946 (patch)
treefd55b7e18c892092ac37276e998356e91388bdce /lib/puppet/parser/functions
parentecfdbb2690359434f1737d05ce723cf2f5d05c4e (diff)
Add a delete_regex function
To maintain backwards compatibility, add a delete_regex function instead of modifying delete itself.
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r--lib/puppet/parser/functions/delete.rb8
-rw-r--r--lib/puppet/parser/functions/delete_regex.rb45
2 files changed, 46 insertions, 7 deletions
diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb
index 814e1ad..466c55c 100644
--- a/lib/puppet/parser/functions/delete.rb
+++ b/lib/puppet/parser/functions/delete.rb
@@ -20,12 +20,6 @@ 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|
@@ -36,7 +30,7 @@ string, or key from a hash.
Array(arguments[1]).each do |item|
case collection
when Array, Hash
- collection.reject! { |coll_item| (coll_item =~ %r{\b#{item}\b}) }
+ collection.delete item
when String
collection.gsub! item, ''
else
diff --git a/lib/puppet/parser/functions/delete_regex.rb b/lib/puppet/parser/functions/delete_regex.rb
new file mode 100644
index 0000000..d72b3e9
--- /dev/null
+++ b/lib/puppet/parser/functions/delete_regex.rb
@@ -0,0 +1,45 @@
+#
+# delete_regex.rb
+#
+
+module Puppet::Parser::Functions
+ newfunction(:delete_regex, :type => :rvalue, :doc => <<-EOS
+deletes all instances of a given element that match a regular expression
+from an array or key from a hash. Multiple regular expressions are assumed
+to be matched as an OR.
+
+*Examples:*
+
+ delete_regex(['a','b','c','b'], 'b')
+ Would return: ['a','c']
+
+ delete_regex(['a','b','c','b'], ['b', 'c'])
+ Would return: ['a']
+
+ delete_regex({'a'=>1,'b'=>2,'c'=>3}, 'b')
+ Would return: {'a'=>1,'c'=>3}
+
+ delete_regex({'a'=>1,'b'=>2,'c'=>3}, '^a$')
+ Would return: {'b'=>2,'c'=>3}
+
+ EOS
+ ) do |arguments|
+
+ raise(Puppet::ParseError, "delete_regex(): 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, String
+ collection.reject! { |coll_item| (coll_item =~ %r{\b#{item}\b}) }
+ else
+ raise(TypeError, "delete_regex(): First argument must be an Array, " +
+ "Hash, or String. Given an argument of class #{collection.class}.")
+ end
+ end
+ collection
+ end
+end
+
+# vim: set ts=2 sw=2 et :