diff options
author | Rob Fugina <rfugina@genome.wustl.edu> | 2014-11-17 16:01:42 -0600 |
---|---|---|
committer | Rob Fugina <rfugina@genome.wustl.edu> | 2014-12-17 12:39:40 -0600 |
commit | ef3d42f7bbdf95b21f46e580de309298cad300ea (patch) | |
tree | d4cf331b1ff8c7b10974cd8d20325272268d3182 /lib/puppet/parser | |
parent | c5467cc507c004d75037d07c70633d1e743825af (diff) | |
download | puppet-stdlib-ef3d42f7bbdf95b21f46e580de309298cad300ea.tar.gz puppet-stdlib-ef3d42f7bbdf95b21f46e580de309298cad300ea.tar.bz2 |
Added basename() based on Ruby's File.basename
Based on dirname code. Includes RSpec tests and docs.
Diffstat (limited to 'lib/puppet/parser')
-rw-r--r-- | lib/puppet/parser/functions/basename.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/basename.rb b/lib/puppet/parser/functions/basename.rb new file mode 100644 index 0000000..f7e4438 --- /dev/null +++ b/lib/puppet/parser/functions/basename.rb @@ -0,0 +1,34 @@ +module Puppet::Parser::Functions + newfunction(:basename, :type => :rvalue, :doc => <<-EOS + Strips directory (and optional suffix) from a filename + EOS + ) do |arguments| + + if arguments.size < 1 then + raise(Puppet::ParseError, "basename(): No arguments given") + elsif arguments.size > 2 then + raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})") + else + + unless arguments[0].is_a?(String) + raise(Puppet::ParseError, 'basename(): Requires string as first argument') + end + + if arguments.size == 1 then + rv = File.basename(arguments[0]) + elsif arguments.size == 2 then + + unless arguments[1].is_a?(String) + raise(Puppet::ParseError, 'basename(): Requires string as second argument') + end + + rv = File.basename(arguments[0], arguments[1]) + end + + end + + return rv + end +end + +# vim: set ts=2 sw=2 et : |