X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole%2FModel%2FBase.pm;h=804f23519d0a808b1000d621660fe2b010d4f0fc;hb=ff62bc5d741a81146439183b3e3555fc0d5d6847;hp=0119d491e19018b7d3ff9608564b8aaa0e6e967c;hpb=0b2bde3134a6580d38c4f555fa5a076ed24740cf;p=maypole.git diff --git a/lib/Maypole/Model/Base.pm b/lib/Maypole/Model/Base.pm index 0119d49..804f235 100644 --- a/lib/Maypole/Model/Base.pm +++ b/lib/Maypole/Model/Base.pm @@ -1,16 +1,32 @@ package Maypole::Model::Base; our %remember; -sub MODIFY_CODE_ATTRIBUTES { $remember{$_[1]} = $_[2]; () } +sub MODIFY_CODE_ATTRIBUTES { $remember{ $_[1] } = $_[2]; () } -sub FETCH_CODE_ATTRIBUTES { $remember{$_[1]} } -sub view :Exported { } -sub edit :Exported { } +sub FETCH_CODE_ATTRIBUTES { $remember{ $_[1] } } + +sub view : Exported { +} + +sub edit : Exported { +} sub process { - my ($class, $r) = @_; - $r->template( my $method = $r->action ); - $r->objects([ $class->retrieve(shift @{$r->{args}}) ]); - $class->$method($r); + my ( $class, $r ) = @_; + my $method = $r->action; + return if $r->{template}; # Authentication has set this, we're done. + + $r->{template} = $method; + $r->objects( [] ); + my $obj = $class->retrieve( $r->{args}->[0] ); + if ($obj) { + $r->objects( [$obj] ); + shift @{ $r->{args} }; + } + $class->$method( $r, $obj, @{ $r->{args} } ); +} + +sub display_columns { + sort shift->columns; } =head1 NAME @@ -34,6 +50,25 @@ errors. A hash of errors will be passed to the template. sub do_edit { die "This is an abstract method" } +=head2 setup_database + + $model->setup_database($config, $namespace, @data) + +Uses the user-defined data in C<@data> to specify a database- for +example, by passing in a DSN. The model class should open the database, +and create a class for each table in the database. These classes will +then be Ced. It should also populate C<< $config->{tables} >> and +C<< $config->{classes} >> with the names of the classes and tables +respectively. The classes should be placed under the specified +namespace. For instance, C should be mapped to the class +C. + +=head2 class_of + + $model->class_of($r, $table) + +This maps between a table name and its associated class. + =head2 retrieve This turns an ID into an object of the appropriate class. @@ -51,7 +86,9 @@ beers, so C needs to return C. =head2 columns -This is a list of the columns in a table. +This is a list of all the columns in a table. You may also override +C, which is the list of columns you want to view, in +the right order. =head2 table @@ -71,7 +108,12 @@ similar. =cut -sub list :Exported { die "This is an abstract method" }; +sub class_of { die "This is an abstract method" } +sub setup_database { die "This is an abstract method" } + +sub list : Exported { + die "This is an abstract method"; +} =pod @@ -88,7 +130,14 @@ Return a hash mapping column names with human-readable equivalents. =cut -sub column_names { my $class = shift; map { $_ => ucfirst $_ } $class->columns } +sub column_names { + my $class = shift; + map { + my $col = $_; + $col =~ s/_+(\w)?/ \U$1/g; + $_ => ucfirst $col + } $class->columns; +} =head2 description