1 package Maypole::Model::Base;
3 use Maypole::Constants;
8 sub MODIFY_CODE_ATTRIBUTES { $remember{ $_[1] } = $_[2]; () }
10 sub FETCH_CODE_ATTRIBUTES { $remember{ $_[1] } }
13 my ( $class, $r ) = @_;
14 my $method = $r->action;
15 return if $r->{template}; # Authentication has set this, we're done.
17 $r->{template} = $method;
18 $r->objects([ $class->fetch_objects($r) ]);
19 $class->$method( $r, $obj, @{ $r->{args} } );
23 shift->display_columns;
32 Maypole::Model::Base - Base class for model classes
36 This is the base class for Maypole data models. This is an abstract class
37 that defines the interface, and can't be used directly.
41 This is the engine of this module. Given the request object, it populates
42 all the relevant variables and calls the requested action.
44 Anyone subclassing this for a different database abstraction mechanism
45 needs to provide the following methods:
49 $model->setup_database($config, $namespace, @data)
51 Uses the user-defined data in C<@data> to specify a database- for
52 example, by passing in a DSN. The model class should open the database,
53 and create a class for each table in the database. These classes will
54 then be C<adopt>ed. It should also populate C<< $config->tables >> and
55 C<< $config->classes >> with the names of the classes and tables
56 respectively. The classes should be placed under the specified
57 namespace. For instance, C<beer> should be mapped to the class
62 $model->class_of($r, $table)
64 This maps between a table name and its associated class.
68 This class method is passed a request object and is expected to return an
69 object of the appropriate table class from information stored in the request
74 This class method is passed the name of a model class that represensts a table
75 and allows the master model class to do any set-up required.
79 This is a list of all the columns in a table. You may also override
80 see also C<display_columns>
84 This is the name of the table.
88 sub class_of { die "This is an abstract method" }
89 sub setup_database { die "This is an abstract method" }
90 sub fetch_objects { die "This is an abstract method" }
98 If there is an object in C<$r-E<gt>objects>, then it should be edited
99 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
100 be created with those parameters, and put back into C<$r-E<gt>objects>.
101 The template should be changed to C<view>, or C<edit> if there were any
102 errors. A hash of errors will be passed to the template.
106 sub do_edit { die "This is an abstract method" }
110 The C<list> method should fill C<$r-E<gt>objects> with all of the
111 objects in the class. You may want to page this using C<Data::Page> or
127 sub list : Exported {
128 die "This is an abstract method";
131 sub view : Exported {
134 sub edit : Exported {
139 Also, see the exported commands in C<Maypole::Model::CDBI>.
141 =head1 Other overrides
143 Additionally, individual derived model classes may want to override the
146 =head2 display_columns
148 Returns a list of columns to display in the model. By default returns
149 all columns in alphabetical order. Override this in base classes to
150 change ordering, or elect not to show columns.
154 Same as display_columns, only for listings. Defaults to display_columns
158 Return a hash mapping column names with human-readable equivalents.
166 $col =~ s/_+(\w)?/ \U$1/g;
173 A description of the class to be passed to the template.
177 sub description { "A poorly defined class" }
181 should return true if a certain action is supported, or false otherwise.
182 Defaults to checking if the sub has the C<:Exported> attribute.
187 my ( $self, $action ) = @_;
188 my $cv = $self->can($action);
190 my $attrs = join " ", attributes::get($cv);
192 warn "$action not exported" if Maypole->debug;
194 } unless $attrs =~ /\bExported\b/i;
200 This can go either in the master model class or in the individual
201 classes, and returns a list of has-many accessors. A brewery has many
202 beers, so C<BeerDB::Brewery> needs to return C<beers>.