X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole.pm;h=29c8917d81022b5ed699c76c8f0b196677fd6d3f;hb=5f530b5f17106319faa2f437a567332c86bf6a2c;hp=53e9be59cbe4afb0dcda50a24bfb1da219a54f03;hpb=fc6bc7a48e5d5d7eaedb22497b1c6c8b8193850c;p=maypole.git diff --git a/lib/Maypole.pm b/lib/Maypole.pm index 53e9be5..29c8917 100644 --- a/lib/Maypole.pm +++ b/lib/Maypole.pm @@ -4,13 +4,14 @@ use attributes (); use UNIVERSAL::require; use strict; use warnings; +use Maypole::Config; our $VERSION = "1.8"; __PACKAGE__->mk_classdata($_) for qw( config init_done view_object ); __PACKAGE__->mk_accessors( qw( ar params query objects model_class args action template ) ); -__PACKAGE__->config( {} ); +__PACKAGE__->config( Maypole::Config->new() ); __PACKAGE__->init_done(0); use Maypole::Constants; @@ -27,26 +28,26 @@ sub setup { sub { Maypole::handler( $calling_class, @_ ) }; } my $config = $calling_class->config; - $config->{model} ||= "Maypole::Model::CDBI"; - $config->{model}->require; - die "Couldn't load the model class $config->{model}: $@" if $@; - $config->{model}->setup_database( $config, $calling_class, @_ ); - for my $subclass ( @{ $config->{classes} } ) { + $config->model || $config->model("Maypole::Model::CDBI"); + $config->model->require; + die "Couldn't load the model class $config->model: $@" if $@; + $config->model->setup_database( $config, $calling_class, @_ ); + for my $subclass ( @{ $config->classes } ) { no strict 'refs'; - unshift @{ $subclass . "::ISA" }, $config->{model}; - $config->{model}->adopt($subclass) - if $config->{model}->can("adopt"); + unshift @{ $subclass . "::ISA" }, $config->model; + $config->model->adopt($subclass) + if $config->model->can("adopt"); } } sub init { my $class = shift; my $config = $class->config; - $config->{view} ||= "Maypole::View::TT"; - $config->{view}->require; - die "Couldn't load the view class $config->{view}: $@" if $@; - $config->{display_tables} ||= [ @{ $class->config->{tables} } ]; - $class->view_object( $class->config->{view}->new ); + $config->view || $config->view("Maypole::View::TT"); + $config->view->require; + die "Couldn't load the view class ".$config->view.": $@" if $@; + $config->display_tables || $config->display_tables([ $class->config->tables ]); + $class->view_object( $class->config->view->new ); $class->init_done(1); } @@ -67,7 +68,7 @@ sub handler { sub handler_guts { my $r = shift; - $r->model_class( $r->config->{model}->class_of( $r, $r->{table} ) ); + $r->model_class( $r->config->model->class_of( $r, $r->{table} ) ); my $applicable = $r->is_applicable; unless ( $applicable == OK ) { @@ -83,7 +84,7 @@ sub handler_guts { if ( my $error = $@ ) { $status = $r->call_exception($error); if ( $status != OK ) { - warn "caught model error: $error"; + warn "caught authenticate error: $error"; return $r->debug ? $r->view_object->error( $r, $error ) : ERROR; } } @@ -106,7 +107,15 @@ sub handler_guts { } } if ( !$r->{output} ) { # You might want to do it yourself - return $r->view_object->process($r); + eval { $status = $r->view_object->process($r) }; + if ( my $error = $@ ) { + $status = $r->call_exception($error); + if ( $status != OK ) { + warn "caught view error: $error"; + return $r->debug ? $r->view_object->error( $r, $error ) : ERROR; + } + } + return $status; } else { return OK; } } @@ -114,13 +123,13 @@ sub handler_guts { sub is_applicable { my $self = shift; my $config = $self->config; - $config->{ok_tables} ||= $config->{display_tables}; - $config->{ok_tables} = { map { $_ => 1 } @{ $config->{ok_tables} } } - if ref $config->{ok_tables} eq "ARRAY"; + $config->ok_tables || $config->ok_tables($config->display_tables); + $config->ok_tables ({ map { $_ => 1 } @{ $config->ok_tables } }) + if ref $config->ok_tables eq "ARRAY"; warn "We don't have that table ($self->{table})" if $self->debug - and not $config->{ok_tables}{ $self->{table} }; - return DECLINED() unless exists $config->{ok_tables}{ $self->{table} }; + and not $config->ok_tables->{ $self->{table} }; + return DECLINED() unless exists $config->ok_tables->{ $self->{table} }; # Does the action method exist? my $cv = $self->model_class->can( $self->{action} ); @@ -212,7 +221,7 @@ this: package ProductDatabase; use base 'Apache::MVC'; __PACKAGE__->set_database("dbi:mysql:products"); - ProductDatabase->config->{uri_base} = "http://your.site/catalogue/"; + ProductDatabase->config->uri_base = "http://your.site/catalogue/"; ProductDatabase::Product->has_a("category" => ProductDatabase::Category); # ... @@ -270,7 +279,7 @@ subclass the model class, and configure your class slightly differently: Then your top-level application package should change the model class: (Before calling C) - ProductDatabase->config->{model} = "ProductDatabase::Model"; + ProductDatabase->config->model("ProductDatabase::Model"); (The C<:Exported> attribute means that the method can be called via the URL C/supersearch/...>.)