From eefd7a9fc4951b523419b207ea197a481878745d Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sun, 24 Apr 2011 01:35:49 +0100 Subject: Adding ability to specifiy range as the index when selecting indices to collect. Signed-off-by: Krzysztof Wilczynski --- collect_indices.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/collect_indices.rb b/collect_indices.rb index e96c3e9..cd087b9 100644 --- a/collect_indices.rb +++ b/collect_indices.rb @@ -22,8 +22,28 @@ module Puppet::Parser::Functions 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 + indices_list = [] + + indices.each do |i| + if m = i.match(/^(\d+)\-(\d+)$/) + start = m[1].to_i + stop = m[2].to_i + + raise(Puppet::ParseError, 'Stop index in given indices range ' + + 'is smaller than the start index') if start > stop + + (start .. stop).each { |i| indices_list << i.to_i } + else + if not i.match(/^\w+$/) + raise(Puppet::ParseError, 'Unknown format of given index') + end + + # In Puppet numbers are often string-encoded ... + indices_list << i.to_i + end + end + + array = indices_list.collect { |i| array[i] }.compact return array end -- cgit v1.2.3