summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions
diff options
context:
space:
mode:
authorMichael Polenchuk <mpolenchuk@mirantis.com>2015-11-18 14:32:24 +0300
committerMichael Polenchuk <mpolenchuk@mirantis.com>2015-12-31 12:46:07 +0300
commit27782242bc27dced7bed316a01c21fffd94e34f8 (patch)
tree67bd90fde8238bfff754d2fcdd782c9fbc6d88f0 /lib/puppet/parser/functions
parent61333cfc48026af204483d681bd8b10cb44d6fc6 (diff)
Add clamp function
Clamp keeps value within the range. Employ of soft() makes the whole thing is independant of order.
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r--lib/puppet/parser/functions/clamp.rb30
1 files changed, 30 insertions, 0 deletions
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