summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>2011-04-23 17:45:49 +0100
committerKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>2011-04-23 17:45:49 +0100
commit908459c1ad7a14d338239e9ba428c56bbffee74c (patch)
tree576c16a5fe46192c8caa1acd4e794e7e3fa948b0
parent92d4a4eb85d72253ee6310b44dd37aa71478ce11 (diff)
First version. Function that allows to collect selected indices
from an array within Puppet manifest. More or less how array slicing works in Perl ... Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
-rw-r--r--collect_indices.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/collect_indices.rb b/collect_indices.rb
new file mode 100644
index 0000000..65abfce
--- /dev/null
+++ b/collect_indices.rb
@@ -0,0 +1,27 @@
+#
+# collect_indices.rb
+#
+
+module Puppet::Parser::Functions
+ newfunction(:collect_indices, :type => :rvalue, :doc => <<-EOS
+ EOS
+ ) do |arguments|
+
+ raise(Puppet::ParseError, "Wrong number of arguments " +
+ "given (#{arguments.size} for 2)") if arguments.size < 2
+
+ array = arguments.shift
+ indices = *arguments # Get them all ... Pokemon ...
+
+ if not indices or indices.empty?
+ raise(Puppet::ParseError, 'You must provide indices to collect')
+ end
+
+ # In Puppet numbers are often string-encoded ...
+ array = indices.collect { |i| array[i.to_i] }.compact
+
+ return array
+ end
+end
+
+# vim: set ts=2 sw=2 et :