X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole.pm;h=6c6ff52c0fc8ef0cad989205b9925382ccb03014;hb=ea4ae8a93a09e21354465c485471e5f10582b784;hp=f103eb2abf7db6187805ba5e5e1cf962c0b20fba;hpb=58417aa5f41b42076f991a81b403633bfcb94f39;p=maypole.git diff --git a/lib/Maypole.pm b/lib/Maypole.pm index f103eb2..6c6ff52 100644 --- a/lib/Maypole.pm +++ b/lib/Maypole.pm @@ -205,22 +205,26 @@ Called by C. This method builds the Maypole model hierarchy. A likely target for over-riding, if you need to build a customised model. +This method also ensures any code in custom model classes is loaded, so you +don't need to load them in the driver. + =cut sub setup_model { - my $calling_class = shift; + my $class = shift; - $calling_class = ref $calling_class if ref $calling_class; + $class = ref $class if ref $class; - my $config = $calling_class->config; + my $config = $class->config; $config->model || $config->model('Maypole::Model::CDBI'); $config->model->require or die sprintf "Couldn't load the model class %s: %s", $config->model, $@; - $config->model->setup_database($config, $calling_class, @_); + # among other things, this populates $config->classes + $config->model->setup_database($config, $class, @_); foreach my $subclass ( @{ $config->classes } ) { @@ -228,12 +232,12 @@ sub setup_model unshift @{ $subclass . "::ISA" }, $config->model; $config->model->adopt($subclass) if $config->model->can("adopt"); - - # TODO: I think we should also load these classes, in case there is any - # custom code. It would save the developer from needing to put - # lots of use MyApp::SomeTable statements in the driver, and should - # help eliminate some of those annoying silent errors if there's a - # syntax error. + + # Load custom model code, if it exists - nb this must happen after the + # unshift, to allow code attributes to work + eval "use $subclass"; + die "Error loading $subclass: $@" + if $@ and $@ !~ /Can\'t locate \S+ in \@INC/; } }