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:
864 bytes
|
Line | |
---|
1 | #
|
---|
2 | # hash.rb
|
---|
3 | #
|
---|
4 |
|
---|
5 | module Puppet::Parser::Functions
|
---|
6 | newfunction(:hash, :type => :rvalue, :doc => <<-EOS
|
---|
7 | This function converts an array into a hash.
|
---|
8 |
|
---|
9 | *Examples:*
|
---|
10 |
|
---|
11 | hash(['a',1,'b',2,'c',3])
|
---|
12 |
|
---|
13 | Would return: {'a'=>1,'b'=>2,'c'=>3}
|
---|
14 | EOS
|
---|
15 | ) do |arguments|
|
---|
16 |
|
---|
17 | raise(Puppet::ParseError, "hash(): Wrong number of arguments " +
|
---|
18 | "given (#{arguments.size} for 1)") if arguments.size < 1
|
---|
19 |
|
---|
20 | array = arguments[0]
|
---|
21 |
|
---|
22 | unless array.is_a?(Array)
|
---|
23 | raise(Puppet::ParseError, 'hash(): Requires array to work with')
|
---|
24 | end
|
---|
25 |
|
---|
26 | result = {}
|
---|
27 |
|
---|
28 | begin
|
---|
29 | # This is to make it compatible with older version of Ruby ...
|
---|
30 | array = array.flatten
|
---|
31 | result = Hash[*array]
|
---|
32 | rescue StandardError
|
---|
33 | raise(Puppet::ParseError, 'hash(): Unable to compute ' +
|
---|
34 | 'hash from array given')
|
---|
35 | end
|
---|
36 |
|
---|
37 | return result
|
---|
38 | end
|
---|
39 | end
|
---|
40 |
|
---|
41 | # vim: set ts=2 sw=2 et :
|
---|
Note:
See
TracBrowser
for help on using the repository browser.