1 package Maypole::Model::Base;
3 use Maypole::Constants;
8 sub MODIFY_CODE_ATTRIBUTES { $remember{ $_[1] } = $_[2]; () }
10 sub FETCH_CODE_ATTRIBUTES { $remember{ $_[1] } }
19 my ( $class, $r ) = @_;
20 my $method = $r->action;
21 return if $r->{template}; # Authentication has set this, we're done.
23 $r->{template} = $method;
25 my $obj = $class->retrieve( $r->{args}->[0] );
27 $r->objects( [$obj] );
28 shift @{ $r->{args} };
30 $class->$method( $r, $obj, @{ $r->{args} } );
39 Maypole::Model::Base - Base class for model classes
43 Anyone subclassing this for a different database abstraction mechanism
44 needs to provide the following methods:
48 If there is an object in C<$r-E<gt>objects>, then it should be edited
49 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
50 be created with those parameters, and put back into C<$r-E<gt>objects>.
51 The template should be changed to C<view>, or C<edit> if there were any
52 errors. A hash of errors will be passed to the template.
56 sub do_edit { die "This is an abstract method" }
60 $model->setup_database($config, $namespace, @data)
62 Uses the user-defined data in C<@data> to specify a database- for
63 example, by passing in a DSN. The model class should open the database,
64 and create a class for each table in the database. These classes will
65 then be C<adopt>ed. It should also populate C<< $config->{tables} >> and
66 C<< $config->{classes} >> with the names of the classes and tables
67 respectively. The classes should be placed under the specified
68 namespace. For instance, C<beer> should be mapped to the class
73 $model->class_of($r, $table)
75 This maps between a table name and its associated class.
79 This turns an ID into an object of the appropriate class.
83 This is called on an model class representing a table and allows the
84 master model class to do any set-up required.
88 This can go either in the master model class or in the individual
89 classes, and returns a list of has-many accessors. A brewery has many
90 beers, so C<BeerDB::Brewery> needs to return C<beers>.
94 This is a list of all the columns in a table. You may also override
95 C<display_columns>, which is the list of columns you want to view, in
100 This is the name of the table.
108 The C<list> method should fill C<< $r-> objects >> with all of the
109 objects in the class. You may want to page this using C<Data::Page> or
116 sub class_of { die "This is an abstract method" }
117 sub setup_database { die "This is an abstract method" }
119 sub list : Exported {
120 die "This is an abstract method";
125 Also, see the exported commands in C<Maypole::Model::CDBI>.
127 =head1 Other overrides
129 Additionally, individual derived model classes may want to override the
134 Return a hash mapping column names with human-readable equivalents.
142 $col =~ s/_+(\w)?/ \U$1/g;
149 A description of the class to be passed to the template.
153 sub description { "A poorly defined class" }
156 my ( $self, $action ) = @_;
157 my $cv = $self->can($action);
159 my $attrs = join " ", attributes::get($cv);
161 warn "$action not exported" if Maypole->debug;
163 } unless $attrs =~ /\bExported\b/i;