diff --git a/lib/puppet/parser/functions/bool2onoff.rb b/lib/puppet/parser/functions/bool2onoff.rb new file mode 100644 index 0000000..e2b4e77 --- /dev/null +++ b/lib/puppet/parser/functions/bool2onoff.rb @@ -0,0 +1,27 @@ +#shamesly stolen from: https://github.com/puppetlabs/puppetlabs-apache/blob/master/lib/puppet/parser/functions/bool2httpd.rb +# with minor changes +# +# +#Copyright (C) 2012 Puppet Labs Inc +# +#Puppet Labs can be contacted at: info@puppetlabs.com +# +#Licensed under the Apache License, Version 2.0 (the "License"); +# +# +Puppet::Parser::Functions::newfunction(:bool2onoff, :type => :rvalue, :doc => <<-EOS +Transform a supposed boolean to On or Off. Other values through. +EOS +) do |args| + raise(Puppet::ParseError, "bool2onoff() wrong number of arguments. Given: #{args.size} for 1)") if args.size != 1 + + arg = args[0] + + if arg.nil? or arg == false or arg =~ /false/i or arg =~ /off/i or arg == :undef + return 'off' + elsif arg == true or arg =~ /true/i or arg =~ /on/i + return 'on' + end + + return arg.to_s +end diff --git a/lib/puppet/parser/functions/bool2yesno.rb b/lib/puppet/parser/functions/bool2yesno.rb new file mode 100644 index 0000000..778a7e0 --- /dev/null +++ b/lib/puppet/parser/functions/bool2yesno.rb @@ -0,0 +1,17 @@ +Puppet::Parser::Functions::newfunction(:bool2yesno, :type => :rvalue, :doc => <<-EOS +Transform a supposed boolean to yes or no. Pass all other values through. +Given a nil value (undef), bool2yesno will return 'no' +EOS +) do |args| + raise(Puppet::ParseError, "bool2yesno() wrong number of arguments. #{args.size} vs 1)") if args.size != 1 + + arg = args[0] + + if arg.nil? or arg == false or arg =~ /false/i or arg =~ /no/i or arg == :undef + return 'no' + elsif arg == true or arg =~ /true/i or arg =~ /yes/i + return 'yes' + end + + return arg.to_s +end