From aafce9c99b779855e1e10e5a337848fa9f676a01 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sat, 30 Apr 2011 16:00:49 +0200 Subject: Moved more functions into lib/puppet/parser/functions/ --- lib/puppet/parser/functions/hash.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lib/puppet/parser/functions/hash.rb (limited to 'lib/puppet/parser/functions/hash.rb') diff --git a/lib/puppet/parser/functions/hash.rb b/lib/puppet/parser/functions/hash.rb new file mode 100644 index 0000000..f0c01d4 --- /dev/null +++ b/lib/puppet/parser/functions/hash.rb @@ -0,0 +1,34 @@ +# +# hash.rb +# + +module Puppet::Parser::Functions + newfunction(:hash, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "hash(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'hash(): Requires array to work with') + end + + result = {} + + begin + # This is to make it compatible with older version of Ruby ... + array = array.flatten + result = Hash[*array] + rescue Exception + raise(Puppet::ParseError, 'hash(): Unable to compute ' + + 'hash from array given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From a1cae426f1d6b8f2c19184ec8aac3ebc47d97744 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 23:09:30 +0100 Subject: (#3) Provide documentation for remaining functions. --- lib/puppet/parser/functions/hash.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/puppet/parser/functions/hash.rb') diff --git a/lib/puppet/parser/functions/hash.rb b/lib/puppet/parser/functions/hash.rb index f0c01d4..453ba1e 100644 --- a/lib/puppet/parser/functions/hash.rb +++ b/lib/puppet/parser/functions/hash.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:hash, :type => :rvalue, :doc => <<-EOS +This function converts and array into a hash. + +*Examples:* + + hash(['a',1,'b',2,'c',3]) + +Would return: {'a'=>1,'b'=>2,'c'=>3} EOS ) do |arguments| -- cgit v1.2.3