Skip to content

Commit 2db7440

Browse files
committed
Merge pull request #538 from mmckinst/bool2str_enhance
add functionality to bool2str function
2 parents 39126a7 + 6de1a6e commit 2db7440

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.markdown

+16
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,22 @@ Converts a boolean to a number. Converts values:
170170
* 'true', 't', '1', 'y', and 'yes' to 1.
171171
Requires a single boolean or string as an input. *Type*: rvalue.
172172

173+
#### `bool2str`
174+
175+
Converts a boolean to a string using optionally supplied arguments. The optional
176+
second and third arguments represent what true and false will be converted to
177+
respectively. If only one argument is given, it will be converted from a boolean
178+
to a string containing 'true' or 'false'.
179+
180+
*Examples:*
181+
~~~
182+
bool2str(true) => 'true'
183+
bool2str(true, 'yes', 'no') => 'yes'
184+
bool2str(false, 't', 'f') => 'f'
185+
~~~
186+
187+
Requires a single boolean as input. *Type*: rvalue.
188+
173189
#### `capitalize`
174190

175191
Capitalizes the first letter of a string or array of strings. Requires either a single string or an array as an input. *Type*: rvalue.

lib/puppet/parser/functions/bool2str.rb

+22-4
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,41 @@
44

55
module Puppet::Parser::Functions
66
newfunction(:bool2str, :type => :rvalue, :doc => <<-EOS
7-
Converts a boolean to a string.
7+
Converts a boolean to a string using optionally supplied arguments. The
8+
optional second and third arguments represent what true and false will be
9+
converted to respectively. If only one argument is given, it will be
10+
converted from a boolean to a string containing 'true' or 'false'.
11+
12+
*Examples:*
13+
14+
bool2str(true) => 'true'
15+
bool2str(true, 'yes', 'no') => 'yes'
16+
bool2str(false, 't', 'f') => 'f'
17+
818
Requires a single boolean as an input.
919
EOS
1020
) do |arguments|
1121

12-
raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " +
13-
"given (#{arguments.size} for 1)") if arguments.size < 1
22+
unless arguments.size == 1 or arguments.size == 3
23+
raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " +
24+
"given (#{arguments.size} for 3)")
25+
end
1426

1527
value = arguments[0]
28+
true_string = arguments[1] || 'true'
29+
false_string = arguments[2] || 'false'
1630
klass = value.class
1731

1832
# We can have either true or false, and nothing else
1933
unless [FalseClass, TrueClass].include?(klass)
2034
raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with')
2135
end
2236

23-
return value.to_s
37+
unless [true_string, false_string].all?{|x| x.kind_of?(String)}
38+
raise(Puppet::ParseError, "bool2str(): Requires strings to convert to" )
39+
end
40+
41+
return value ? true_string : false_string
2442
end
2543
end
2644

spec/functions/bool2str_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
[ 'true', 'false', nil, :undef, ''].each do |invalid|
77
it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError) }
88
end
9+
it { is_expected.to run.with_params(true, 'yes', 'no', 'maybe').and_raise_error(Puppet::ParseError) }
10+
it { is_expected.to run.with_params(true, 'maybe').and_raise_error(Puppet::ParseError) }
11+
it { is_expected.to run.with_params(true, 0, 1).and_raise_error(Puppet::ParseError) }
912
it { is_expected.to run.with_params(true).and_return("true") }
1013
it { is_expected.to run.with_params(false).and_return("false") }
14+
it { is_expected.to run.with_params(true, 'yes', 'no').and_return("yes") }
15+
it { is_expected.to run.with_params(false, 'yes', 'no').and_return("no") }
16+
1117
end

0 commit comments

Comments
 (0)