Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / clamp.rb
1 #
2 # clamp.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:clamp, :type => :rvalue, :arity => -2, :doc => <<-DOC
6     Clamps value to a range.
7
8     Note: From Puppet 6.0.0 this can be done with only core Puppet like this:
9       [$minval, $maxval, $value_to_clamp].sort[1]
10     DOC
11              ) do |args|
12
13     args.flatten!
14
15     raise(Puppet::ParseError, 'clamp(): Wrong number of arguments, need three to clamp') if args.size != 3
16
17     # check values out
18     args.each do |value|
19       case [value.class]
20       when [String]
21         raise(Puppet::ParseError, "clamp(): Required explicit numeric (#{value}:String)") unless value =~ %r{^\d+$}
22       when [Hash]
23         raise(Puppet::ParseError, "clamp(): The Hash type is not allowed (#{value})")
24       end
25     end
26
27     # convert to numeric each element
28     # then sort them and get a middle value
29     args.map { |n| n.to_i }.sort[1]
30   end
31 end