From 1c33c55aa2505f7cfbb73d0288cf720d2892cdc5 Mon Sep 17 00:00:00 2001 From: Jordi Prats Date: Mon, 7 Mar 2016 18:55:54 +0100 Subject: [PATCH] afegides funcions --- lib/puppet/parser/functions/bool2onoff.rb | 27 +++++++++++++++++++++++ lib/puppet/parser/functions/bool2yesno.rb | 17 ++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 lib/puppet/parser/functions/bool2onoff.rb create mode 100644 lib/puppet/parser/functions/bool2yesno.rb 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