source: other-projects/hathitrust/vagrant-solr-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/delete_at.rb@ 30960

Last change on this file since 30960 was 30960, checked in by davidb, 7 years ago

Switch to using Puppet to provision machine. Strongly based on files developed for spark-hdfs cluster

File size: 1.1 KB
Line 
1#
2# delete_at.rb
3#
4
5module Puppet::Parser::Functions
6 newfunction(:delete_at, :type => :rvalue, :doc => <<-EOS
7Deletes a determined indexed value from an array.
8
9*Examples:*
10
11 delete_at(['a','b','c'], 1)
12
13Would return: ['a','c']
14 EOS
15 ) do |arguments|
16
17 raise(Puppet::ParseError, "delete_at(): Wrong number of arguments " +
18 "given (#{arguments.size} for 2)") if arguments.size < 2
19
20 array = arguments[0]
21
22 unless array.is_a?(Array)
23 raise(Puppet::ParseError, 'delete_at(): Requires array to work with')
24 end
25
26 index = arguments[1]
27
28 if index.is_a?(String) and not index.match(/^\d+$/)
29 raise(Puppet::ParseError, 'delete_at(): You must provide ' +
30 'non-negative numeric index')
31 end
32
33 result = array.clone
34
35 # Numbers in Puppet are often string-encoded which is troublesome ...
36 index = index.to_i
37
38 if index > result.size - 1 # First element is at index 0 is it not?
39 raise(Puppet::ParseError, 'delete_at(): Given index ' +
40 'exceeds size of array given')
41 end
42
43 result.delete_at(index) # We ignore the element that got deleted ...
44
45 return result
46 end
47end
48
49# vim: set ts=2 sw=2 et :
Note: See TracBrowser for help on using the repository browser.