1 package Maypole::Model::Base;
4 use Maypole::Constants;
7 # don't know why this is a global - drb
10 sub MODIFY_CODE_ATTRIBUTES {
11 shift; # class name not used
12 my ($coderef, @attrs) = @_;
13 $remember{$coderef} = [$coderef, \@attrs];
15 # previous version took care to return an empty array, not sure why,
16 # but shall cargo cult it until know better
20 sub FETCH_CODE_ATTRIBUTES { @{ $remember{$_[1]}->[1] || [] } }
24 for my $key (keys %remember) {
25 my $value = delete $remember{$key};
27 $remember{$key} = $value;
32 my ( $class, $r ) = @_;
33 my $method = $r->action;
35 $r->{template} = $method;
36 my $obj = $class->fetch_objects($r);
37 $r->objects([$obj]) if $obj;
39 $class->$method( $r, $obj, @{ $r->{args} } );
43 shift->display_columns;
52 Maypole::Model::Base - Base class for model classes
56 This is the base class for Maypole data models. This is an abstract class
57 that defines the interface, and can't be used directly.
61 This is the engine of this module. Given the request object, it populates
62 all the relevant variables and calls the requested action.
64 Anyone subclassing this for a different database abstraction mechanism
65 needs to provide the following methods:
69 $model->setup_database($config, $namespace, @data)
71 Uses the user-defined data in C<@data> to specify a database- for
72 example, by passing in a DSN. The model class should open the database,
73 and create a class for each table in the database. These classes will
74 then be C<adopt>ed. It should also populate C<< $config->tables >> and
75 C<< $config->classes >> with the names of the classes and tables
76 respectively. The classes should be placed under the specified
77 namespace. For instance, C<beer> should be mapped to the class
82 $model->class_of($r, $table)
84 This maps between a table name and its associated class.
88 This class method is passed a request object and is expected to return an
89 object of the appropriate table class from information stored in the request
94 This class method is passed the name of a model class that represensts a table
95 and allows the master model class to do any set-up required.
99 This is a list of all the columns in a table. You may also override
100 see also C<display_columns>
104 This is the name of the table.
108 sub class_of { die "This is an abstract method" }
109 sub setup_database { die "This is an abstract method" }
110 sub fetch_objects { die "This is an abstract method" }
118 If there is an object in C<$r-E<gt>objects>, then it should be edited
119 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
120 be created with those parameters, and put back into C<$r-E<gt>objects>.
121 The template should be changed to C<view>, or C<edit> if there were any
122 errors. A hash of errors will be passed to the template.
126 sub do_edit { die "This is an abstract method" }
130 The C<list> method should fill C<$r-E<gt>objects> with all of the
131 objects in the class. You may want to page this using C<Data::Page> or
144 Empty Action, calls list if provided with a table.
150 sub list : Exported {
151 die "This is an abstract method";
154 sub view : Exported {
157 sub edit : Exported {
160 sub index : Exported {
161 my ( $self, $r ) = @_;
163 $r->template("list");
164 return $self->list($r);
170 Also, see the exported commands in C<Maypole::Model::CDBI>.
172 =head1 Other overrides
174 Additionally, individual derived model classes may want to override the
177 =head2 display_columns
179 Returns a list of columns to display in the model. By default returns
180 all columns in alphabetical order. Override this in base classes to
181 change ordering, or elect not to show columns.
185 Same as display_columns, only for listings. Defaults to display_columns
189 Return a hash mapping column names with human-readable equivalents.
197 $col =~ s/_+(\w)?/ \U$1/g;
204 should return true if a certain action is supported, or false otherwise.
205 Defaults to checking if the sub has the C<:Exported> attribute.
210 my ( $self, $action, $attrs ) = @_;
211 my $cv = $self->can($action);
212 warn "is_public failed . action is $action. self is $self" and return 0 unless $cv;
214 my %attrs = (ref $attrs) ? %$attrs : map {$_ => 1} $self->method_attrs($action,$cv) ;
217 warn "is_public failed. $action not exported. attributes are : ", %attrs;
219 } unless $attrs{Exported};
224 =head2 add_model_superclass
226 Adds model as superclass to model classes (if necessary)
230 sub add_model_superclass { return; }
234 Returns the list of attributes defined for a method. Maypole itself only
235 defines the C<Exported> attribute.
240 my ($class, $method, $cv) = @_;
242 $cv ||= $class->can($method);
246 my @attrs = attributes::get($cv);
253 This can go either in the master model class or in the individual
254 classes, and returns a list of has-many accessors. A brewery has many
255 beers, so C<BeerDB::Brewery> needs to return C<beers>.
267 L<Maypole>, L<Maypole::Model::CDBI>.
271 Maypole is currently maintained by Aaron Trevena.
273 =head1 AUTHOR EMERITUS
275 Simon Cozens, C<simon#cpan.org>
277 Simon Flack maintained Maypole from 2.05 to 2.09
279 Sebastian Riedel, C<sri#oook.de> maintained Maypole from 1.99_01 to 2.04
283 You may distribute this code under the same terms as Perl itself.