Skip to content

add functionality to bool2str function #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ Converts a boolean to a number. Converts values:
* 'true', 't', '1', 'y', and 'yes' to 1.
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`

Capitalizes the first letter of a string or array of strings. Requires either a single string or an array as an input. *Type*: rvalue.
Expand Down
26 changes: 22 additions & 4 deletions lib/puppet/parser/functions/bool2str.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,41 @@

module Puppet::Parser::Functions
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.
EOS
) do |arguments|

raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size < 1
unless arguments.size == 1 or arguments.size == 3
raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " +
"given (#{arguments.size} for 3)")
end

value = arguments[0]
true_string = arguments[1] || 'true'
false_string = arguments[2] || 'false'
klass = value.class

# We can have either true or false, and nothing else
unless [FalseClass, TrueClass].include?(klass)
raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with')
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

Expand Down
6 changes: 6 additions & 0 deletions spec/functions/bool2str_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
[ 'true', 'false', nil, :undef, ''].each do |invalid|
it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError) }
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(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