# frozen_string_literal: true
# @summary
# Validate the passed value represents a numeric value.
Puppet::Functions.create_function(:validate_numeric) do Puppet::Functions.create_function(:validate_numeric) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return [Boolean] `true` or `false`
# A boolean value returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Perform validation of a string against one or more regular
# expressions.
#
Puppet::Functions.create_function(:validate_re) do Puppet::Functions.create_function(:validate_re) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
# The first argument of this function should be a string to
# test, and the second argument should be a stringified regular expression
# (without the // delimiters) or an array of regular expressions
#
# @return [Boolean]
# `true` or `false` returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# Validate that a passed string has length less/equal with the passed value
Puppet::Functions.create_function(:validate_slength) do Puppet::Functions.create_function(:validate_slength) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return [Boolean] `true` or `false`
# A boolean value returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Validate that all passed values are string data structures.
Puppet::Functions.create_function(:validate_string) do Puppet::Functions.create_function(:validate_string) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return [Boolean] `true` or `false`
# A boolean value returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# #
# abs.rb # abs.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:abs, type: :rvalue, doc: <<-DOC newfunction(:abs, :type => :rvalue, :doc => <<-DOC
@summary Returns the absolute value of a number, for example -34.56 becomes
**Deprecated:** Returns the absolute value of a number 34.56. Takes a single integer and float value as an argument.
For example -34.56 becomes 34.56.
Takes a single integer or float value as an argument.
> *Note:*
**Deprected** from Puppet 6.0.0, the built-in
['abs'](https://puppet.com/docs/puppet/6.4/function.html#abs)function will be used instead.
@return The absolute value of the given number if it was an Integer
Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
will be used instead of this function.
DOC DOC
) do |arguments| ) do |arguments|
raise(Puppet::ParseError, "abs(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? raise(Puppet::ParseError, "abs(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
value = arguments[0] value = arguments[0]
# Numbers in Puppet are often string-encoded which is troublesome ... # Numbers in Puppet are often string-encoded which is troublesome ...
if value.is_a?(String) if value.is_a?(String)
if %r{^-?(?:\d+)(?:\.\d+){1}$}.match?(value) if value =~ %r{^-?(?:\d+)(?:\.\d+){1}$}
value = value.to_f value = value.to_f
elsif %r{^-?\d+$}.match?(value) elsif value =~ %r{^-?\d+$}
value = value.to_i value = value.to_i
else else
raise(Puppet::ParseError, 'abs(): Requires float or integer to work with') raise(Puppet::ParseError, 'abs(): Requires float or integer to work with')
......
# frozen_string_literal: true
# #
# any2array.rb # any2array.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:any2array, type: :rvalue, doc: <<-DOC newfunction(:any2array, :type => :rvalue, :doc => <<-DOC
@summary This converts any object to an array containing that object. Empty argument
This converts any object to an array containing that object. lists are converted to an empty array. Arrays are left untouched. Hashes are
converted to arrays of alternating keys and values.
Empty argument lists are converted to an empty array. Arrays are left
untouched. Hashes are converted to arrays of alternating keys and values.
> *Note:* Note that since Puppet 5.0.0 it is possible to create new data types for almost any
since Puppet 5.0.0 it is possible to create new data types for almost any datatype using the type system and the built-in
datatype using the type system and the built-in [`Array.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-array-and-tuple)
[`Array.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-array-and-tuple) function is used to create a new Array..
function is used to create a new Array..
``` $hsh = {'key' => 42, 'another-key' => 100}
$hsh = {'key' => 42, 'another-key' => 100} notice(Array($hsh))
notice(Array($hsh))
```
Would notice `[['key', 42], ['another-key', 100]]` Would notice `[['key', 42], ['another-key', 100]]`
The Array data type also has a special mode to "create an array if not already an array" The Array data type also has a special mode to "create an array if not already an array"
``` notice(Array({'key' => 42, 'another-key' => 100}, true))
notice(Array({'key' => 42, 'another-key' => 100}, true))
```
Would notice `[{'key' => 42, 'another-key' => 100}]`, as the `true` flag prevents the hash from being Would notice `[{'key' => 42, 'another-key' => 100}]`, as the `true` flag prevents the hash from being
transformed into an array. transformed into an array.
@return [Array] The new array containing the given object
DOC DOC
) do |arguments| ) do |arguments|
if arguments.empty? if arguments.empty?
return [] return []
end end
......
# frozen_string_literal: true
# #
# any2bool.rb # any2bool.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:any2bool, type: :rvalue, doc: <<-DOC newfunction(:any2bool, :type => :rvalue, :doc => <<-DOC
@summary This converts 'anything' to a boolean. In practise it does the following:
Converts 'anything' to a boolean.
In practise it does the following:
* Strings such as Y,y,1,T,t,TRUE,yes,'true' will return true * Strings such as Y,y,1,T,t,TRUE,yes,'true' will return true
* Strings such as 0,F,f,N,n,FALSE,no,'false' will return false * Strings such as 0,F,f,N,n,FALSE,no,'false' will return false
* Booleans will just return their original value * Booleans will just return their original value
...@@ -18,10 +14,9 @@ module Puppet::Parser::Functions ...@@ -18,10 +14,9 @@ module Puppet::Parser::Functions
Also see the built-in [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean) Also see the built-in [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
function. function.
@return [Boolean] The boolean value of the object that was given
DOC DOC
) do |arguments| ) do |arguments|
raise(Puppet::ParseError, "any2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? raise(Puppet::ParseError, "any2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
# If argument is already Boolean, return it # If argument is already Boolean, return it
......
# frozen_string_literal: true
# #
# assert_private.rb # assert_private.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:assert_private, doc: <<-DOC newfunction(:assert_private, :doc => <<-DOC
@summary Sets the current class or definition as private.
Sets the current class or definition as private.
@return
set the current class or definition as private.
Calling the class or definition from outside the current module will fail. Calling the class or definition from outside the current module will fail.
DOC DOC
) do |args| ) do |args|
raise(Puppet::ParseError, "assert_private(): Wrong number of arguments given (#{args.size}}) for 0 or 1)") if args.size > 1 raise(Puppet::ParseError, "assert_private(): Wrong number of arguments given (#{args.size}}) for 0 or 1)") if args.size > 1
scope = self scope = self
if scope.lookupvar('module_name') != scope.lookupvar('caller_module_name') if scope.lookupvar('module_name') != scope.lookupvar('caller_module_name')
message = nil message = nil
if args[0]&.is_a?(String) if args[0] && args[0].is_a?(String)
message = args[0] message = args[0]
else else
manifest_name = scope.source.name manifest_name = scope.source.name
......
# frozen_string_literal: true
# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:base64, type: :rvalue, doc: <<-DOC) do |args| newfunction(:base64, :type => :rvalue, :doc => <<-'DOC') do |args|
@summary Base64 encode or decode a string based on the command and the string submitted
Base64 encode or decode a string based on the command and the string submitted
@example Example usage
Encode and decode a string
$encodestring = base64('encode', 'thestring') Usage:
$decodestring = base64('decode', 'dGhlc3RyaW5n')
Explicitly define encode/decode method: default, strict, urlsafe $encodestring = base64('encode', 'thestring')
$decodestring = base64('decode', 'dGhlc3RyaW5n')
$method = 'default' # explicitly define encode/decode method: default, strict, urlsafe
$encodestring = base64('encode', 'thestring', $method) $method = 'default'
$decodestring = base64('decode', 'dGhlc3RyaW5n', $method) $encodestring = base64('encode', 'thestring', $method)
$decodestring = base64('decode', 'dGhlc3RyaW5n', $method)
Encode a string as if it was binary Note: Since Puppet 4.8.0, the Binary data type can be used to produce base 64 encoded strings.
See the `new()` function for the Binary and String types for documentation. Also see `binary_file()`
function for reading a file with binary (non UTF-8) content.
# encode a string as if it was binary
$encodestring = String(Binary('thestring', '%s')) $encodestring = String(Binary('thestring', '%s'))
# decode a Binary assuming it is an UTF-8 String
Decode a Binary assuming it is an UTF-8 String
$decodestring = String(Binary("dGhlc3RyaW5n"), "%s") $decodestring = String(Binary("dGhlc3RyaW5n"), "%s")
> **Note:*
Since Puppet 4.8.0, the Binary data type can be used to produce base 64 encoded strings.
See the `new()` function for the Binary and String types for documentation. Also see `binary_file()`
function for reading a file with binary (non UTF-8) content.
@return [String] The encoded/decoded value
DOC DOC
require 'base64' require 'base64'
......
# frozen_string_literal: true
# #
# basename.rb # basename.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:basename, type: :rvalue, doc: <<-DOC newfunction(:basename, :type => :rvalue, :doc => <<-DOC
@summary Strips directory (and optional suffix) from a filename
Strips directory (and optional suffix) from a filename
@return [String] The stripped filename
DOC DOC
) do |arguments| ) do |arguments|
raise(Puppet::ParseError, 'basename(): No arguments given') if arguments.empty? raise(Puppet::ParseError, 'basename(): No arguments given') if arguments.empty?
raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})") if arguments.size > 2 raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})") if arguments.size > 2
raise(Puppet::ParseError, 'basename(): Requires string as first argument') unless arguments[0].is_a?(String) raise(Puppet::ParseError, 'basename(): Requires string as first argument') unless arguments[0].is_a?(String)
......
# frozen_string_literal: true
# #
# bool2num.rb # bool2num.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:bool2num, type: :rvalue, doc: <<-DOC newfunction(:bool2num, :type => :rvalue, :doc => <<-DOC
@summary Converts a boolean to a number. Converts the values:
Converts a boolean to a number.
Converts the values:
```
false, f, 0, n, and no to 0 false, f, 0, n, and no to 0
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. Requires a single boolean or string as an input.
> *Note:* Note that since Puppet 5.0.0 it is possible to create new data types for almost any
since Puppet 5.0.0 it is possible to create new data types for almost any datatype using the type system and the built-in
datatype using the type system and the built-in [`Numeric.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-numeric),
[`Numeric.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-numeric), [`Integer.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-integer), and
[`Integer.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-integer), and [`Float.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-float)
[`Float.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-float) function are used to convert to numeric values.
function are used to convert to numeric values.
```
notice(Integer(false)) # Notices 0
notice(Float(true)) # Notices 1.0
```
@return [Integer] The converted value as a number notice(Integer(false)) # Notices 0
notice(Float(true)) # Notices 1.0
DOC DOC
) do |arguments| ) do |arguments|
raise(Puppet::ParseError, "bool2num(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? raise(Puppet::ParseError, "bool2num(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
value = function_str2bool([arguments[0]]) value = function_str2bool([arguments[0]])
......
# frozen_string_literal: true
# #
# bool2str.rb # bool2str.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:bool2str, type: :rvalue, doc: <<-DOC newfunction(:bool2str, :type => :rvalue, :doc => <<-DOC
@summary Converts a boolean to a string using optionally supplied arguments. The
Converts a boolean to a string using optionally supplied arguments. optional second and third arguments represent what true and false will be
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 to respectively. If only one argument is given, it will be
converted from a boolean to a string containing 'true' or 'false'. converted from a boolean to a string containing 'true' or 'false'.
@return *Examples:*
The converted value to string of the given Boolean
**Examples of usage**
``` bool2str(true) => 'true'
bool2str(true) => 'true' bool2str(true, 'yes', 'no') => 'yes'
bool2str(true, 'yes', 'no') => 'yes' bool2str(false, 't', 'f') => 'f'
bool2str(false, 't', 'f') => 'f'
```
Requires a single boolean as an input. Requires a single boolean as an input.
> *Note:* Note that since Puppet 5.0.0 it is possible to create new data types for almost any
since Puppet 5.0.0 it is possible to create new data types for almost any datatype using the type system and the built-in
datatype using the type system and the built-in [`String.new`](https://puppet.com/docs/puppet/latest/function.html#boolean-to-string)
[`String.new`](https://puppet.com/docs/puppet/latest/function.html#boolean-to-string) function is used to convert to String with many different format options.
function is used to convert to String with many different format options.
```
notice(String(false)) # Notices 'false' notice(String(false)) # Notices 'false'
notice(String(true)) # Notices 'true' notice(String(true)) # Notices 'true'
notice(String(false, '%y')) # Notices 'yes' notice(String(false, '%y')) # Notices 'yes'
notice(String(true, '%y')) # Notices 'no' notice(String(true, '%y')) # Notices 'no'
```
DOC DOC
) do |arguments| ) do |arguments|
unless arguments.size == 1 || arguments.size == 3 unless arguments.size == 1 || arguments.size == 3
raise(Puppet::ParseError, "bool2str(): Wrong number of arguments given (#{arguments.size} for 3)") raise(Puppet::ParseError, "bool2str(): Wrong number of arguments given (#{arguments.size} for 3)")
end end
......
# frozen_string_literal: true
# #
# camelcase.rb # camelcase.rb
# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:camelcase, type: :rvalue, doc: <<-DOC newfunction(:camelcase, :type => :rvalue, :doc => <<-DOC
@summary Converts the case of a string or all strings in an array to camel case.
**Deprecated** Converts the case of a string or all strings in an array to camel case.
> *Note:*
**Deprecated** from Puppet 6.0.0, this function has been replaced with
a built-in [`camelcase`](https://puppet.com/docs/puppet/latest/function.html#camelcase)
function.
@return [String] The converted String, if it was a String that was given Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
@return [Array[String]] The converted Array, if it was a Array that was given will be used instead of this function.
DOC DOC
) do |arguments| ) do |arguments|
raise(Puppet::ParseError, "camelcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? raise(Puppet::ParseError, "camelcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
value = arguments[0] value = arguments[0]
......
# frozen_string_literal: true
# #
# capitalize.rb # capitalize.rb
# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:capitalize, type: :rvalue, doc: <<-DOC newfunction(:capitalize, :type => :rvalue, :doc => <<-DOC
@summary Capitalizes the first letter of a string or array of strings.
**Deprecated** Capitalizes the first letter of a string or array of strings.
Requires either a single string or an array as an input. Requires either a single string or an array as an input.
> *Note:* Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
**Deprecated** from Puppet 6.0.0, yhis function has been replaced with a will be used instead of this function.
built-in [`capitalize`](https://puppet.com/docs/puppet/latest/function.html#capitalize)
function.
@return [String] The converted String, if it was a String that was given
@return [Array[String]] The converted Array, if it was a Array that was given
DOC DOC
) do |arguments| ) do |arguments|
raise(Puppet::ParseError, "capitalize(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? raise(Puppet::ParseError, "capitalize(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
value = arguments[0] value = arguments[0]
......
# frozen_string_literal: true
# #
# ceiling.rb # ceiling.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:ceiling, type: :rvalue, doc: <<-DOC newfunction(:ceiling, :type => :rvalue, :doc => <<-DOC
@summary Returns the smallest integer greater or equal to the argument.
**Deprecated** Returns the smallest integer greater or equal to the argument.
Takes a single numeric value as an argument. Takes a single numeric value as an argument.
> *Note:* Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
**Deprecated** from Puppet 6.0.0, this function has been replaced with a will be used instead of this function.
built-in [`ceiling`](https://puppet.com/docs/puppet/latest/function.html#ceiling) function.
@return [Integer] The rounded value
DOC DOC
) do |arguments| ) do |arguments|
raise(Puppet::ParseError, "ceiling(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 raise(Puppet::ParseError, "ceiling(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1
begin begin
......
# frozen_string_literal: true
# #
# chomp.rb # chomp.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:chomp, type: :rvalue, doc: <<-DOC newfunction(:chomp, :type => :rvalue, :doc => <<-DOC
@summary Removes the record separator from the end of a string or an array of
**Deprecated** Removes the record separator from the end of a string or an array of strings. strings, for example `hello\n` becomes `hello`.
For example `hello\n` becomes `hello`.
Requires a single string or array as an input. Requires a single string or array as an input.
> *Note:* Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
**Deprecated** from Puppet 6.0.0, this function has been replaced with a will be used instead of this function.
built-in [`chomp`](https://puppet.com/docs/puppet/latest/function.html#chomp) function.
@return [String] The converted String, if it was a String that was given
@return [Array[String]] The converted Array, if it was a Array that was given
DOC DOC
) do |arguments| ) do |arguments|
raise(Puppet::ParseError, "chomp(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? raise(Puppet::ParseError, "chomp(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
value = arguments[0] value = arguments[0]
......
# frozen_string_literal: true
# #
# chop.rb # chop.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:chop, type: :rvalue, doc: <<-DOC newfunction(:chop, :type => :rvalue, :doc => <<-DOC
@summary Returns a new string with the last character removed. If the string ends
**Deprecated** Returns a new string with the last character removed. with `\r\n`, both characters are removed. Applying chop to an empty
string returns an empty string. If you wish to merely remove record
If the string ends with `\r\n`, both characters are removed. Applying separators then you should use the `chomp` function.
chop to an empty string returns an empty string. If you wish to merely
remove record separators then you should use the `chomp` function.
Requires a string or array of strings as input. Requires a string or array of strings as input.
> *Note:* **Deprecated** from Puppet 6.0.0, this function has been replaced with a Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
built-in [`chop`](https://puppet.com/docs/puppet/latest/function.html#chop) function. will be used instead of this function.
@return [String] The given String, sans the last character.
DOC DOC
) do |arguments| ) do |arguments|
raise(Puppet::ParseError, "chop(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? raise(Puppet::ParseError, "chop(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
value = arguments[0] value = arguments[0]
......
# frozen_string_literal: true
# #
# clamp.rb # clamp.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:clamp, type: :rvalue, arity: -2, doc: <<-DOC newfunction(:clamp, :type => :rvalue, :arity => -2, :doc => <<-DOC
@summary Clamps value to a range.
Keeps value within the range [Min, X, Max] by sort based on integer value
(parameter order doesn't matter).
Strings are converted and compared numerically. Arrays of values are flattened
into a list for further handling.
@example Example usage
clamp('24', [575, 187])` returns 187. Note: From Puppet 6.0.0 this can be done with only core Puppet like this:
clamp(16, 88, 661)` returns 88. [$minval, $maxval, $value_to_clamp].sort[1]
clamp([4, 3, '99'])` returns 4.
> *Note:*
From Puppet 6.0.0 this can be done with only core Puppet like this:
`[$minval, $maxval, $value_to_clamp].sort[1]`
@return [Array[Integer]] The sorted Array
DOC DOC
) do |args| ) do |args|
args.flatten! args.flatten!
raise(Puppet::ParseError, 'clamp(): Wrong number of arguments, need three to clamp') if args.size != 3 raise(Puppet::ParseError, 'clamp(): Wrong number of arguments, need three to clamp') if args.size != 3
...@@ -33,7 +18,7 @@ module Puppet::Parser::Functions ...@@ -33,7 +18,7 @@ module Puppet::Parser::Functions
args.each do |value| args.each do |value|
case [value.class] case [value.class]
when [String] when [String]
raise(Puppet::ParseError, "clamp(): Required explicit numeric (#{value}:String)") unless %r{^\d+$}.match?(value) raise(Puppet::ParseError, "clamp(): Required explicit numeric (#{value}:String)") unless value =~ %r{^\d+$}
when [Hash] when [Hash]
raise(Puppet::ParseError, "clamp(): The Hash type is not allowed (#{value})") raise(Puppet::ParseError, "clamp(): The Hash type is not allowed (#{value})")
end end
......
# frozen_string_literal: true
# #
# concat.rb # concat.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:concat, type: :rvalue, doc: <<-DOC newfunction(:concat, :type => :rvalue, :doc => <<-DOC
@summary Appends the contents of multiple arrays into array 1.
Appends the contents of multiple arrays into array 1.
*Example:*
@example Example usage concat(['1','2','3'],['4','5','6'],['7','8','9'])
concat(['1','2','3'],'4') returns ['1','2','3','4'] Would result in:
concat(['1','2','3'],'4',['5','6','7']) returns ['1','2','3','4','5','6','7']
> *Note:* ['1','2','3','4','5','6','7','8','9']
Since Puppet 4.0, you can use the `+`` operator for concatenation of arrays and
merge of hashes, and the `<<`` operator for appending:
`['1','2','3'] + ['4','5','6'] + ['7','8','9']` returns `['1','2','3','4','5','6','7','8','9']` Note: Since Puppet 4.0 concatenation of arrays and hashes can be done with the + operator.
`[1, 2, 3] << 4` returns `[1, 2, 3, 4]`
`[1, 2, 3] << [4, 5]` returns `[1, 2, 3, [4, 5]]`
@return [Array] The single concatenated array ['1','2','3'] + ['4','5','6'] + ['7','8','9']
DOC DOC
) do |arguments| ) do |arguments|
# Check that more than 2 arguments have been given ... # Check that more than 2 arguments have been given ...
raise(Puppet::ParseError, "concat(): Wrong number of arguments given (#{arguments.size} for < 2)") if arguments.size < 2 raise(Puppet::ParseError, "concat(): Wrong number of arguments given (#{arguments.size} for < 2)") if arguments.size < 2
......
# frozen_string_literal: true
# #
# convert_base.rb # convert_base.rb
# #
module Puppet::Parser::Functions module Puppet::Parser::Functions
newfunction(:convert_base, type: :rvalue, arity: 2, doc: <<-'DOC') do |args| newfunction(:convert_base, :type => :rvalue, :arity => 2, :doc => <<-'DOC') do |args|
@summary Converts a given integer or base 10 string representing an integer to a specified base, as a string.
Converts a given integer or base 10 string representing an integer to a
specified base, as a string.
@return
converted value as a string
@example Example usage
convert_base(5, 2)` results in: `'101'` Usage:
convert_base('254', '16')` results in: `'fe'`
> *Note:* $binary_repr = convert_base(5, 2) # $binary_repr is now set to "101"
Since Puppet 4.5.0 this can be done with the built-in $hex_repr = convert_base("254", "16") # $hex_repr is now set to "fe"
[`String.new`](https://puppet.com/docs/puppet/latest/function.html#integer-to-string)
function and its many formatting options:
`$binary_repr = String(5, '%b')` return `"101"` Note: Since Puppet 4.5.0 this can be done with String.new() and its many formatting options:
`$hex_repr = String(254, "%x")` return `"fe"`
`$hex_repr = String(254, "%#x")` return `"0xfe"`
@return [String] The converted value as a String $binary_repr = String(5, '%b') # results in "101"
$hex_repr = String(254, "%x") # results in "fe"
$hex_repr = String(254, "%#x") # results in "0xfe"
DOC DOC
raise Puppet::ParseError, 'convert_base(): First argument must be either a string or an integer' unless args[0].is_a?(Integer) || args[0].is_a?(String) raise Puppet::ParseError, 'convert_base(): First argument must be either a string or an integer' unless args[0].is_a?(Integer) || args[0].is_a?(String)
raise Puppet::ParseError, 'convert_base(): Second argument must be either a string or an integer' unless args[1].is_a?(Integer) || args[1].is_a?(String) raise Puppet::ParseError, 'convert_base(): Second argument must be either a string or an integer' unless args[1].is_a?(Integer) || args[1].is_a?(String)
if args[0].is_a?(String) if args[0].is_a?(String)
raise Puppet::ParseError, 'convert_base(): First argument must be an integer or a string corresponding to an integer in base 10' unless %r{^[0-9]+$}.match?(args[0]) raise Puppet::ParseError, 'convert_base(): First argument must be an integer or a string corresponding to an integer in base 10' unless args[0] =~ %r{^[0-9]+$}
end end
if args[1].is_a?(String) if args[1].is_a?(String)
raise Puppet::ParseError, 'convert_base(): First argument must be an integer or a string corresponding to an integer in base 10' unless %r{^[0-9]+$}.match?(args[1]) raise Puppet::ParseError, 'convert_base(): First argument must be an integer or a string corresponding to an integer in base 10' unless args[1] =~ %r{^[0-9]+$}
end end
number_to_convert = args[0] number_to_convert = args[0]
......