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 meant to define the interface, and can't be used directly.
41 This is the engine of this module. It populates all the relevant variables
42 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 method should populate $r->objects from $r->{args}.
72 This is called on an model class representing a table and allows the
73 master model class to do any set-up required.
77 This is a list of all the columns in a table. You may also override
78 see also C<display_columns>
82 This is the name of the table.
86 sub class_of { die "This is an abstract method" }
87 sub setup_database { die "This is an abstract method" }
88 sub fetch_objects { die "This is an abstract method" }
96 If there is an object in C<$r-E<gt>objects>, then it should be edited
97 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
98 be created with those parameters, and put back into C<$r-E<gt>objects>.
99 The template should be changed to C<view>, or C<edit> if there were any
100 errors. A hash of errors will be passed to the template.
104 sub do_edit { die "This is an abstract method" }
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
125 sub list : Exported {
126 die "This is an abstract method";
129 sub view : Exported {
132 sub edit : Exported {
137 Also, see the exported commands in C<Maypole::Model::CDBI>.
139 =head1 Other overrides
141 Additionally, individual derived model classes may want to override the
144 =head2 display_columns
146 Returns a list of columns to display in the model. by default returns
147 all columns in alphabetical order. Override this in base classes to
148 change ordering, or elect not to show columns.
152 Same as display_columns, only for listings. Defaults to display_columns
156 Return a hash mapping column names with human-readable equivalents.
164 $col =~ s/_+(\w)?/ \U$1/g;
171 A description of the class to be passed to the template.
175 sub description { "A poorly defined class" }
179 should return true if a certain action is supported, or false otherwise.
180 Defaults to checking if the sub has the :Exported attribute.
185 my ( $self, $action ) = @_;
186 my $cv = $self->can($action);
188 my $attrs = join " ", attributes::get($cv);
190 warn "$action not exported" if Maypole->debug;
192 } unless $attrs =~ /\bExported\b/i;
198 This can go either in the master model class or in the individual
199 classes, and returns a list of has-many accessors. A brewery has many
200 beers, so C<BeerDB::Brewery> needs to return C<beers>.