From 99a93d366f2e1efb977fcc8fe300d3d8357c8214 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Apr 2011 09:27:10 +0200 Subject: Convert to module format. --- lib/puppet/parser/functions/reverse.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/puppet/parser/functions/reverse.rb (limited to 'lib/puppet/parser/functions/reverse.rb') diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb new file mode 100644 index 0000000..79e9b93 --- /dev/null +++ b/lib/puppet/parser/functions/reverse.rb @@ -0,0 +1,27 @@ +# +# reverse.rb +# + +module Puppet::Parser::Functions + newfunction(:reverse, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "reverse(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'reverse(): Requires either ' + + 'array or string to work with') + end + + result = value.reverse + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 19313b43ea04066a88a0b78e83650ac52785e2e9 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 22:18:56 +0100 Subject: (#3) Apply missing documentation to more functions. --- lib/puppet/parser/functions/num2bool.rb | 2 + lib/puppet/parser/functions/prefix.rb | 7 +++ lib/puppet/parser/functions/range.rb | 12 +++++ lib/puppet/parser/functions/reverse.rb | 1 + lib/puppet/parser/functions/rstrip.rb | 1 + lib/puppet/parser/functions/shuffle.rb | 1 + lib/puppet/parser/functions/size.rb | 1 + lib/puppet/parser/functions/sort.rb | 1 + lib/puppet/parser/functions/squeeze.rb | 1 + lib/puppet/parser/functions/str2bool.rb | 3 ++ lib/puppet/parser/functions/strftime.rb | 63 ++++++++++++++++++++++++ lib/puppet/parser/functions/strip.rb | 8 +++ lib/puppet/parser/functions/swapcase.rb | 7 +++ lib/puppet/parser/functions/time.rb | 7 +++ lib/puppet/parser/functions/type.rb | 19 ++++++- lib/puppet/parser/functions/unique.rb | 17 +++++++ lib/puppet/parser/functions/upcase.rb | 9 ++++ lib/puppet/parser/functions/validate_resource.rb | 5 ++ lib/puppet/parser/functions/values.rb | 14 ++++++ lib/puppet/parser/functions/values_at.rb | 25 ++++++++-- lib/puppet/parser/functions/zip.rb | 9 ++++ spec/unit/parser/functions/type_spec.rb | 22 ++++++--- spec/unit/parser/functions/unique_spec.rb | 9 +++- spec/unit/parser/functions/values_spec.rb | 4 ++ 24 files changed, 235 insertions(+), 13 deletions(-) (limited to 'lib/puppet/parser/functions/reverse.rb') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 2baef62..874db22 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -6,6 +6,8 @@ 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| diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 0e0cee2..4593976 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -4,6 +4,13 @@ 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| diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index 6afb50c..6e85422 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -6,6 +6,18 @@ 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| diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb index 79e9b93..fe04869 100644 --- a/lib/puppet/parser/functions/reverse.rb +++ b/lib/puppet/parser/functions/reverse.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:reverse, :type => :rvalue, :doc => <<-EOS +Reverses the order of a string or array. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/rstrip.rb b/lib/puppet/parser/functions/rstrip.rb index 56849d3..29b0998 100644 --- a/lib/puppet/parser/functions/rstrip.rb +++ b/lib/puppet/parser/functions/rstrip.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS +Strips leading spaces to the right of the string. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb index 73e798c..18134ab 100644 --- a/lib/puppet/parser/functions/shuffle.rb +++ b/lib/puppet/parser/functions/shuffle.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS +Randomizes the order of a string or array elements. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/size.rb b/lib/puppet/parser/functions/size.rb index aa4f4ad..cc207e3 100644 --- a/lib/puppet/parser/functions/size.rb +++ b/lib/puppet/parser/functions/size.rb @@ -6,6 +6,7 @@ module Puppet::Parser::Functions newfunction(:size, :type => :rvalue, :doc => <<-EOS +Returns the number of elements in a string or array. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 49ca20a..3785496 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:sort, :type => :rvalue, :doc => <<-EOS +Sorts strings and arrays lexically. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index 9a1dd20..65c174a 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -4,6 +4,7 @@ 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| diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb index f3a6d6c..c320da6 100644 --- a/lib/puppet/parser/functions/str2bool.rb +++ b/lib/puppet/parser/functions/str2bool.rb @@ -4,6 +4,9 @@ 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| diff --git a/lib/puppet/parser/functions/strftime.rb b/lib/puppet/parser/functions/strftime.rb index c919320..0b52ade 100644 --- a/lib/puppet/parser/functions/strftime.rb +++ b/lib/puppet/parser/functions/strftime.rb @@ -4,6 +4,69 @@ 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| diff --git a/lib/puppet/parser/functions/strip.rb b/lib/puppet/parser/functions/strip.rb index ebab20e..5f4630d 100644 --- a/lib/puppet/parser/functions/strip.rb +++ b/lib/puppet/parser/functions/strip.rb @@ -4,6 +4,14 @@ 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| diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb index a0002a9..b9e6632 100644 --- a/lib/puppet/parser/functions/swapcase.rb +++ b/lib/puppet/parser/functions/swapcase.rb @@ -4,6 +4,13 @@ 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| diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb index e1f5b6f..0cddaf8 100644 --- a/lib/puppet/parser/functions/time.rb +++ b/lib/puppet/parser/functions/time.rb @@ -4,6 +4,13 @@ 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| diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb index 389a056..de6087e 100644 --- a/lib/puppet/parser/functions/type.rb +++ b/lib/puppet/parser/functions/type.rb @@ -4,6 +4,13 @@ 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| @@ -22,11 +29,19 @@ module Puppet::Parser::Functions # 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 diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb index a922c94..8844a74 100644 --- a/lib/puppet/parser/functions/unique.rb +++ b/lib/puppet/parser/functions/unique.rb @@ -4,6 +4,23 @@ 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| diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb index 8a9769d..fe6cadc 100644 --- a/lib/puppet/parser/functions/upcase.rb +++ b/lib/puppet/parser/functions/upcase.rb @@ -4,6 +4,15 @@ 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| diff --git a/lib/puppet/parser/functions/validate_resource.rb b/lib/puppet/parser/functions/validate_resource.rb index bbb5a54..d71ef3f 100644 --- a/lib/puppet/parser/functions/validate_resource.rb +++ b/lib/puppet/parser/functions/validate_resource.rb @@ -4,6 +4,11 @@ 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| diff --git a/lib/puppet/parser/functions/values.rb b/lib/puppet/parser/functions/values.rb index c1c4a77..1606756 100644 --- a/lib/puppet/parser/functions/values.rb +++ b/lib/puppet/parser/functions/values.rb @@ -4,6 +4,20 @@ 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| diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb index 331af6a..7f1de8e 100644 --- a/lib/puppet/parser/functions/values_at.rb +++ b/lib/puppet/parser/functions/values_at.rb @@ -2,11 +2,30 @@ # 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| diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb index 024d64a..2b56e9c 100644 --- a/lib/puppet/parser/functions/zip.rb +++ b/lib/puppet/parser/functions/zip.rb @@ -4,6 +4,15 @@ 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| diff --git a/spec/unit/parser/functions/type_spec.rb b/spec/unit/parser/functions/type_spec.rb index 36c3823..53071f3 100644 --- a/spec/unit/parser/functions/type_spec.rb +++ b/spec/unit/parser/functions/type_spec.rb @@ -18,19 +18,29 @@ describe "the type function" do 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 diff --git a/spec/unit/parser/functions/unique_spec.rb b/spec/unit/parser/functions/unique_spec.rb index 7b8b08d..627dc33 100644 --- a/spec/unit/parser/functions/unique_spec.rb +++ b/spec/unit/parser/functions/unique_spec.rb @@ -19,8 +19,13 @@ describe "the unique function" do 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 diff --git a/spec/unit/parser/functions/values_spec.rb b/spec/unit/parser/functions/values_spec.rb index 92f1311..f6eb5b6 100644 --- a/spec/unit/parser/functions/values_spec.rb +++ b/spec/unit/parser/functions/values_spec.rb @@ -23,4 +23,8 @@ describe "the values function" do 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 -- cgit v1.2.3