add functionality to bool2str to return strings of your choice for a boolean
This commit is contained in:
parent
5b3c623394
commit
6de1a6e062
3 changed files with 44 additions and 4 deletions
|
@ -170,6 +170,22 @@ Converts a boolean to a number. Converts values:
|
||||||
* 'true', 't', '1', 'y', and 'yes' to 1.
|
* 'true', 't', '1', 'y', and 'yes' to 1.
|
||||||
Requires a single boolean or string as an input. *Type*: rvalue.
|
Requires a single boolean or string as an input. *Type*: rvalue.
|
||||||
|
|
||||||
|
#### `bool2str`
|
||||||
|
|
||||||
|
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 input. *Type*: rvalue.
|
||||||
|
|
||||||
#### `capitalize`
|
#### `capitalize`
|
||||||
|
|
||||||
Capitalizes the first letter of a string or array of strings. Requires either a single string or an array as an input. *Type*: rvalue.
|
Capitalizes the first letter of a string or array of strings. Requires either a single string or an array as an input. *Type*: rvalue.
|
||||||
|
|
|
@ -4,15 +4,29 @@
|
||||||
|
|
||||||
module Puppet::Parser::Functions
|
module Puppet::Parser::Functions
|
||||||
newfunction(:bool2str, :type => :rvalue, :doc => <<-EOS
|
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.
|
Requires a single boolean as an input.
|
||||||
EOS
|
EOS
|
||||||
) do |arguments|
|
) do |arguments|
|
||||||
|
|
||||||
raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " +
|
unless arguments.size == 1 or arguments.size == 3
|
||||||
"given (#{arguments.size} for 1)") if arguments.size < 1
|
raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " +
|
||||||
|
"given (#{arguments.size} for 3)")
|
||||||
|
end
|
||||||
|
|
||||||
value = arguments[0]
|
value = arguments[0]
|
||||||
|
true_string = arguments[1] || 'true'
|
||||||
|
false_string = arguments[2] || 'false'
|
||||||
klass = value.class
|
klass = value.class
|
||||||
|
|
||||||
# We can have either true or false, and nothing else
|
# 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')
|
raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with')
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,12 @@ describe 'bool2str' do
|
||||||
[ 'true', 'false', nil, :undef, ''].each do |invalid|
|
[ 'true', 'false', nil, :undef, ''].each do |invalid|
|
||||||
it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError) }
|
it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError) }
|
||||||
end
|
end
|
||||||
|
it { is_expected.to run.with_params(true, 'yes', 'no', 'maybe').and_raise_error(Puppet::ParseError) }
|
||||||
|
it { is_expected.to run.with_params(true, 'maybe').and_raise_error(Puppet::ParseError) }
|
||||||
|
it { is_expected.to run.with_params(true, 0, 1).and_raise_error(Puppet::ParseError) }
|
||||||
it { is_expected.to run.with_params(true).and_return("true") }
|
it { is_expected.to run.with_params(true).and_return("true") }
|
||||||
it { is_expected.to run.with_params(false).and_return("false") }
|
it { is_expected.to run.with_params(false).and_return("false") }
|
||||||
|
it { is_expected.to run.with_params(true, 'yes', 'no').and_return("yes") }
|
||||||
|
it { is_expected.to run.with_params(false, 'yes', 'no').and_return("no") }
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue