puppetmodule-stdlib 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. data/CHANGELOG +253 -0
  2. data/CONTRIBUTING.md +65 -0
  3. data/Gemfile +33 -0
  4. data/LICENSE +19 -0
  5. data/Modulefile +11 -0
  6. data/README.markdown +1173 -0
  7. data/README_DEVELOPER.markdown +35 -0
  8. data/RELEASE_PROCESS.markdown +24 -0
  9. data/Rakefile +2 -0
  10. data/lib/facter/pe_version.rb +53 -0
  11. data/lib/facter/puppet_vardir.rb +26 -0
  12. data/lib/facter/root_home.rb +19 -0
  13. data/lib/facter/util/puppet_settings.rb +21 -0
  14. data/lib/puppet/parser/functions/abs.rb +36 -0
  15. data/lib/puppet/parser/functions/any2array.rb +33 -0
  16. data/lib/puppet/parser/functions/bool2num.rb +49 -0
  17. data/lib/puppet/parser/functions/capitalize.rb +34 -0
  18. data/lib/puppet/parser/functions/chomp.rb +35 -0
  19. data/lib/puppet/parser/functions/chop.rb +37 -0
  20. data/lib/puppet/parser/functions/concat.rb +37 -0
  21. data/lib/puppet/parser/functions/count.rb +22 -0
  22. data/lib/puppet/parser/functions/defined_with_params.rb +35 -0
  23. data/lib/puppet/parser/functions/delete.rb +46 -0
  24. data/lib/puppet/parser/functions/delete_at.rb +49 -0
  25. data/lib/puppet/parser/functions/downcase.rb +33 -0
  26. data/lib/puppet/parser/functions/empty.rb +28 -0
  27. data/lib/puppet/parser/functions/ensure_packages.rb +24 -0
  28. data/lib/puppet/parser/functions/ensure_resource.rb +35 -0
  29. data/lib/puppet/parser/functions/flatten.rb +33 -0
  30. data/lib/puppet/parser/functions/floor.rb +20 -0
  31. data/lib/puppet/parser/functions/fqdn_rotate.rb +46 -0
  32. data/lib/puppet/parser/functions/get_module_path.rb +17 -0
  33. data/lib/puppet/parser/functions/getparam.rb +35 -0
  34. data/lib/puppet/parser/functions/getvar.rb +26 -0
  35. data/lib/puppet/parser/functions/grep.rb +33 -0
  36. data/lib/puppet/parser/functions/has_interface_with.rb +52 -0
  37. data/lib/puppet/parser/functions/has_ip_address.rb +25 -0
  38. data/lib/puppet/parser/functions/has_ip_network.rb +25 -0
  39. data/lib/puppet/parser/functions/has_key.rb +28 -0
  40. data/lib/puppet/parser/functions/hash.rb +41 -0
  41. data/lib/puppet/parser/functions/is_array.rb +22 -0
  42. data/lib/puppet/parser/functions/is_domain_name.rb +47 -0
  43. data/lib/puppet/parser/functions/is_float.rb +27 -0
  44. data/lib/puppet/parser/functions/is_function_available.rb +23 -0
  45. data/lib/puppet/parser/functions/is_hash.rb +22 -0
  46. data/lib/puppet/parser/functions/is_integer.rb +27 -0
  47. data/lib/puppet/parser/functions/is_ip_address.rb +32 -0
  48. data/lib/puppet/parser/functions/is_mac_address.rb +27 -0
  49. data/lib/puppet/parser/functions/is_numeric.rb +27 -0
  50. data/lib/puppet/parser/functions/is_string.rb +26 -0
  51. data/lib/puppet/parser/functions/join.rb +41 -0
  52. data/lib/puppet/parser/functions/join_keys_to_values.rb +47 -0
  53. data/lib/puppet/parser/functions/keys.rb +26 -0
  54. data/lib/puppet/parser/functions/loadyaml.rb +20 -0
  55. data/lib/puppet/parser/functions/lstrip.rb +33 -0
  56. data/lib/puppet/parser/functions/max.rb +21 -0
  57. data/lib/puppet/parser/functions/member.rb +44 -0
  58. data/lib/puppet/parser/functions/merge.rb +33 -0
  59. data/lib/puppet/parser/functions/min.rb +21 -0
  60. data/lib/puppet/parser/functions/num2bool.rb +43 -0
  61. data/lib/puppet/parser/functions/parsejson.rb +24 -0
  62. data/lib/puppet/parser/functions/parseyaml.rb +24 -0
  63. data/lib/puppet/parser/functions/pick.rb +29 -0
  64. data/lib/puppet/parser/functions/prefix.rb +45 -0
  65. data/lib/puppet/parser/functions/range.rb +80 -0
  66. data/lib/puppet/parser/functions/reject.rb +31 -0
  67. data/lib/puppet/parser/functions/reverse.rb +28 -0
  68. data/lib/puppet/parser/functions/rstrip.rb +32 -0
  69. data/lib/puppet/parser/functions/shuffle.rb +46 -0
  70. data/lib/puppet/parser/functions/size.rb +48 -0
  71. data/lib/puppet/parser/functions/sort.rb +27 -0
  72. data/lib/puppet/parser/functions/squeeze.rb +36 -0
  73. data/lib/puppet/parser/functions/str2bool.rb +46 -0
  74. data/lib/puppet/parser/functions/str2saltedsha512.rb +32 -0
  75. data/lib/puppet/parser/functions/strftime.rb +107 -0
  76. data/lib/puppet/parser/functions/strip.rb +39 -0
  77. data/lib/puppet/parser/functions/suffix.rb +45 -0
  78. data/lib/puppet/parser/functions/swapcase.rb +39 -0
  79. data/lib/puppet/parser/functions/time.rb +49 -0
  80. data/lib/puppet/parser/functions/to_bytes.rb +28 -0
  81. data/lib/puppet/parser/functions/type.rb +50 -0
  82. data/lib/puppet/parser/functions/unique.rb +51 -0
  83. data/lib/puppet/parser/functions/upcase.rb +41 -0
  84. data/lib/puppet/parser/functions/uriescape.rb +36 -0
  85. data/lib/puppet/parser/functions/validate_absolute_path.rb +56 -0
  86. data/lib/puppet/parser/functions/validate_array.rb +33 -0
  87. data/lib/puppet/parser/functions/validate_augeas.rb +81 -0
  88. data/lib/puppet/parser/functions/validate_bool.rb +34 -0
  89. data/lib/puppet/parser/functions/validate_cmd.rb +47 -0
  90. data/lib/puppet/parser/functions/validate_hash.rb +33 -0
  91. data/lib/puppet/parser/functions/validate_re.rb +40 -0
  92. data/lib/puppet/parser/functions/validate_slength.rb +52 -0
  93. data/lib/puppet/parser/functions/validate_string.rb +33 -0
  94. data/lib/puppet/parser/functions/values.rb +39 -0
  95. data/lib/puppet/parser/functions/values_at.rb +98 -0
  96. data/lib/puppet/parser/functions/zip.rb +65 -0
  97. data/lib/puppet/provider/file_line/ruby.rb +59 -0
  98. data/lib/puppet/type/anchor.rb +41 -0
  99. data/lib/puppet/type/file_line.rb +70 -0
  100. data/manifests/init.pp +20 -0
  101. data/manifests/stages.pp +43 -0
  102. data/spec/functions/defined_with_params_spec.rb +37 -0
  103. data/spec/functions/ensure_packages_spec.rb +42 -0
  104. data/spec/functions/ensure_resource_spec.rb +40 -0
  105. data/spec/functions/getparam_spec.rb +34 -0
  106. data/spec/monkey_patches/alias_should_to_must.rb +8 -0
  107. data/spec/monkey_patches/publicize_methods.rb +10 -0
  108. data/spec/spec.opts +6 -0
  109. data/spec/spec_helper.rb +28 -0
  110. data/spec/unit/facter/pe_version_spec.rb +76 -0
  111. data/spec/unit/facter/root_home_spec.rb +40 -0
  112. data/spec/unit/facter/util/puppet_settings_spec.rb +35 -0
  113. data/spec/unit/puppet/parser/functions/abs_spec.rb +25 -0
  114. data/spec/unit/puppet/parser/functions/any2array_spec.rb +55 -0
  115. data/spec/unit/puppet/parser/functions/bool2num_spec.rb +24 -0
  116. data/spec/unit/puppet/parser/functions/capitalize_spec.rb +19 -0
  117. data/spec/unit/puppet/parser/functions/chomp_spec.rb +19 -0
  118. data/spec/unit/puppet/parser/functions/chop_spec.rb +19 -0
  119. data/spec/unit/puppet/parser/functions/concat_spec.rb +15 -0
  120. data/spec/unit/puppet/parser/functions/count_spec.rb +31 -0
  121. data/spec/unit/puppet/parser/functions/delete_at_spec.rb +19 -0
  122. data/spec/unit/puppet/parser/functions/delete_spec.rb +38 -0
  123. data/spec/unit/puppet/parser/functions/downcase_spec.rb +24 -0
  124. data/spec/unit/puppet/parser/functions/empty_spec.rb +23 -0
  125. data/spec/unit/puppet/parser/functions/flatten_spec.rb +27 -0
  126. data/spec/unit/puppet/parser/functions/floor_spec.rb +39 -0
  127. data/spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb +33 -0
  128. data/spec/unit/puppet/parser/functions/get_module_path_spec.rb +46 -0
  129. data/spec/unit/puppet/parser/functions/getvar_spec.rb +37 -0
  130. data/spec/unit/puppet/parser/functions/grep_spec.rb +19 -0
  131. data/spec/unit/puppet/parser/functions/has_interface_with_spec.rb +64 -0
  132. data/spec/unit/puppet/parser/functions/has_ip_address_spec.rb +39 -0
  133. data/spec/unit/puppet/parser/functions/has_ip_network_spec.rb +36 -0
  134. data/spec/unit/puppet/parser/functions/has_key_spec.rb +42 -0
  135. data/spec/unit/puppet/parser/functions/hash_spec.rb +19 -0
  136. data/spec/unit/puppet/parser/functions/is_array_spec.rb +29 -0
  137. data/spec/unit/puppet/parser/functions/is_domain_name_spec.rb +64 -0
  138. data/spec/unit/puppet/parser/functions/is_float_spec.rb +33 -0
  139. data/spec/unit/puppet/parser/functions/is_function_available.rb +31 -0
  140. data/spec/unit/puppet/parser/functions/is_hash_spec.rb +29 -0
  141. data/spec/unit/puppet/parser/functions/is_integer_spec.rb +34 -0
  142. data/spec/unit/puppet/parser/functions/is_ip_address_spec.rb +39 -0
  143. data/spec/unit/puppet/parser/functions/is_mac_address_spec.rb +29 -0
  144. data/spec/unit/puppet/parser/functions/is_numeric_spec.rb +39 -0
  145. data/spec/unit/puppet/parser/functions/is_string_spec.rb +34 -0
  146. data/spec/unit/puppet/parser/functions/join_keys_to_values_spec.rb +40 -0
  147. data/spec/unit/puppet/parser/functions/join_spec.rb +19 -0
  148. data/spec/unit/puppet/parser/functions/keys_spec.rb +21 -0
  149. data/spec/unit/puppet/parser/functions/lstrip_spec.rb +19 -0
  150. data/spec/unit/puppet/parser/functions/max_spec.rb +27 -0
  151. data/spec/unit/puppet/parser/functions/member_spec.rb +24 -0
  152. data/spec/unit/puppet/parser/functions/merge_spec.rb +47 -0
  153. data/spec/unit/puppet/parser/functions/min_spec.rb +27 -0
  154. data/spec/unit/puppet/parser/functions/num2bool_spec.rb +67 -0
  155. data/spec/unit/puppet/parser/functions/parsejson_spec.rb +22 -0
  156. data/spec/unit/puppet/parser/functions/parseyaml_spec.rb +24 -0
  157. data/spec/unit/puppet/parser/functions/pick_spec.rb +34 -0
  158. data/spec/unit/puppet/parser/functions/prefix_spec.rb +19 -0
  159. data/spec/unit/puppet/parser/functions/range_spec.rb +34 -0
  160. data/spec/unit/puppet/parser/functions/reject_spec.rb +20 -0
  161. data/spec/unit/puppet/parser/functions/reverse_spec.rb +19 -0
  162. data/spec/unit/puppet/parser/functions/rstrip_spec.rb +24 -0
  163. data/spec/unit/puppet/parser/functions/shuffle_spec.rb +24 -0
  164. data/spec/unit/puppet/parser/functions/size_spec.rb +24 -0
  165. data/spec/unit/puppet/parser/functions/sort_spec.rb +24 -0
  166. data/spec/unit/puppet/parser/functions/squeeze_spec.rb +24 -0
  167. data/spec/unit/puppet/parser/functions/str2bool_spec.rb +31 -0
  168. data/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb +45 -0
  169. data/spec/unit/puppet/parser/functions/strftime_spec.rb +29 -0
  170. data/spec/unit/puppet/parser/functions/strip_spec.rb +18 -0
  171. data/spec/unit/puppet/parser/functions/suffix_spec.rb +19 -0
  172. data/spec/unit/puppet/parser/functions/swapcase_spec.rb +19 -0
  173. data/spec/unit/puppet/parser/functions/time_spec.rb +29 -0
  174. data/spec/unit/puppet/parser/functions/to_bytes_spec.rb +58 -0
  175. data/spec/unit/puppet/parser/functions/type_spec.rb +43 -0
  176. data/spec/unit/puppet/parser/functions/unique_spec.rb +24 -0
  177. data/spec/unit/puppet/parser/functions/upcase_spec.rb +24 -0
  178. data/spec/unit/puppet/parser/functions/uriescape_spec.rb +24 -0
  179. data/spec/unit/puppet/parser/functions/validate_absolute_path_spec.rb +83 -0
  180. data/spec/unit/puppet/parser/functions/validate_array_spec.rb +38 -0
  181. data/spec/unit/puppet/parser/functions/validate_augeas_spec.rb +102 -0
  182. data/spec/unit/puppet/parser/functions/validate_bool_spec.rb +51 -0
  183. data/spec/unit/puppet/parser/functions/validate_cmd_spec.rb +81 -0
  184. data/spec/unit/puppet/parser/functions/validate_hash_spec.rb +43 -0
  185. data/spec/unit/puppet/parser/functions/validate_re_spec.rb +76 -0
  186. data/spec/unit/puppet/parser/functions/validate_slength_spec.rb +48 -0
  187. data/spec/unit/puppet/parser/functions/validate_string_spec.rb +60 -0
  188. data/spec/unit/puppet/parser/functions/values_at_spec.rb +38 -0
  189. data/spec/unit/puppet/parser/functions/values_spec.rb +31 -0
  190. data/spec/unit/puppet/parser/functions/zip_spec.rb +15 -0
  191. data/spec/unit/puppet/provider/file_line/ruby_spec.rb +127 -0
  192. data/spec/unit/puppet/type/anchor_spec.rb +11 -0
  193. data/spec/unit/puppet/type/file_line_spec.rb +69 -0
  194. data/spec/watchr.rb +86 -0
  195. data/tests/file_line.pp +9 -0
  196. data/tests/has_interface_with.pp +10 -0
  197. data/tests/has_ip_address.pp +3 -0
  198. data/tests/has_ip_network.pp +4 -0
  199. data/tests/init.pp +1 -0
  200. metadata +252 -0
