4 module Puppet::Parser::Functions
5 newfunction(:clamp, :type => :rvalue, :arity => -2, :doc => <<-DOC
6 Clamps value to a range.
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]
15 raise(Puppet::ParseError, 'clamp(): Wrong number of arguments, need three to clamp') if args.size != 3
21 raise(Puppet::ParseError, "clamp(): Required explicit numeric (#{value}:String)") unless value =~ %r{^\d+$}
23 raise(Puppet::ParseError, "clamp(): The Hash type is not allowed (#{value})")
27 # convert to numeric each element
28 # then sort them and get a middle value
29 args.map { |n| n.to_i }.sort[1]