Skip to content

Commit 366ccfc

Browse files
committed
enhance bool2str function
1 parent 5b3c623 commit 366ccfc

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.markdown

Lines changed: 16 additions & 0 deletions
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 an optionally supplied two
176+
element array of strings. The first and second element
177+
of the optional array represent what true and false will be converted
178+
to respectively.
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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,39 @@
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 an optionally supplied two element
8+
array of strings. The first and second element of the optional
9+
array represent what true and false will be converted to respectively.
10+
811
Requires a single boolean as an input.
912
EOS
1013
) do |arguments|
1114

1215
raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " +
13-
"given (#{arguments.size} for 1)") if arguments.size < 1
16+
"given (#{arguments.size} for 2)") if arguments.size > 2
1417

1518
value = arguments[0]
19+
conversion_names = arguments[1] || ['true', 'false']
1620
klass = value.class
1721

1822
# We can have either true or false, and nothing else
1923
unless [FalseClass, TrueClass].include?(klass)
2024
raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with')
2125
end
2226

23-
return value.to_s
27+
unless conversion_names.kind_of?(Array)
28+
raise(Puppet::ParseError, 'bool2str(): Requires an array to work with')
29+
end
30+
31+
unless conversion_names.count == 2
32+
raise(Puppet::ParseError, 'bool2str(): Requires a two element array to work with')
33+
end
34+
35+
unless conversion_names.all?{|name| name.kind_of?(String)}
36+
raise(Puppet::ParseError, "bool2str(): Requires strings to convert to" )
37+
end
38+
39+
return value ? conversion_names[0] : conversion_names[1]
2440
end
2541
end
2642

spec/functions/bool2str_spec.rb

Lines changed: 6 additions & 0 deletions
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, false, ['yes', 'no']).and_raise_error(Puppet::ParseError) }
10+
it { is_expected.to run.with_params(true, [0, 1]).and_raise_error(Puppet::ParseError) }
11+
it { is_expected.to run.with_params(true, ['yes', 'no', 'maybe']).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)