summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Huff <shuff@vecna.org>2013-04-01 11:44:09 -0400
committerSteve Huff <shuff@vecna.org>2013-04-01 11:44:09 -0400
commitc372f177708df4c844337e9901646b7b84b86cd8 (patch)
treea946ee1edb5862430106b35150d82aef3a61d8fe
parent8d217f0012fef332642faf485ad187773a95bcc1 (diff)
Cleanup per adrianthebo suggestions
* use Float() to process string arguments * get rid of doubly nested arrays * removing needless ternary operator * improving error message handling
-rw-r--r--lib/puppet/parser/functions/num2bool.rb22
-rw-r--r--spec/unit/puppet/parser/functions/num2bool_spec.rb36
2 files changed, 19 insertions, 39 deletions
diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb
index cf98f80..af0e6ed 100644
--- a/lib/puppet/parser/functions/num2bool.rb
+++ b/lib/puppet/parser/functions/num2bool.rb
@@ -19,28 +19,24 @@ become true.
when Numeric
# Yay, it's a number
when String
- # Deal with strings later
+ begin
+ number = Float(number)
+ rescue ArgumentError => ex
+ raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}")
+ end
else
begin
number = number.to_s
- rescue NoMethodError
- raise(Puppet::ParseError, 'num2bool(): Unable to parse argument: ' + $!)
- end
- end
-
- case number
- when String
- # Only accept strings that look somewhat like numbers
- unless number =~ /^-?\d+/
- raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number")
+ rescue NoMethodError => ex
+ raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}")
end
end
- # Truncate floats
+ # Truncate Floats
number = number.to_i
# Return true for any positive number and false otherwise
- return number > 0 ? true : false
+ return number > 0
end
end
diff --git a/spec/unit/puppet/parser/functions/num2bool_spec.rb b/spec/unit/puppet/parser/functions/num2bool_spec.rb
index 038881f..b56196d 100644
--- a/spec/unit/puppet/parser/functions/num2bool_spec.rb
+++ b/spec/unit/puppet/parser/functions/num2bool_spec.rb
@@ -25,18 +25,13 @@ describe "the num2bool function" do
result.should(be_true)
end
- it "should return true if passed number 1" do
- result = scope.function_num2bool([1])
- result.should(be_true)
- end
-
- it "should return true if passed array with string 1" do
- result = scope.function_num2bool([["1"]])
+ it "should return true if passed string 1.5" do
+ result = scope.function_num2bool(["1.5"])
result.should(be_true)
end
- it "should return true if passed array with number 1" do
- result = scope.function_num2bool([[1]])
+ it "should return true if passed number 1" do
+ result = scope.function_num2bool([1])
result.should(be_true)
end
@@ -50,34 +45,23 @@ describe "the num2bool function" do
result.should(be_false)
end
- it "should return false if passed array with string 0" do
- result = scope.function_num2bool([["0"]])
- result.should(be_false)
- end
-
- it "should return false if passed array with number 0" do
- result = scope.function_num2bool([[0]])
- result.should(be_false)
- end
-
it "should return false if passed string -1" do
result = scope.function_num2bool(["-1"])
result.should(be_false)
end
- it "should return false if passed number -1" do
- result = scope.function_num2bool([-1])
+ it "should return false if passed string -1.5" do
+ result = scope.function_num2bool(["-1.5"])
result.should(be_false)
end
- it "should return false if passed array with string -1" do
- result = scope.function_num2bool([["-1"]])
+ it "should return false if passed number -1" do
+ result = scope.function_num2bool([-1])
result.should(be_false)
end
- it "should return false if passed array with number -1" do
- result = scope.function_num2bool([[-1]])
+ it "should return false if passed float -1.5" do
+ result = scope.function_num2bool([-1.5])
result.should(be_false)
end
-
end