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] );
21 $r->objects( [$obj] );
22 shift @{ $r->{args} };
24 $class->$method( $r, $obj, @{ $r->{args} } );
28 shift->display_columns;
37 Maypole::Model::Base - Base class for model classes
41 This is the base class for Maypole data models. This is an abstract class
42 meant to define the interface, and can't be used directly.
46 This is the engine of this module. It populates all the relevant variables
47 and calls the requested action.
49 Anyone subclassing this for a different database abstraction mechanism
50 needs to provide the following methods:
54 $model->setup_database($config, $namespace, @data)
56 Uses the user-defined data in C<@data> to specify a database- for
57 example, by passing in a DSN. The model class should open the database,
58 and create a class for each table in the database. These classes will
59 then be C<adopt>ed. It should also populate C<< $config->{tables} >> and
60 C<< $config->{classes} >> with the names of the classes and tables
61 respectively. The classes should be placed under the specified
62 namespace. For instance, C<beer> should be mapped to the class
67 $model->class_of($r, $table)
69 This maps between a table name and its associated class.
73 This turns an ID into an object of the appropriate class.
77 This is called on an model class representing a table and allows the
78 master model class to do any set-up required.
82 This is a list of all the columns in a table. You may also override
83 see also C<display_columns>
87 This is the name of the table.
91 sub class_of { die "This is an abstract method" }
92 sub setup_database { die "This is an abstract method" }
100 If there is an object in C<$r-E<gt>objects>, then it should be edited
101 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
102 be created with those parameters, and put back into C<$r-E<gt>objects>.
103 The template should be changed to C<view>, or C<edit> if there were any
104 errors. A hash of errors will be passed to the template.
108 sub do_edit { die "This is an abstract method" }
113 The C<list> method should fill C<< $r-> objects >> with all of the
114 objects in the class. You may want to page this using C<Data::Page> or
131 sub list : Exported {
132 die "This is an abstract method";
135 sub view : Exported {
138 sub edit : Exported {
143 Also, see the exported commands in C<Maypole::Model::CDBI>.
145 =head1 Other overrides
147 Additionally, individual derived model classes may want to override the
150 =head2 display_columns
152 Returns a list of columns to display in the model. by default returns
153 all columns in alphabetical order. Override this in base classes to
154 change ordering, or elect not to show columns.
158 Same as display_columns, only for listings. Defaults to display_columns
162 Return a hash mapping column names with human-readable equivalents.
170 $col =~ s/_+(\w)?/ \U$1/g;
177 A description of the class to be passed to the template.
181 sub description { "A poorly defined class" }
185 should return true if a certain action is supported, or false otherwise.
186 Defaults to checking if the sub has the :Exported attribute.
191 my ( $self, $action ) = @_;
192 my $cv = $self->can($action);
194 my $attrs = join " ", attributes::get($cv);
196 warn "$action not exported" if Maypole->debug;
198 } unless $attrs =~ /\bExported\b/i;
204 This can go either in the master model class or in the individual
205 classes, and returns a list of has-many accessors. A brewery has many
206 beers, so C<BeerDB::Brewery> needs to return C<beers>.