add type with coersion for WebGUI::Config

This commit is contained in:
Graham Knop 2010-06-17 05:54:36 -05:00
parent 19c2c5fa1c
commit 8bfa022441
4 changed files with 16 additions and 12 deletions

View file

@ -25,6 +25,7 @@ use MooseX::NonMoose;
use WebGUI::Config; use WebGUI::Config;
use WebGUI::Pluggable; use WebGUI::Pluggable;
use WebGUI::Paths; use WebGUI::Paths;
use WebGUI::Types;
use Try::Tiny; use Try::Tiny;
extends 'Plack::Component'; extends 'Plack::Component';
@ -48,18 +49,10 @@ These subroutines are available from this package:
=cut =cut
has config => ( has config => (
is => 'rw',
isa => 'WebGUI::Config',
);
has site => (
is => 'ro', is => 'ro',
isa => 'Str',
required => 1, required => 1,
trigger => sub { isa => 'WebGUI::Type::Config',
my ($self, $site) = @_; coerce => 1,
my $config = WebGUI::Config->new( $site );
$self->config($config);
},
); );
=head2 call( $env ) =head2 call( $env )

View file

@ -45,6 +45,17 @@ Similarly, if an undef value is applied, it is coerced into an empty arrayref.
=cut =cut
subtype 'WebGUI::Type::Config'
=> as class_type('WebGUI::Config');
coerce 'WebGUI::Type::Config'
=> from Str
=> via {
require WebGUI::Config;
WebGUI::Config->new($_)
}
;
subtype 'WebGUI::Type::JSONArray' subtype 'WebGUI::Type::JSONArray'
=> as 'ArrayRef' => as 'ArrayRef'
; ;

View file

@ -10,7 +10,7 @@ use HTTP::Request::Common;
use Test::More tests => 9; use Test::More tests => 9;
use HTTP::Exception; use HTTP::Exception;
my $wg = WebGUI->new(site => WebGUI::Test->file); my $wg = WebGUI->new(config => WebGUI::Test->config);
my $regular_app = builder { my $regular_app = builder {
enable '+WebGUI::Middleware::Session', config => $wg->config; enable '+WebGUI::Middleware::Session', config => $wg->config;

View file

@ -4,7 +4,7 @@ use Plack::App::File;
use WebGUI; use WebGUI;
builder { builder {
my $wg = WebGUI->new( site => $ENV{WEBGUI_CONFIG} ); my $wg = WebGUI->new( config => $ENV{WEBGUI_CONFIG} );
my $config = $wg->config; my $config = $wg->config;
enable 'Log4perl', category => $config->getFilename, conf => WebGUI::Paths->logConfig; enable 'Log4perl', category => $config->getFilename, conf => WebGUI::Paths->logConfig;