]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/merge.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / 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       unless arg.is_a?(Hash)
26         raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments"
27       end
28       accumulator.merge!(arg)
29     end
30     # Return the fully merged hash
31     accumulator
32   end
33 end