From 27782242bc27dced7bed316a01c21fffd94e34f8 Mon Sep 17 00:00:00 2001 From: Michael Polenchuk Date: Wed, 18 Nov 2015 14:32:24 +0300 Subject: Add clamp function Clamp keeps value within the range. Employ of soft() makes the whole thing is independant of order. --- lib/puppet/parser/functions/clamp.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/puppet/parser/functions/clamp.rb (limited to 'lib/puppet/parser/functions/clamp.rb') diff --git a/lib/puppet/parser/functions/clamp.rb b/lib/puppet/parser/functions/clamp.rb new file mode 100644 index 0000000..432c7c1 --- /dev/null +++ b/lib/puppet/parser/functions/clamp.rb @@ -0,0 +1,30 @@ +# +# clamp.rb +# + +module Puppet::Parser::Functions + newfunction(:clamp, :type => :rvalue, :arity => -2, :doc => <<-EOS + Clamps value to a range. + EOS + ) 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 value =~ /^\d+$/ + 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 +end -- cgit v1.2.3