]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Maypole/Model/Base.pm
Added Maypole::Model::Base::is_public()
[maypole.git] / lib / Maypole / Model / Base.pm
index 908a2fca294a926e2f412c5daee3d2345479c9d7..b592eaab30f9439d257a2a04df8775c6a504d7e7 100644 (file)
@@ -1,21 +1,42 @@
-package Apache::MVC::Model::Base;
+package Maypole::Model::Base;
+
+use Maypole::Constants;
+use attributes ();
+
 our %remember;
-sub MODIFY_CODE_ATTRIBUTES { $remember{$_[1]} = $_[2]; () }
 
-sub FETCH_CODE_ATTRIBUTES { $remember{$_[1]} } 
-sub view :Exported { }
-sub edit :Exported { }
+sub MODIFY_CODE_ATTRIBUTES { $remember{ $_[1] } = $_[2]; () }
+
+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
 
-Apache::MVC::Model::Base - Base class for model classes
+Maypole::Model::Base - Base class for model classes
 
 =head1 DESCRIPTION
 
@@ -34,6 +55,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 C<adopt>ed. 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<beer> should be mapped to the class
+C<BeerDB::Beer>.
+
+=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 +91,9 @@ beers, so C<BeerDB::Brewery> needs to return C<beers>.
 
 =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<display_columns>, which is the list of columns you want to view, in
+the right order.
 
 =head2 table
 
@@ -71,11 +113,16 @@ 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
 
-Also, see the exported commands in C<Apache::MVC::Model::CDBI>.
+Also, see the exported commands in C<Maypole::Model::CDBI>.
 
 =head1 Other overrides
 
@@ -88,7 +135,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
 
@@ -98,5 +152,16 @@ A description of the class to be passed to the template.
 
 sub description { "A poorly defined class" }
 
-1;
+sub is_public {
+    my ( $self, $action ) = @_;
+    my $cv = $self->can($action);
+    return DECLINED() unless $cv;
+    my $attrs = join " ", attributes::get($cv);
+    do {
+        warn "$action not exported" if Maypole->debug;
+        return DECLINED();
+    } unless $attrs =~ /\bExported\b/i;
+    return OK;
+}
 
+1;