From 8bfa02244139fd278d940cf4722fe05f6dd7b0e7 Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Thu, 17 Jun 2010 05:54:36 -0500 Subject: [PATCH] add type with coersion for WebGUI::Config --- lib/WebGUI.pm | 13 +++---------- lib/WebGUI/Types.pm | 11 +++++++++++ t/Exception/app.t | 2 +- var/site.psgi | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/WebGUI.pm b/lib/WebGUI.pm index e862f7127..e24948317 100644 --- a/lib/WebGUI.pm +++ b/lib/WebGUI.pm @@ -25,6 +25,7 @@ use MooseX::NonMoose; use WebGUI::Config; use WebGUI::Pluggable; use WebGUI::Paths; +use WebGUI::Types; use Try::Tiny; extends 'Plack::Component'; @@ -48,18 +49,10 @@ These subroutines are available from this package: =cut has config => ( - is => 'rw', - isa => 'WebGUI::Config', -); -has site => ( is => 'ro', - isa => 'Str', required => 1, - trigger => sub { - my ($self, $site) = @_; - my $config = WebGUI::Config->new( $site ); - $self->config($config); - }, + isa => 'WebGUI::Type::Config', + coerce => 1, ); =head2 call( $env ) diff --git a/lib/WebGUI/Types.pm b/lib/WebGUI/Types.pm index 0fed3d275..25e70df5b 100644 --- a/lib/WebGUI/Types.pm +++ b/lib/WebGUI/Types.pm @@ -45,6 +45,17 @@ Similarly, if an undef value is applied, it is coerced into an empty arrayref. =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' => as 'ArrayRef' ; diff --git a/t/Exception/app.t b/t/Exception/app.t index f40f1d715..2c65a63d1 100644 --- a/t/Exception/app.t +++ b/t/Exception/app.t @@ -10,7 +10,7 @@ use HTTP::Request::Common; use Test::More tests => 9; use HTTP::Exception; -my $wg = WebGUI->new(site => WebGUI::Test->file); +my $wg = WebGUI->new(config => WebGUI::Test->config); my $regular_app = builder { enable '+WebGUI::Middleware::Session', config => $wg->config; diff --git a/var/site.psgi b/var/site.psgi index 32dd72e6f..1f97874d7 100644 --- a/var/site.psgi +++ b/var/site.psgi @@ -4,7 +4,7 @@ use Plack::App::File; use WebGUI; builder { - my $wg = WebGUI->new( site => $ENV{WEBGUI_CONFIG} ); + my $wg = WebGUI->new( config => $ENV{WEBGUI_CONFIG} ); my $config = $wg->config; enable 'Log4perl', category => $config->getFilename, conf => WebGUI::Paths->logConfig;