From: Tomas Doran Date: Sun, 28 Jun 2009 13:31:18 +0000 (+0000) Subject: Patch to add an __ENV(foo)__ macro to configloader from Stuart Watt, from -devel... X-Git-Tag: v0.24~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-ConfigLoader.git;a=commitdiff_plain;h=24e563f5b0bcdd4c8a4385dd03aaf69b09b75836 Patch to add an __ENV(foo)__ macro to configloader from Stuart Watt, from -devel list --- diff --git a/Changes b/Changes index edbcf79..878709f 100644 --- 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 diff --git a/lib/Catalyst/Plugin/ConfigLoader.pm b/lib/Catalyst/Plugin/ConfigLoader.pm index c1618e3..8fe0e71 100644 --- a/lib/Catalyst/Plugin/ConfigLoader.pm +++ b/lib/Catalyst/Plugin/ConfigLoader.pm @@ -280,6 +280,8 @@ default macros: =item * C<__HOME__> - replaced with C<$c-Epath_to('')> +=item * C<__ENV(foo)__> - replaced with the value of C<$ENV{foo}> + =item * C<__path_to(foo/bar)__> - replaced with C<$c-Epath_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 Edkamholz@cpan.orgE - L integration +=item * Stuart Watt - Addition of ENV macro. + =back Work to this module has been generously sponsored by: diff --git a/t/21-mock_load_env.t b/t/21-mock_load_env.t index 9494712..151ec12 100644 --- a/t/21-mock_load_env.t +++ b/t/21-mock_load_env.t @@ -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' ); diff --git a/t/mockapp/mockapp.pl b/t/mockapp/mockapp.pl index 15b46c5..a800fce 100644 --- a/t/mockapp/mockapp.pl +++ b/t/mockapp/mockapp.pl @@ -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' } }, }