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;
19 my $obj = $class->retrieve( $r->{args}->[0] );
20 $r->objects( [$obj] ) if $obj;
21 $class->$method( $r, $obj, @{ $r->{args} } );
25 shift->display_columns;
34 Maypole::Model::Base - Base class for model classes
38 This is the base class for Maypole data models. This is an abstract class
39 meant to define the interface, and can't be used directly.
43 This is the engine of this module. It populates all the relevant variables
44 and calls the requested action.
46 Anyone subclassing this for a different database abstraction mechanism
47 needs to provide the following methods:
51 $model->setup_database($config, $namespace, @data)
53 Uses the user-defined data in C<@data> to specify a database- for
54 example, by passing in a DSN. The model class should open the database,
55 and create a class for each table in the database. These classes will
56 then be C<adopt>ed. It should also populate C<< $config->{tables} >> and
57 C<< $config->{classes} >> with the names of the classes and tables
58 respectively. The classes should be placed under the specified
59 namespace. For instance, C<beer> should be mapped to the class
64 $model->class_of($r, $table)
66 This maps between a table name and its associated class.
70 This turns an ID into an object of the appropriate class.
74 This is called on an model class representing a table and allows the
75 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" }
97 If there is an object in C<$r-E<gt>objects>, then it should be edited
98 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
99 be created with those parameters, and put back into C<$r-E<gt>objects>.
100 The template should be changed to C<view>, or C<edit> if there were any
101 errors. A hash of errors will be passed to the template.
105 sub do_edit { die "This is an abstract method" }
109 The C<list> method should fill C<< $r-> objects >> with all of the
110 objects in the class. You may want to page this using C<Data::Page> or
126 sub list : Exported {
127 die "This is an abstract method";
130 sub view : Exported {
133 sub edit : Exported {
138 Also, see the exported commands in C<Maypole::Model::CDBI>.
140 =head1 Other overrides
142 Additionally, individual derived model classes may want to override the
145 =head2 display_columns
147 Returns a list of columns to display in the model. by default returns
148 all columns in alphabetical order. Override this in base classes to
149 change ordering, or elect not to show columns.
153 Same as display_columns, only for listings. Defaults to display_columns
157 Return a hash mapping column names with human-readable equivalents.
165 $col =~ s/_+(\w)?/ \U$1/g;
172 A description of the class to be passed to the template.
176 sub description { "A poorly defined class" }
180 should return true if a certain action is supported, or false otherwise.
181 Defaults to checking if the sub has the :Exported attribute.
186 my ( $self, $action ) = @_;
187 my $cv = $self->can($action);
189 my $attrs = join " ", attributes::get($cv);
191 warn "$action not exported" if Maypole->debug;
193 } unless $attrs =~ /\bExported\b/i;
199 This can go either in the master model class or in the individual
200 classes, and returns a list of has-many accessors. A brewery has many
201 beers, so C<BeerDB::Brewery> needs to return C<beers>.