Skip to content

Commit c372f17

Browse files
committed
Cleanup per adrianthebo suggestions
* use Float() to process string arguments * get rid of doubly nested arrays * removing needless ternary operator * improving error message handling
1 parent 8d217f0 commit c372f17

File tree

2 files changed

+19
-39
lines changed

2 files changed

+19
-39
lines changed

lib/puppet/parser/functions/num2bool.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,24 @@ module Puppet::Parser::Functions
1919
when Numeric
2020
# Yay, it's a number
2121
when String
22-
# Deal with strings later
22+
begin
23+
number = Float(number)
24+
rescue ArgumentError => ex
25+
raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}")
26+
end
2327
else
2428
begin
2529
number = number.to_s
26-
rescue NoMethodError
27-
raise(Puppet::ParseError, 'num2bool(): Unable to parse argument: ' + $!)
28-
end
29-
end
30-
31-
case number
32-
when String
33-
# Only accept strings that look somewhat like numbers
34-
unless number =~ /^-?\d+/
35-
raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number")
30+
rescue NoMethodError => ex
31+
raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}")
3632
end
3733
end
3834

39-
# Truncate floats
35+
# Truncate Floats
4036
number = number.to_i
4137

4238
# Return true for any positive number and false otherwise
43-
return number > 0 ? true : false
39+
return number > 0
4440
end
4541
end
4642

spec/unit/puppet/parser/functions/num2bool_spec.rb

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,13 @@
2525
result.should(be_true)
2626
end
2727

28-
it "should return true if passed number 1" do
29-
result = scope.function_num2bool([1])
30-
result.should(be_true)
31-
end
32-
33-
it "should return true if passed array with string 1" do
34-
result = scope.function_num2bool([["1"]])
28+
it "should return true if passed string 1.5" do
29+
result = scope.function_num2bool(["1.5"])
3530
result.should(be_true)
3631
end
3732

38-
it "should return true if passed array with number 1" do
39-
result = scope.function_num2bool([[1]])
33+
it "should return true if passed number 1" do
34+
result = scope.function_num2bool([1])
4035
result.should(be_true)
4136
end
4237

@@ -50,34 +45,23 @@
5045
result.should(be_false)
5146
end
5247

53-
it "should return false if passed array with string 0" do
54-
result = scope.function_num2bool([["0"]])
55-
result.should(be_false)
56-
end
57-
58-
it "should return false if passed array with number 0" do
59-
result = scope.function_num2bool([[0]])
60-
result.should(be_false)
61-
end
62-
6348
it "should return false if passed string -1" do
6449
result = scope.function_num2bool(["-1"])
6550
result.should(be_false)
6651
end
6752

68-
it "should return false if passed number -1" do
69-
result = scope.function_num2bool([-1])
53+
it "should return false if passed string -1.5" do
54+
result = scope.function_num2bool(["-1.5"])
7055
result.should(be_false)
7156
end
7257

73-
it "should return false if passed array with string -1" do
74-
result = scope.function_num2bool([["-1"]])
58+
it "should return false if passed number -1" do
59+
result = scope.function_num2bool([-1])
7560
result.should(be_false)
7661
end
7762

78-
it "should return false if passed array with number -1" do
79-
result = scope.function_num2bool([[-1]])
63+
it "should return false if passed float -1.5" do
64+
result = scope.function_num2bool([-1.5])
8065
result.should(be_false)
8166
end
82-
8367
end

0 commit comments

Comments
 (0)