@@ -0,0 +1,29 @@
1
+ module Puppet::Parser::Functions
2
+ newfunction(:pick, :type => :rvalue, :doc => <<-EOS
3
+
4
+ This function is similar to a coalesce function in SQL in that it will return
5
+ the first value in a list of values that is not undefined or an empty string
6
+ (two things in Puppet that will return a boolean false value). Typically,
7
+ this function is used to check for a value in the Puppet Dashboard/Enterprise
8
+ Console, and failover to a default value like the following:
9
+
10
+ $real_jenkins_version = pick($::jenkins_version, '1.449')
11
+
12
+ The value of $real_jenkins_version will first look for a top-scope variable
13
+ called 'jenkins_version' (note that parameters set in the Puppet Dashboard/
14
+ Enterprise Console are brought into Puppet as top-scope variables), and,
15
+ failing that, will use a default value of 1.449.
16
+
17
+ EOS
18
+ ) do |args|
19
+ args = args.compact
20
+ args.delete(:undef)
21
+ args.delete(:undefined)
22
+ args.delete("")
23
+ if args[0].to_s.empty? then
24
+ fail "Must provide non empty value."
25
+ else
26
+ return args[0]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,45 @@
1
+ #
2
+ # prefix.rb
3
+ #
4
+
5
+ module Puppet::Parser::Functions
6
+ newfunction(:prefix, :type => :rvalue, :doc => <<-EOS
7
+ This function applies a prefix to all elements in an array.
8
+
9
+ *Examples:*
10
+
11
+ prefix(['a','b','c'], 'p')
12
+
13
+ Will return: ['pa','pb','pc']
14
+ EOS
15
+ ) do |arguments|
16
+
17
+ # Technically we support two arguments but only first is mandatory ...
18
+ raise(Puppet::ParseError, "prefix(): Wrong number of arguments " +
19
+ "given (#{arguments.size} for 1)") if arguments.size < 1
20
+
21
+ array = arguments[0]
22
+
23
+ unless array.is_a?(Array)
24
+ raise Puppet::ParseError, "prefix(): expected first argument to be an Array, got #{array.inspect}"
25
+ end
26
+
27
+ prefix = arguments[1] if arguments[1]
28
+
29
+ if prefix
30
+ unless prefix.is_a?(String)
31
+ raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{suffix.inspect}"
32
+ end
33
+ end
34
+
35
+ # Turn everything into string same as join would do ...
36
+ result = array.collect do |i|
37
+ i = i.to_s
38
+ prefix ? prefix + i : i
39
+ end
40
+
41
+ return result
42
+ end
43
+ end
44
+
45
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,80 @@
1
+ #
2
+ # range.rb
3
+ #
4
+
5
+ # TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ...
6
+
7
+ module Puppet::Parser::Functions
8
+ newfunction(:range, :type => :rvalue, :doc => <<-EOS
9
+ When given range in the form of (start, stop) it will extrapolate a range as
10
+ an array.
11
+
12
+ *Examples:*
13
+
14
+ range("0", "9")
15
+
16
+ Will return: [0,1,2,3,4,5,6,7,8,9]
17
+
18
+ range("00", "09")
19
+
20
+ Will return: [0,1,2,3,4,5,6,7,8,9] (Zero padded strings are converted to
21
+ integers automatically)
22
+
23
+ range("a", "c")
24
+
25
+ Will return: ["a","b","c"]
26
+
27
+ range("host01", "host10")
28
+
29
+ Will return: ["host01", "host02", ..., "host09", "host10"]
30
+ EOS
31
+ ) do |arguments|
32
+
33
+ # We support more than one argument but at least one is mandatory ...
34
+ raise(Puppet::ParseError, "range(): Wrong number of " +
35
+ "arguments given (#{arguments.size} for 1)") if arguments.size < 1
36
+
37
+ if arguments.size > 1
38
+ start = arguments[0]
39
+ stop = arguments[1]
40
+
41
+ type = '..' # We select simplest type for Range available in Ruby ...
42
+
43
+ elsif arguments.size > 0
44
+ value = arguments[0]
45
+
46
+ if m = value.match(/^(\w+)(\.\.\.?|\-)(\w+)$/)
47
+ start = m[1]
48
+ stop = m[3]
49
+
50
+ type = m[2]
51
+
52
+ elsif value.match(/^.+$/)
53
+ raise(Puppet::ParseError, 'range(): Unable to compute range ' +
54
+ 'from the value given')
55
+ else
56
+ raise(Puppet::ParseError, 'range(): Unknown format of range given')
57
+ end
58
+ end
59
+
60
+ # Check whether we have integer value if so then make it so ...
61
+ if start.match(/^\d+$/)
62
+ start = start.to_i
63
+ stop = stop.to_i
64
+ else
65
+ start = start.to_s
66
+ stop = stop.to_s
67
+ end
68
+
69
+ range = case type
70
+ when /^(\.\.|\-)$/ then (start .. stop)
71
+ when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ...
72
+ end
73
+
74
+ result = range.collect { |i| i } # Get them all ... Pokemon ...
75
+
76
+ return result
77
+ end
78
+ end
79
+
80
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,31 @@
1
+ #
2
+ # reject.rb
3
+ #
4
+
5
+ module Puppet::Parser::Functions
6
+ newfunction(:reject, :type => :rvalue, :doc => <<-EOS) do |args|
7
+ This function searches through an array and rejects all elements that match
8
+ the provided regular expression.
9
+
10
+ *Examples:*
11
+
12
+ reject(['aaa','bbb','ccc','aaaddd'], 'aaa')
13
+
14
+ Would return:
15
+
16
+ ['bbb','ccc']
17
+ EOS
18
+
19
+ if (args.size != 2)
20
+ raise Puppet::ParseError,
21
+ "reject(): Wrong number of arguments given #{args.size} for 2"
22
+ end
23
+
24
+ ary = args[0]
25
+ pattern = Regexp.new(args[1])
26
+
27
+ ary.reject { |e| e =~ pattern }
28
+ end
29
+ end
30
+
31
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,28 @@
1
+ #
2
+ # reverse.rb
3
+ #
4
+
5
+ module Puppet::Parser::Functions
6
+ newfunction(:reverse, :type => :rvalue, :doc => <<-EOS
7
+ Reverses the order of a string or array.
8
+ EOS
9
+ ) do |arguments|
10
+
11
+ raise(Puppet::ParseError, "reverse(): Wrong number of arguments " +
12
+ "given (#{arguments.size} for 1)") if arguments.size < 1
13
+
14
+ value = arguments[0]
15
+ klass = value.class
16
+
17
+ unless [Array, String].include?(klass)
18
+ raise(Puppet::ParseError, 'reverse(): Requires either ' +
19
+ 'array or string to work with')
20
+ end
21
+
22
+ result = value.reverse
23
+
24
+ return result
25
+ end
26
+ end
27
+
28
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,32 @@
1
+ #
2
+ # rstrip.rb
3
+ #
4
+
5
+ module Puppet::Parser::Functions
6
+ newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS
7
+ Strips leading spaces to the right of the string.
8
+ EOS
9
+ ) do |arguments|
10
+
11
+ raise(Puppet::ParseError, "rstrip(): Wrong number of arguments " +
12
+ "given (#{arguments.size} for 1)") if arguments.size < 1
13
+
14
+ value = arguments[0]
15
+ klass = value.class
16
+
17
+ unless [Array, String].include?(klass)
18
+ raise(Puppet::ParseError, 'rstrip(): Requires either ' +
19
+ 'array or string to work with')
20
+ end
21
+
22
+ if value.is_a?(Array)
23
+ result = value.collect { |i| i.is_a?(String) ? i.rstrip : i }
24
+ else
25
+ result = value.rstrip
26
+ end
27
+
28
+ return result
29
+ end
30
+ end
31
+
32
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,46 @@
1
+ #
2
+ # shuffle.rb
3
+ #
4
+
5
+ module Puppet::Parser::Functions
6
+ newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS
7
+ Randomizes the order of a string or array elements.
8
+ EOS
9
+ ) do |arguments|
10
+
11
+ raise(Puppet::ParseError, "shuffle(): Wrong number of arguments " +
12
+ "given (#{arguments.size} for 1)") if arguments.size < 1
13
+
14
+ value = arguments[0]
15
+ klass = value.class
16
+
17
+ unless [Array, String].include?(klass)
18
+ raise(Puppet::ParseError, 'shuffle(): Requires either ' +
19
+ 'array or string to work with')
20
+ end
21
+
22
+ result = value.clone
23
+
24
+ string = value.is_a?(String) ? true : false
25
+
26
+ # Check whether it makes sense to shuffle ...
27
+ return result if result.size <= 1
28
+
29
+ # We turn any string value into an array to be able to shuffle ...
30
+ result = string ? result.split('') : result
31
+
32
+ elements = result.size
33
+
34
+ # Simple implementation of Fisher–Yates in-place shuffle ...
35
+ elements.times do |i|
36
+ j = rand(elements - i) + i
37
+ result[j], result[i] = result[i], result[j]
38
+ end
39
+
40
+ result = string ? result.join : result
41
+
42
+ return result
43
+ end
44
+ end
45
+
46
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,48 @@
1
+ #
2
+ # size.rb
3
+ #
4
+
5
+ # TODO(Krzysztof Wilczynski): Support for hashes would be nice too ...
6
+
7
+ module Puppet::Parser::Functions
8
+ newfunction(:size, :type => :rvalue, :doc => <<-EOS
9
+ Returns the number of elements in a string or array.
10
+ EOS
11
+ ) do |arguments|
12
+
13
+ raise(Puppet::ParseError, "size(): Wrong number of arguments " +
14
+ "given (#{arguments.size} for 1)") if arguments.size < 1
15
+
16
+ item = arguments[0]
17
+
18
+ if item.is_a?(String)
19
+
20
+ begin
21
+ #
22
+ # Check whether your item is a numeric value or not ...
23
+ # This will take care about positive and/or negative numbers
24
+ # for both integer and floating-point values ...
25
+ #
26
+ # Please note that Puppet has no notion of hexadecimal
27
+ # nor octal numbers for its DSL at this point in time ...
28
+ #
29
+ Float(item)
30
+
31
+ raise(Puppet::ParseError, 'size(): Requires either ' +
32
+ 'string or array to work with')
33
+
34
+ rescue ArgumentError
35
+ result = item.size
36
+ end
37
+
38
+ elsif item.is_a?(Array)
39
+ result = item.size
40
+ else
41
+ raise(Puppet::ParseError, 'size(): Unknown type given')
42
+ end
43
+
44
+ return result
45
+ end
46
+ end
47
+
48
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,27 @@
1
+ #
2
+ # sort.rb
3
+ #
4
+
5
+ module Puppet::Parser::Functions
6
+ newfunction(:sort, :type => :rvalue, :doc => <<-EOS
7
+ Sorts strings and arrays lexically.
8
+ EOS
9
+ ) do |arguments|
10
+
11
+ if (arguments.size != 1) then
12
+ raise(Puppet::ParseError, "sort(): Wrong number of arguments "+
13
+ "given #{arguments.size} for 1")
14
+ end
15
+
16
+ value = arguments[0]
17
+
18
+ if value.is_a?(Array) then
19
+ value.sort
20
+ elsif value.is_a?(String) then
21
+ value.split("").sort.join("")
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,36 @@
1
+ #
2
+ # squeeze.rb
3
+ #
4
+
5
+ module Puppet::Parser::Functions
6
+ newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS
7
+ Returns a new string where runs of the same character that occur in this set are replaced by a single character.
8
+ EOS
9
+ ) do |arguments|
10
+
11
+ if ((arguments.size != 2) and (arguments.size != 1)) then
12
+ raise(Puppet::ParseError, "squeeze(): Wrong number of arguments "+
13
+ "given #{arguments.size} for 2 or 1")
14
+ end
15
+
16
+ item = arguments[0]
17
+ squeezeval = arguments[1]
18
+
19
+ if item.is_a?(Array) then
20
+ if squeezeval then
21
+ item.collect { |i| i.squeeze(squeezeval) }
22
+ else
23
+ item.collect { |i| i.squeeze }
24
+ end
25
+ else
26
+ if squeezeval then
27
+ item.squeeze(squeezeval)
28
+ else
29
+ item.squeeze
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+
36
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,46 @@
1
+ #
2
+ # str2bool.rb
3
+ #
4
+
5
+ module Puppet::Parser::Functions
6
+ newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS
7
+ This converts a string to a boolean. This attempt to convert strings that
8
+ contain things like: y, 1, t, true to 'true' and strings that contain things
9
+ like: 0, f, n, false, no to 'false'.
10
+ EOS
11
+ ) do |arguments|
12
+
13
+ raise(Puppet::ParseError, "str2bool(): Wrong number of arguments " +
14
+ "given (#{arguments.size} for 1)") if arguments.size < 1
15
+
16
+ string = arguments[0]
17
+
18
+ # If string is already Boolean, return it
19
+ if !!string == string
20
+ return string
21
+ end
22
+
23
+ unless string.is_a?(String)
24
+ raise(Puppet::ParseError, 'str2bool(): Requires either ' +
25
+ 'string to work with')
26
+ end
27
+
28
+ # We consider all the yes, no, y, n and so on too ...
29
+ result = case string
30
+ #
31
+ # This is how undef looks like in Puppet ...
32
+ # We yield false in this case.
33
+ #
34
+ when /^$/, '' then false # Empty string will be false ...
35
+ when /^(1|t|y|true|yes)$/ then true
36
+ when /^(0|f|n|false|no)$/ then false
37
+ when /^(undef|undefined)$/ then false # This is not likely to happen ...
38
+ else
39
+ raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given')
40
+ end
41
+
42
+ return result
43
+ end
44
+ end
45
+
46
+ # vim: set ts=2 sw=2 et :