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>
This commit is contained in:
parent
92d4a4eb85
commit
908459c1ad
1 changed files with 27 additions and 0 deletions
27
collect_indices.rb
Normal file
27
collect_indices.rb
Normal file
|
@ -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 :
|
Loading…
Reference in a new issue