module Puppet::Parser::Functions
newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS
+This function converts a number into a true boolean. Zero becomes false. Numbers
+higher then 0 become true.
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:prefix, :type => :rvalue, :doc => <<-EOS
+This function applies a prefix to all elements in an array.
+
+*Examles:*
+
+ prefix(['a','b','c'], 'p')
+
+Will return: ['pa','pb','pc']
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:range, :type => :rvalue, :doc => <<-EOS
+When given range in the form of (start, stop) it will extrapolate a range as
+an array.
+
+*Examples:*
+
+ range("0", "9")
+
+Will return: [0,1,2,3,4,5,6,7,8,9]
+
+ range("a", "c")
+
+Will return: ["a","b","c"]
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:reverse, :type => :rvalue, :doc => <<-EOS
+Reverses the order of a string or array.
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS
+Strips leading spaces to the right of the string.
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS
+Randomizes the order of a string or array elements.
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:size, :type => :rvalue, :doc => <<-EOS
+Returns the number of elements in a string or array.
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:sort, :type => :rvalue, :doc => <<-EOS
+Sorts strings and arrays lexically.
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS
+Returns a new string where runs of the same character that occur in this set are replaced by a single character.
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS
+This converts a string to a boolean. This attempt to convert strings that
+contain things like: y, 1, t, true to 'true' and strings that contain things
+like: 0, f, n, false, no to 'false'.
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:strftime, :type => :rvalue, :doc => <<-EOS
+This function returns formatted time.
+
+*Examples:*
+
+To return the time since epoch:
+
+ strftime("%s")
+
+To return the date:
+
+ strftime("%Y-%m-%d")
+
+*Format meaning:*
+
+ %a - The abbreviated weekday name (``Sun'')
+ %A - The full weekday name (``Sunday'')
+ %b - The abbreviated month name (``Jan'')
+ %B - The full month name (``January'')
+ %c - The preferred local date and time representation
+ %C - Century (20 in 2009)
+ %d - Day of the month (01..31)
+ %D - Date (%m/%d/%y)
+ %e - Day of the month, blank-padded ( 1..31)
+ %F - Equivalent to %Y-%m-%d (the ISO 8601 date format)
+ %h - Equivalent to %b
+ %H - Hour of the day, 24-hour clock (00..23)
+ %I - Hour of the day, 12-hour clock (01..12)
+ %j - Day of the year (001..366)
+ %k - hour, 24-hour clock, blank-padded ( 0..23)
+ %l - hour, 12-hour clock, blank-padded ( 0..12)
+ %L - Millisecond of the second (000..999)
+ %m - Month of the year (01..12)
+ %M - Minute of the hour (00..59)
+ %n - Newline (\n)
+ %N - Fractional seconds digits, default is 9 digits (nanosecond)
+ %3N millisecond (3 digits)
+ %6N microsecond (6 digits)
+ %9N nanosecond (9 digits)
+ %p - Meridian indicator (``AM'' or ``PM'')
+ %P - Meridian indicator (``am'' or ``pm'')
+ %r - time, 12-hour (same as %I:%M:%S %p)
+ %R - time, 24-hour (%H:%M)
+ %s - Number of seconds since 1970-01-01 00:00:00 UTC.
+ %S - Second of the minute (00..60)
+ %t - Tab character (\t)
+ %T - time, 24-hour (%H:%M:%S)
+ %u - Day of the week as a decimal, Monday being 1. (1..7)
+ %U - Week number of the current year,
+ starting with the first Sunday as the first
+ day of the first week (00..53)
+ %v - VMS date (%e-%b-%Y)
+ %V - Week number of year according to ISO 8601 (01..53)
+ %W - Week number of the current year,
+ starting with the first Monday as the first
+ day of the first week (00..53)
+ %w - Day of the week (Sunday is 0, 0..6)
+ %x - Preferred representation for the date alone, no time
+ %X - Preferred representation for the time alone, no date
+ %y - Year without a century (00..99)
+ %Y - Year with century
+ %z - Time zone as hour offset from UTC (e.g. +0900)
+ %Z - Time zone name
+ %% - Literal ``%'' character
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:strip, :type => :rvalue, :doc => <<-EOS
+This function removes leading and trailing whitespace from a string or from
+every string inside an array.
+
+*Examples:*
+
+ strip(" aaa ")
+
+Would result in: "aaa"
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:swapcase, :type => :rvalue, :doc => <<-EOS
+This function will swap the existing case of a string.
+
+*Examples:*
+
+ swapcase("aBcD")
+
+Would result in: "AbCd"
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:time, :type => :rvalue, :doc => <<-EOS
+This function will return the current time since epoch as an integer.
+
+*Examples:*
+
+ time()
+
+Will return something like: 1311972653
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:type, :type => :rvalue, :doc => <<-EOS
+Returns the type when passed a variable. Type can be one of:
+
+* string
+* array
+* hash
+* float
+* integer
EOS
) do |arguments|
# We note that Integer is the parent to Bignum and Fixnum ...
result = case klass
- when /^(?:Big|Fix)num$/ then 'Integer'
+ when /^(?:Big|Fix)num$/ then 'integer'
else klass
end
- return result
+ if result == "String" then
+ if value == value.to_i.to_s then
+ result = "Integer"
+ elsif value == value.to_f.to_s then
+ result = "Float"
+ end
+ end
+
+ return result.downcase
end
end
module Puppet::Parser::Functions
newfunction(:unique, :type => :rvalue, :doc => <<-EOS
+This function will remove duplicates from strings and arrays.
+
+*Examples:*
+
+ unique("aabbcc")
+
+Will return:
+
+ abc
+
+You can also use this with arrays:
+
+ unique(["a","a","b","b","c","c"])
+
+This returns:
+
+ ["a","b","c"]
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:upcase, :type => :rvalue, :doc => <<-EOS
+Converts a string or an array of strings to uppercase.
+
+*Examples:*
+
+ upcase("abcd")
+
+Will return:
+
+ ASDF
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:validate_resource, :type => :statement, :doc => <<-EOS
+This function when placed at the beginning of a class, will go looking for a
+valid kwalify schema by replacing the extension of the file with '.schema'.
+
+It will then validate the arguments passed to the function using that kwalify
+schema.
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:values, :type => :rvalue, :doc => <<-EOS
+When given a hash this function will return the values of that hash.
+
+*Examples:*
+
+ $hash = {
+ 'a' => 1,
+ 'b' => 2,
+ 'c' => 3,
+ }
+ values($hash)
+
+This example would return:
+
+ [1,2,3]
EOS
) do |arguments|
# values_at.rb
#
-# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ...
-# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ...
-
module Puppet::Parser::Functions
newfunction(:values_at, :type => :rvalue, :doc => <<-EOS
+Finds value inside an array based on location.
+
+The first argument is the array you want to analyze, and the second element can
+be a combination of:
+
+* A single numeric index
+* A range in the form of 'start-stop' (eg. 4-9)
+* An array combining the above
+
+*Examples*:
+
+ values_at(['a','b','c'], 2)
+
+Would return ['c'].
+
+ values_at(['a','b','c'], ["0-1"])
+
+Would return ['a','b'].
+
+ values_at(['a','b','c','d','e'], [0, "2-3"])
+
+Would return ['a','c','d'].
EOS
) do |arguments|
module Puppet::Parser::Functions
newfunction(:zip, :type => :rvalue, :doc => <<-EOS
+Takes one element from first array and merges corresponding elements from second array. This generates a sequence of n-element arrays, where n is one more than the count of arguments.
+
+*Example:*
+
+ zip(['1','2','3'],['4','5','6'])
+
+Would result in:
+
+ ["1", "4"], ["2", "5"], ["3", "6"]
EOS
) do |arguments|
lambda { @scope.function_type([]) }.should( raise_error(Puppet::ParseError))
end
- it "should return String when given a string" do
+ it "should return string when given a string" do
result = @scope.function_type(["aaabbbbcccc"])
- result.should(eq('String'))
+ result.should(eq('string'))
end
- it "should return Array when given an array" do
+ it "should return array when given an array" do
result = @scope.function_type([["aaabbbbcccc","asdf"]])
- result.should(eq('Array'))
+ result.should(eq('array'))
end
- it "should return Hash when given a hash" do
+ it "should return hash when given a hash" do
result = @scope.function_type([{"a"=>1,"b"=>2}])
- result.should(eq('Hash'))
+ result.should(eq('hash'))
+ end
+
+ it "should return integer when given an integer" do
+ result = @scope.function_type(["1"])
+ result.should(eq('integer'))
+ end
+
+ it "should return float when given a float" do
+ result = @scope.function_type(["1.34"])
+ result.should(eq('float'))
end
end
end
it "should remove duplicate elements in a string" do
- result = @scope.function_squeeze([["aabbc"]])
- result.should(eq(['abc']))
+ result = @scope.function_unique(["aabbc"])
+ result.should(eq('abc'))
+ end
+
+ it "should remove duplicate elements in an array" do
+ result = @scope.function_unique([["a","a","b","b","c"]])
+ result.should(eq(['a','b','c']))
end
end
result.should(eq(['1','2','3']))
end
+ it "should return values from a hash" do
+ lambda { @scope.function_values([['a','b','c']]) }.should( raise_error(Puppet::ParseError))
+ end
+
end