Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / clamp.rb
1 #
2 # clamp.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:clamp, :type => :rvalue, :arity => -2, :doc => <<-EOS
7     Clamps value to a range.
8     EOS
9   ) do |args|
10
11     args.flatten!
12
13     raise(Puppet::ParseError, 'clamp(): Wrong number of arguments, need three to clamp') if args.size != 3
14
15     # check values out
16     args.each do |value|
17       case [value.class]
18         when [String]
19           raise(Puppet::ParseError, "clamp(): Required explicit numeric (#{value}:String)") unless value =~ /^\d+$/
20         when [Hash]
21           raise(Puppet::ParseError, "clamp(): The Hash type is not allowed (#{value})")
22       end
23     end
24
25     # convert to numeric each element
26     # then sort them and get a middle value
27     args.map{ |n| n.to_i }.sort[1]
28   end
29 end