]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/hash.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / hash.rb
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 Exception
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 :