Puppet Function: apache::bool2httpd

Defined in:
lib/puppet/functions/apache/bool2httpd.rb
Function type:
Ruby 4.x API

Summary

Transform a supposed boolean to On or Off. Passes all other values through.

Overview

apache::bool2httpd(Any $arg)Any

Examples:

$trace_enable     = false
$server_signature = 'mail'

apache::bool2httpd($trace_enable) # returns 'Off'
apache::bool2httpd($server_signature) # returns 'mail'
apache::bool2httpd(undef) # returns 'Off'

Parameters:

  • arg (Any)

    The value to be converted into a string.

Returns:

  • (Any)

    Will return either ‘On` or `Off` if given a boolean value. Returns a string of any other given value.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppet/functions/apache/bool2httpd.rb', line 6

Puppet::Functions.create_function(:'apache::bool2httpd') do
  # @param arg
  #   The value to be converted into a string.
  #
  # @return
  #   Will return either `On` or `Off` if given a boolean value. Returns a string of any
  #   other given value.
  # @example
  #   $trace_enable     = false
  #   $server_signature = 'mail'
  #
  #   apache::bool2httpd($trace_enable) # returns 'Off'
  #   apache::bool2httpd($server_signature) # returns 'mail'
  #   apache::bool2httpd(undef) # returns 'Off'
  #
  def bool2httpd(arg)
    return 'Off' if arg.nil? || arg == false || matches_string?(arg, %r{false}i) || arg == :undef
    return 'On' if arg == true || matches_string?(arg, %r{true}i)
    arg.to_s
  end

  private

  def matches_string?(value, matcher)
    if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.4.0')
      value =~ matcher
    else
      value.is_a?(String) && value.match?(matcher)
    end
  end
end