Puppet Function: clamp
- Defined in:
- vendor_modules/stdlib/lib/puppet/parser/functions/clamp.rb
- Function type:
- Ruby 3.x API
Summary
Keeps value within the range [Min, X, Max] by sort based on integer value (parameter order doesn't matter).Overview
Strings are converted and compared numerically. Arrays of values are flattened into a list for further handling.
> Note:
From Puppet 6.0.0 this can be done with only core Puppet like this:
`[$minval, $maxval, $value_to_clamp].sort[1]`
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'vendor_modules/stdlib/lib/puppet/parser/functions/clamp.rb', line 7 newfunction(:clamp, type: :rvalue, arity: -2, doc: <<-DOC @summary Keeps value within the range [Min, X, Max] by sort based on integer value (parameter order doesn't matter). Strings are converted and compared numerically. Arrays of values are flattened into a list for further handling. @example Example usage clamp('24', [575, 187])` returns 187. clamp(16, 88, 661)` returns 88. clamp([4, 3, '99'])` returns 4. > *Note:* From Puppet 6.0.0 this can be done with only core Puppet like this: `[$minval, $maxval, $value_to_clamp].sort[1]` @return [Array[Integer]] The sorted Array DOC ) do |args| args.flatten! raise(Puppet::ParseError, 'clamp(): Wrong number of arguments, need three to clamp') if args.size != 3 # check values out args.each do |value| case [value.class] when [String] raise(Puppet::ParseError, "clamp(): Required explicit numeric (#{value}:String)") unless %r{^\d+$}.match?(value) when [Hash] raise(Puppet::ParseError, "clamp(): The Hash type is not allowed (#{value})") end end # convert to numeric each element # then sort them and get a middle value args.map { |n| n.to_i }.sort[1] end |