Skip to content

Commit 2778224

Browse files
committed
Add clamp function
Clamp keeps value within the range. Employ of soft() makes the whole thing is independant of order.
1 parent 61333cf commit 2778224

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

README.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ Removes the record separator from the end of a string or an array of strings; fo
202202

203203
Returns a new string with the last character removed. If the string ends with '\r\n', both characters are removed. Applying `chop` to an empty string returns an empty string. If you want to merely remove record separators, then you should use the `chomp` function. Requires a string or an array of strings as input. *Type*: rvalue.
204204

205+
#### `clamp`
206+
207+
Keeps value within the range [Min, X, Max] by sort based on integer value (order of params doesn't matter). Takes strings, arrays or numerics. Strings are converted and compared numerically. Arrays of values are flattened into a list for further handling. For example:
208+
* `clamp('24', [575, 187])` returns 187.
209+
* `clamp(16, 88, 661)` returns 88.
210+
* `clamp([4, 3, '99'])` returns 4.
211+
*Type*: rvalue.
212+
205213
#### `concat`
206214

207215
Appends the contents of multiple arrays onto the first array given. For example:
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# clamp.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:clamp, :type => :rvalue, :arity => -2, :doc => <<-EOS
7+
Clamps value to a range.
8+
EOS
9+
) do |args|
10+
11+
args.flatten!
12+
13+
raise(Puppet::ParseError, 'clamp(): Wrong number of arguments, ' +
14+
'need three to clamp') if args.size != 3
15+
16+
# check values out
17+
args.each do |value|
18+
case [value.class]
19+
when [String]
20+
raise(Puppet::ParseError, "clamp(): Required explicit numeric (#{value}:String)") unless value =~ /^\d+$/
21+
when [Hash]
22+
raise(Puppet::ParseError, "clamp(): The Hash type is not allowed (#{value})")
23+
end
24+
end
25+
26+
# convert to numeric each element
27+
# then sort them and get a middle value
28+
args.map{ |n| n.to_i }.sort[1]
29+
end
30+
end

spec/acceptance/clamp_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper_acceptance'
3+
4+
describe 'clamp function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
5+
describe 'success' do
6+
it 'clamps list of values' do
7+
pp = <<-EOS
8+
$x = 17
9+
$y = 225
10+
$z = 155
11+
$o = clamp($x, $y, $z)
12+
if $o == $z {
13+
notify { 'output correct': }
14+
}
15+
EOS
16+
17+
apply_manifest(pp, :catch_failures => true) do |r|
18+
expect(r.stdout).to match(/Notice: output correct/)
19+
end
20+
end
21+
it 'clamps array of values' do
22+
pp = <<-EOS
23+
$a = [7, 19, 66]
24+
$b = 19
25+
$o = clamp($a)
26+
if $o == $b {
27+
notify { 'output correct': }
28+
}
29+
EOS
30+
31+
apply_manifest(pp, :catch_failures => true) do |r|
32+
expect(r.stdout).to match(/Notice: output correct/)
33+
end
34+
end
35+
end
36+
describe 'failure' do
37+
it 'handles improper argument counts'
38+
it 'handles no arguments'
39+
end
40+
end

spec/functions/clamp_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'spec_helper'
2+
3+
describe 'clamp' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
6+
it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
7+
it { is_expected.to run.with_params(12, 88, 71, 190).and_raise_error(Puppet::ParseError, /Wrong number of arguments, need three to clamp/) }
8+
it { is_expected.to run.with_params('12string', 88, 15).and_raise_error(Puppet::ParseError, /Required explicit numeric/) }
9+
it { is_expected.to run.with_params(1, 2, {'a' => 55}).and_raise_error(Puppet::ParseError, /The Hash type is not allowed/) }
10+
it { is_expected.to run.with_params('24', [575, 187]).and_return(187) }
11+
it { is_expected.to run.with_params([4, 3, '99']).and_return(4) }
12+
it { is_expected.to run.with_params(16, 750, 88).and_return(88) }
13+
it { is_expected.to run.with_params([3, 873], 73).and_return(73) }
14+
it { is_expected.to run.with_params([4], 8, 75).and_return(8) }
15+
it { is_expected.to run.with_params([6], [31], 9911).and_return(31) }
16+
end

0 commit comments

Comments
 (0)