]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/merge.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / merge.rb
1 module Puppet::Parser::Functions
2   newfunction(:merge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
3     Merges two or more hashes together and returns the resulting hash.
4
5     For example:
6
7         $hash1 = {'one' => 1, 'two', => 2}
8         $hash2 = {'two' => 'dos', 'three', => 'tres'}
9         $merged_hash = merge($hash1, $hash2)
10         # The resulting hash is equivalent to:
11         # $merged_hash =  {'one' => 1, 'two' => 'dos', 'three' => 'tres'}
12
13     When there is a duplicate key, the key in the rightmost hash will "win."
14
15     ENDHEREDOC
16
17     if args.length < 2
18       raise Puppet::ParseError, ("merge(): wrong number of arguments (#{args.length}; must be at least 2)")
19     end
20
21     # The hash we accumulate into
22     accumulator = Hash.new
23     # Merge into the accumulator hash
24     args.each do |arg|
25       next if arg.is_a? String and arg.empty? # empty string is synonym for puppet's undef
26       unless arg.is_a?(Hash)
27         raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments"
28       end
29       accumulator.merge!(arg)
30     end
31     # Return the fully merged hash
32     accumulator
33   end
34 end