added C::Utils::class2tempdir()
Christian Hansen [Fri, 24 Jun 2005 13:57:01 +0000 (13:57 +0000)]
lib/Catalyst/Utils.pm

index e329648..15e4a68 100644 (file)
@@ -3,6 +3,7 @@ package Catalyst::Utils;
 use strict;
 use attributes ();
 use Catalyst::Exception;
+use File::Spec;
 use HTTP::Request;
 use Path::Class;
 use URI;
@@ -114,6 +115,36 @@ sub class2prefix {
     return $prefix;
 }
 
+=item class2tempdir( $class [, $create ] );
+
+Returns a tempdir for class. If create is true it will try to create the path.
+
+    My::App becomes /tmp/my/app
+    My::App::C::Foo::Bar becomes /tmp/my/app/c/foo/bar
+
+=cut
+
+sub class2tempdir {
+    my $class  = shift || '';
+    my $create = shift || 0;
+    my @parts  = split '::', lc $class;
+
+    my $tmpdir = dir( File::Spec->tmpdir, @parts )->cleanup;
+
+    if ( $create && ! -e $tmpdir ) {
+
+        eval { $tmpdir->mkpath };
+
+        if ( $@ ) {
+            Catalyst::Exception->throw(
+                message => qq/Couldn't create tmpdir '$tmpdir', "$@"/
+            );
+        }
+    }
+
+    return $tmpdir->stringify;
+}
+
 =item home($class)
 
 Returns home directory for given class.