From 99a93d366f2e1efb977fcc8fe300d3d8357c8214 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Apr 2011 09:27:10 +0200 Subject: Convert to module format. --- lib/puppet/parser/functions/num2bool.rb | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/puppet/parser/functions/num2bool.rb (limited to 'lib/puppet/parser/functions/num2bool.rb') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb new file mode 100644 index 0000000..2baef62 --- /dev/null +++ b/lib/puppet/parser/functions/num2bool.rb @@ -0,0 +1,38 @@ +# +# num2bool.rb +# + +# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... + +module Puppet::Parser::Functions + newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + number = arguments[0] + + # Only numbers allowed ... + unless number.match(/^\-?\d+$/) + raise(Puppet::ParseError, 'num2bool(): Requires integer to work with') + end + + result = case number + when /^0$/ + false + when /^\-?\d+$/ + # Numbers in Puppet are often string-encoded which is troublesome ... + number = number.to_i + # We yield true for any positive number and false otherwise ... + number > 0 ? true : false + else + raise(Puppet::ParseError, 'num2bool(): Unknown numeric format given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 19313b43ea04066a88a0b78e83650ac52785e2e9 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 22:18:56 +0100 Subject: (#3) Apply missing documentation to more functions. --- lib/puppet/parser/functions/num2bool.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/puppet/parser/functions/num2bool.rb') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 2baef62..874db22 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -6,6 +6,8 @@ module Puppet::Parser::Functions newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS +This function converts a number into a true boolean. Zero becomes false. Numbers +higher then 0 become true. EOS ) do |arguments| -- cgit v1.2.3