Patch to add an __ENV(foo)__ macro to configloader from Stuart Watt, from -devel...
Tomas Doran [Sun, 28 Jun 2009 13:31:18 +0000 (13:31 +0000)]
Changes
lib/Catalyst/Plugin/ConfigLoader.pm
t/21-mock_load_env.t
t/mockapp/mockapp.pl

diff --git a/Changes b/Changes
index edbcf79..878709f 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
 Revision history for Perl extension Catalyst::Plugin::ConfigLoader.
 
 0.24  XXX
+    - Add an __ENV(foo)__ macro + tests (Stuart Watt)
     - Document CATALYST_CONFIG_LOCAL_SUFFIX and MYAPP_LOCAL_CONFIG_SUFFIX
       much better (Louis Erickson)
     - Fix so that having CATALYST_CONFIG_LOCAL_SUFFIX set in $ENV{} doesn't
index c1618e3..8fe0e71 100644 (file)
@@ -280,6 +280,8 @@ default macros:
 
 =item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
 
+=item * C<__ENV(foo)__> - replaced with the value of C<$ENV{foo}>
+
 =item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
 
 =item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
@@ -305,6 +307,17 @@ sub config_substitutions {
     my $subs = $c->config->{ 'Plugin::ConfigLoader' }->{ substitutions }
         || {};
     $subs->{ HOME }    ||= sub { shift->path_to( '' ); };
+    $subs->{ ENV }    ||= 
+        sub { 
+            my ( $c, $v ) = @_; 
+            if (! defined($ENV{$v})) {
+                Catalyst::Exception->throw( message =>
+                    "Missing environment variable: $v" );
+                return "";
+            } else {
+                return $ENV{ $v }; 
+            }
+        };
     $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
     $subs->{ literal } ||= sub { return $_[ 1 ]; };
     my $subsre = join( '|', keys %$subs );
@@ -329,6 +342,8 @@ development of this module:
 
 =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt> - L<Data::Visitor> integration
 
+=item * Stuart Watt - Addition of ENV macro.
+
 =back
 
 Work to this module has been generously sponsored by: 
index 9494712..151ec12 100644 (file)
@@ -1,6 +1,6 @@
 package MockApp;
 
-use Test::More tests => 9;
+use Test::More tests => 10;
 
 use Cwd;
 local %ENV;
@@ -23,3 +23,4 @@ is( __PACKAGE__->config->{ 'Model::Baz' }->{ another },  'new key' );
 is( __PACKAGE__->config->{ 'view' },                     'View::TT::New' );
 is( __PACKAGE__->config->{ 'foo_sub' },                  'x-y' );
 is( __PACKAGE__->config->{ 'literal_macro' },            '__DATA__' );
+is( __PACKAGE__->config->{ 'environment_macro' },        $ENV{ CATALYST_HOME }.'/mockapp.pl' );
index 15b46c5..a800fce 100644 (file)
@@ -4,5 +4,6 @@
     'Model::Baz'      => { qux => 'xyzzy' },
     foo_sub           => '__foo(x,y)__',
     literal_macro     => '__literal(__DATA__)__',
+    environment_macro => '__ENV(CATALYST_HOME)__/mockapp.pl',
     Plugin            => { Zot => { zoot => 'zooot' } },
 }