]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
(#3) Apply missing documentation to more functions.
authorKen Barber <ken@bob.sh>
Fri, 29 Jul 2011 21:18:56 +0000 (22:18 +0100)
committerKen Barber <ken@bob.sh>
Fri, 29 Jul 2011 21:18:56 +0000 (22:18 +0100)
24 files changed:
lib/puppet/parser/functions/num2bool.rb
lib/puppet/parser/functions/prefix.rb
lib/puppet/parser/functions/range.rb
lib/puppet/parser/functions/reverse.rb
lib/puppet/parser/functions/rstrip.rb
lib/puppet/parser/functions/shuffle.rb
lib/puppet/parser/functions/size.rb
lib/puppet/parser/functions/sort.rb
lib/puppet/parser/functions/squeeze.rb
lib/puppet/parser/functions/str2bool.rb
lib/puppet/parser/functions/strftime.rb
lib/puppet/parser/functions/strip.rb
lib/puppet/parser/functions/swapcase.rb
lib/puppet/parser/functions/time.rb
lib/puppet/parser/functions/type.rb
lib/puppet/parser/functions/unique.rb
lib/puppet/parser/functions/upcase.rb
lib/puppet/parser/functions/validate_resource.rb
lib/puppet/parser/functions/values.rb
lib/puppet/parser/functions/values_at.rb
lib/puppet/parser/functions/zip.rb
spec/unit/parser/functions/type_spec.rb
spec/unit/parser/functions/unique_spec.rb
spec/unit/parser/functions/values_spec.rb

index 2baef62778389141e13e7613fcc92986ac89334d..874db226e673b25cf1027f7b10b84211f58b1407 100644 (file)
@@ -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|
 
index 0e0cee2b4e46281a01731593fa68d089d3a9dbe6..4593976673b467d5bf636945e968d8da7ae16edc 100644 (file)
@@ -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|
 
index 6afb50c299b61d07f73dad189762b568dbd519b8..6e854225075ba763b92b090ff4f09ee10b247fc9 100644 (file)
@@ -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|
 
index 79e9b9358ee7fa2c0d8770b2d53a5eed52117b8e..fe048690cdddddbc25582d04ac0f174be4883a24 100644 (file)
@@ -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|
 
index 56849d30a4b282506d7211de4a10690e74fbbcfe..29b0998205f37fed5357f65a2fd6409d37e4ba7b 100644 (file)
@@ -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|
 
index 73e798c91afbf6f2107dfaf42c0f8f5549e73310..18134ab633ea962a41016be3d5ac795e60077670 100644 (file)
@@ -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|
 
index aa4f4ada08ed937d836bd982d3ef313850d30df3..cc207e3fadc40fc10477eb351fcfd1d4ac775704 100644 (file)
@@ -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|
 
index 49ca20accb4076d457e0823649e2e60b4766a6d5..378549624c97a7aaed323acd3091826fb4b387e9 100644 (file)
@@ -4,6 +4,7 @@
 
 module Puppet::Parser::Functions
   newfunction(:sort, :type => :rvalue, :doc => <<-EOS
+Sorts strings and arrays lexically.
     EOS
   ) do |arguments|
 
index 9a1dd20eb9af13a04549fe7f61bbeed71bdc2f43..65c174aae84ee5d6c8d2450d7ad9c2b4310bcbee 100644 (file)
@@ -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|
 
index f3a6d6cb65257bc2dc453b8321351e2830dd2ca6..c320da663a2fc162f2fbcbad52ae3df90b3000f3 100644 (file)
@@ -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|
 
index c91932070f2bbe72b95bccfdf79526f6ed1d3aa1..0b52adecdbfc36527477fef37889dd5e0f18c374 100644 (file)
@@ -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|
 
index ebab20e7232866717f34a62b70c8312ae7d7c026..5f4630d7de97a1f382f127c5b82a210cfaf1e06a 100644 (file)
@@ -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|
 
index a0002a94240306612e37af37201686eb5e6ddf48..b9e663253389f7c6453047a95021bb9ebabbab50 100644 (file)
@@ -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|
 
index e1f5b6f7b4ab9ba3c13630e0a32f4d16313af359..0cddaf86b9341cd4b2ed21fd68bafdce83914ce2 100644 (file)
@@ -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|
 
index 389a0562b1c810d73d48945c8c682bb357b05cda..de6087e877d32891d53e626b50f29bbc66ede4f9 100644 (file)
@@ -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
 
index a922c94d55fd088df3eb710f6e5672c8ed40eee8..8844a7418526da26046c3d810134f61ea726213d 100644 (file)
@@ -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|
 
index 8a9769d820160449b82b45feab008c1ca2a97f39..fe6cadc3cced1b88ab530c2db2501a0cdcd8050d 100644 (file)
@@ -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|
 
index bbb5a546a5d41ba6d7bafea2c2635e0ca5c689fa..d71ef3f8900fdc8e5ea3bdac0e54896747efe017 100644 (file)
@@ -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|
  
index c1c4a774dbdbe7a2cdbbee4fd3d9fd0a0daeab9c..16067561b4006d684315a7577199cd2911a05060 100644 (file)
@@ -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|
 
index 331af6a138cec8b8ccda290c30cfe9c41df222ef..7f1de8e45d1059bfea55a6ac2c215562dfb1445f 100644 (file)
@@ -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|
 
index 024d64ac747afeb9908b8d69a0a8dab243c3e944..2b56e9ca07089716970d1d09a8dcdbb2ded230f2 100644 (file)
@@ -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|
 
index 36c382364b1bff8e8dd43d718f431e49e0ec61be..53071f37a64dc4c5ab6ef258156aef8199341c41 100644 (file)
@@ -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
index 7b8b08dcef9e75e38196b6676244bc0d050e1eaa..627dc334c429bbe2d632008192cb18982e369c8b 100644 (file)
@@ -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
index 92f1311d48a363ded4bb2637b675faf83b836ad7..f6eb5b666ed3ba6436600a63aa6752bdc73ca97b 100644 (file)
@@ -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