summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTP Honey <tphoney@users.noreply.github.com>2017-07-28 12:03:14 +0100
committerGitHub <noreply@github.com>2017-07-28 12:03:14 +0100
commit0c8697477255f2ca6c477c0a960cd151478729e1 (patch)
tree218de9aae6613515476bb29171d92e658ccaa7c9
parenta8ce26dbc97c66a154607b8751cb59addd14db77 (diff)
parent772a2d2f406b65ef65161a04455865b37a1c8456 (diff)
Merge pull request #798 from eputnam/round_func
(maint) move/rewrite round() as ruby function
-rw-r--r--README.md2
-rw-r--r--functions/round.pp9
-rw-r--r--lib/puppet/parser/functions/round.rb33
-rwxr-xr-xspec/functions/round_spec.rb5
4 files changed, 38 insertions, 11 deletions
diff --git a/README.md b/README.md
index a8b93ca..b910140 100644
--- a/README.md
+++ b/README.md
@@ -1550,7 +1550,7 @@ For example, `reject(['aaa','bbb','ccc','aaaddd'], 'aaa')` returns ['bbb','ccc']
Reverses the order of a string or array.
-#### `stdlib::round`
+#### `round`
Rounds a number to the nearest integer
diff --git a/functions/round.pp b/functions/round.pp
deleted file mode 100644
index 6bad92d..0000000
--- a/functions/round.pp
+++ /dev/null
@@ -1,9 +0,0 @@
-function stdlib::round(
- Numeric $input,
-) {
- if $input >= 0 {
- Integer( $input + 0.5 )
- } else {
- Integer( $input - 0.5 )
- }
-}
diff --git a/lib/puppet/parser/functions/round.rb b/lib/puppet/parser/functions/round.rb
new file mode 100644
index 0000000..489c301
--- /dev/null
+++ b/lib/puppet/parser/functions/round.rb
@@ -0,0 +1,33 @@
+#
+# round.rb
+#
+
+module Puppet::Parser::Functions
+ newfunction(:round, :type => :rvalue, :doc => <<-EOS
+ Rounds a number to the nearest integer
+
+ *Examples:*
+
+ round(2.9)
+
+ returns: 3
+
+ round(2.4)
+
+ returns: 2
+
+ EOS
+ ) do |args|
+
+ raise Puppet::ParseError, "round(): Wrong number of arguments given #{args.size} for 1" if args.size != 1
+ raise Puppet::ParseError, "round(): Expected a Numeric, got #{args[0].class}" unless args[0].is_a? Numeric
+
+ value = args[0]
+
+ if value >= 0
+ Integer(value + 0.5)
+ else
+ Integer(value - 0.5)
+ end
+ end
+end
diff --git a/spec/functions/round_spec.rb b/spec/functions/round_spec.rb
index 5aa801c..8b13478 100755
--- a/spec/functions/round_spec.rb
+++ b/spec/functions/round_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'
-describe 'stdlib::round' do
+describe 'round' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params(34.3).and_return(34) }
it { is_expected.to run.with_params(-34.3).and_return(-34) }
@@ -8,4 +8,7 @@ describe 'stdlib::round' do
it { is_expected.to run.with_params(-34.5).and_return(-35) }
it { is_expected.to run.with_params(34.7).and_return(35) }
it { is_expected.to run.with_params(-34.7).and_return(-35) }
+ it { is_expected.to run.with_params("test").and_raise_error Puppet::ParseError }
+ it { is_expected.to run.with_params("test", "best").and_raise_error Puppet::ParseError }
+ it { is_expected.to run.with_params(3, 4).and_raise_error Puppet::ParseError }
end