summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions
diff options
context:
space:
mode:
authorDavid Schmitt <david.schmitt@puppetlabs.com>2015-10-16 09:31:01 +0100
committerDavid Schmitt <david.schmitt@puppetlabs.com>2015-10-16 09:31:01 +0100
commit2db7440c6798d3ea0bf2f92c66d8281b2bfcff0c (patch)
treebddff86c5d1fc3708583c40c5c79099177bacd16 /lib/puppet/parser/functions
parent39126a7bc82797799280231fef7ef9706a113c6c (diff)
parent6de1a6e0622f69ec22c64e72fd53ec12ae8c9111 (diff)
downloadpuppet-stdlib-2db7440c6798d3ea0bf2f92c66d8281b2bfcff0c.tar.gz
puppet-stdlib-2db7440c6798d3ea0bf2f92c66d8281b2bfcff0c.tar.bz2
Merge pull request #538 from mmckinst/bool2str_enhance
add functionality to bool2str function
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r--lib/puppet/parser/functions/bool2str.rb26
1 files changed, 22 insertions, 4 deletions
diff --git a/lib/puppet/parser/functions/bool2str.rb b/lib/puppet/parser/functions/bool2str.rb
index fcd3791..7e36474 100644
--- a/lib/puppet/parser/functions/bool2str.rb
+++ b/lib/puppet/parser/functions/bool2str.rb
@@ -4,15 +4,29 @@
module Puppet::Parser::Functions
newfunction(:bool2str, :type => :rvalue, :doc => <<-EOS
- Converts a boolean to a string.
+ Converts a boolean to a string using optionally supplied arguments. The
+ optional second and third arguments represent what true and false will be
+ converted to respectively. If only one argument is given, it will be
+ converted from a boolean to a string containing 'true' or 'false'.
+
+ *Examples:*
+
+ bool2str(true) => 'true'
+ bool2str(true, 'yes', 'no') => 'yes'
+ bool2str(false, 't', 'f') => 'f'
+
Requires a single boolean as an input.
EOS
) do |arguments|
- raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " +
- "given (#{arguments.size} for 1)") if arguments.size < 1
+ unless arguments.size == 1 or arguments.size == 3
+ raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " +
+ "given (#{arguments.size} for 3)")
+ end
value = arguments[0]
+ true_string = arguments[1] || 'true'
+ false_string = arguments[2] || 'false'
klass = value.class
# We can have either true or false, and nothing else
@@ -20,7 +34,11 @@ module Puppet::Parser::Functions
raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with')
end
- return value.to_s
+ unless [true_string, false_string].all?{|x| x.kind_of?(String)}
+ raise(Puppet::ParseError, "bool2str(): Requires strings to convert to" )
+ end
+
+ return value ? true_string : false_string
end
end