diff options
author | David Schmitt <david.schmitt@puppetlabs.com> | 2016-01-04 15:35:42 +0000 |
---|---|---|
committer | David Schmitt <david.schmitt@puppetlabs.com> | 2016-01-04 15:35:42 +0000 |
commit | 9cce93054aaeafeddf38fa48d22d5f123b94adff (patch) | |
tree | da4c94980a3cd6fb31974fea5aa9bda1ed099370 /lib | |
parent | 0073c6d8de4fa0a042e82287c8a1e9603601a57b (diff) | |
parent | 27782242bc27dced7bed316a01c21fffd94e34f8 (diff) |
Merge pull request #545 from mpolenchuk/master
Add clamp function
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/parser/functions/clamp.rb | 30 |
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 |