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 my $obj = $class->fetch_objects($r);
19 $r->objects([$obj]) if $obj;
20 $class->$method( $r, $obj, @{ $r->{args} } );
24 shift->display_columns;
33 Maypole::Model::Base - Base class for model classes
37 This is the base class for Maypole data models. This is an abstract class
38 that defines the interface, and can't be used directly.
42 This is the engine of this module. Given the request object, it populates
43 all the relevant variables and calls the requested action.
45 Anyone subclassing this for a different database abstraction mechanism
46 needs to provide the following methods:
50 $model->setup_database($config, $namespace, @data)
52 Uses the user-defined data in C<@data> to specify a database- for
53 example, by passing in a DSN. The model class should open the database,
54 and create a class for each table in the database. These classes will
55 then be C<adopt>ed. It should also populate C<< $config->tables >> and
56 C<< $config->classes >> with the names of the classes and tables
57 respectively. The classes should be placed under the specified
58 namespace. For instance, C<beer> should be mapped to the class
63 $model->class_of($r, $table)
65 This maps between a table name and its associated class.
69 This class method is passed a request object and is expected to return an
70 object of the appropriate table class from information stored in the request
75 This class method is passed the name of a model class that represensts a table
76 and allows the master model class to do any set-up required.
80 This is a list of all the columns in a table. You may also override
81 see also C<display_columns>
85 This is the name of the table.
89 sub class_of { die "This is an abstract method" }
90 sub setup_database { die "This is an abstract method" }
91 sub fetch_objects { die "This is an abstract method" }
99 If there is an object in C<$r-E<gt>objects>, then it should be edited
100 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
101 be created with those parameters, and put back into C<$r-E<gt>objects>.
102 The template should be changed to C<view>, or C<edit> if there were any
103 errors. A hash of errors will be passed to the template.
107 sub do_edit { die "This is an abstract method" }
111 The C<list> method should fill C<$r-E<gt>objects> with all of the
112 objects in the class. You may want to page this using C<Data::Page> or
128 sub list : Exported {
129 die "This is an abstract method";
132 sub view : Exported {
135 sub edit : Exported {
140 Also, see the exported commands in C<Maypole::Model::CDBI>.
142 =head1 Other overrides
144 Additionally, individual derived model classes may want to override the
147 =head2 display_columns
149 Returns a list of columns to display in the model. By default returns
150 all columns in alphabetical order. Override this in base classes to
151 change ordering, or elect not to show columns.
155 Same as display_columns, only for listings. Defaults to display_columns
159 Return a hash mapping column names with human-readable equivalents.
167 $col =~ s/_+(\w)?/ \U$1/g;
174 should return true if a certain action is supported, or false otherwise.
175 Defaults to checking if the sub has the C<:Exported> attribute.
180 my ( $self, $action ) = @_;
181 my $cv = $self->can($action);
183 my $attrs = join " ", (attributes::get($cv) || ());
185 warn "$action not exported" if Maypole->debug;
187 } unless $attrs =~ /\bExported\b/i;
193 This can go either in the master model class or in the individual
194 classes, and returns a list of has-many accessors. A brewery has many
195 beers, so C<BeerDB::Brewery> needs to return C<beers>.