X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Fany2array.rb;h=b45e5b50bf341bd9ad79bfe655acc5497fdb6493;hb=30caaa85aed7015ca0d77216bff175eebd917eb7;hp=e71407e89e73a5272ad0e95ce2463bec88327aed;hpb=ad88f67c13ae0f1a08936dad643f1e3509ab5f40;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/lib/puppet/parser/functions/any2array.rb b/3rdparty/modules/stdlib/lib/puppet/parser/functions/any2array.rb index e71407e89..b45e5b50b 100644 --- a/3rdparty/modules/stdlib/lib/puppet/parser/functions/any2array.rb +++ b/3rdparty/modules/stdlib/lib/puppet/parser/functions/any2array.rb @@ -1,31 +1,54 @@ # # any2array.rb # - module Puppet::Parser::Functions - newfunction(:any2array, :type => :rvalue, :doc => <<-EOS -This converts any object to an array containing that object. Empty argument -lists are converted to an empty array. Arrays are left untouched. Hashes are -converted to arrays of alternating keys and values. - EOS - ) do |arguments| + newfunction(:any2array, :type => :rvalue, :doc => <<-DOC + @summary + This converts any object to an array containing that object. + + Empty argument lists are converted to an empty array. Arrays are left + untouched. Hashes are converted to arrays of alternating keys and values. + + > *Note:* + since Puppet 5.0.0 it is possible to create new data types for almost any + datatype using the type system and the built-in + [`Array.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-array-and-tuple) + function is used to create a new Array.. + + ``` + $hsh = {'key' => 42, 'another-key' => 100} + notice(Array($hsh)) + ``` + + Would notice `[['key', 42], ['another-key', 100]]` + + The Array data type also has a special mode to "create an array if not already an array" + + ``` + notice(Array({'key' => 42, 'another-key' => 100}, true)) + ``` + + Would notice `[{'key' => 42, 'another-key' => 100}]`, as the `true` flag prevents the hash from being + transformed into an array. + + @return [Array] The new array containing the given object + DOC + ) do |arguments| if arguments.empty? - return [] + return [] end - if arguments.length == 1 - if arguments[0].kind_of?(Array) - return arguments[0] - elsif arguments[0].kind_of?(Hash) - result = [] - arguments[0].each do |key, value| - result << key << value - end - return result - end + return arguments unless arguments.length == 1 + return arguments[0] if arguments[0].is_a?(Array) + return [] if arguments == [''] + if arguments[0].is_a?(Hash) + result = [] + arguments[0].each do |key, value| + result << key << value + end + return result end - return arguments end end