1 package Maypole::Model::Base;
4 use Maypole::Constants;
7 # don't know why this is a global - drb
10 sub MODIFY_CODE_ATTRIBUTES
12 shift; # class name not used
13 my ($coderef, @attrs) = @_;
15 $remember{$coderef} = \@attrs;
17 # previous version took care to return an empty array, not sure why,
18 # but shall cargo cult it until know better
22 sub FETCH_CODE_ATTRIBUTES { @{ $remember{$_[1]} || [] } }
25 my ( $class, $r ) = @_;
26 my $method = $r->action;
27 return if $r->{template}; # Authentication has set this, we're done.
29 $r->{template} = $method;
30 my $obj = $class->fetch_objects($r);
31 $r->objects([$obj]) if $obj;
33 $class->$method( $r, $obj, @{ $r->{args} } );
37 shift->display_columns;
46 Maypole::Model::Base - Base class for model classes
50 This is the base class for Maypole data models. This is an abstract class
51 that defines the interface, and can't be used directly.
55 This is the engine of this module. Given the request object, it populates
56 all the relevant variables and calls the requested action.
58 Anyone subclassing this for a different database abstraction mechanism
59 needs to provide the following methods:
63 $model->setup_database($config, $namespace, @data)
65 Uses the user-defined data in C<@data> to specify a database- for
66 example, by passing in a DSN. The model class should open the database,
67 and create a class for each table in the database. These classes will
68 then be C<adopt>ed. It should also populate C<< $config->tables >> and
69 C<< $config->classes >> with the names of the classes and tables
70 respectively. The classes should be placed under the specified
71 namespace. For instance, C<beer> should be mapped to the class
76 $model->class_of($r, $table)
78 This maps between a table name and its associated class.
82 This class method is passed a request object and is expected to return an
83 object of the appropriate table class from information stored in the request
88 This class method is passed the name of a model class that represensts a table
89 and allows the master model class to do any set-up required.
93 This is a list of all the columns in a table. You may also override
94 see also C<display_columns>
98 This is the name of the table.
102 sub class_of { die "This is an abstract method" }
103 sub setup_database { die "This is an abstract method" }
104 sub fetch_objects { die "This is an abstract method" }
112 If there is an object in C<$r-E<gt>objects>, then it should be edited
113 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
114 be created with those parameters, and put back into C<$r-E<gt>objects>.
115 The template should be changed to C<view>, or C<edit> if there were any
116 errors. A hash of errors will be passed to the template.
120 sub do_edit { die "This is an abstract method" }
124 The C<list> method should fill C<$r-E<gt>objects> with all of the
125 objects in the class. You may want to page this using C<Data::Page> or
138 Empty Action, calls list if provided with a table.
144 sub list : Exported {
145 die "This is an abstract method";
148 sub view : Exported {
151 sub edit : Exported {
154 sub index : Exported {
155 my ( $self, $r ) = @_;
157 $r->template("list");
158 return $self->list($r);
164 Also, see the exported commands in C<Maypole::Model::CDBI>.
166 =head1 Other overrides
168 Additionally, individual derived model classes may want to override the
171 =head2 display_columns
173 Returns a list of columns to display in the model. By default returns
174 all columns in alphabetical order. Override this in base classes to
175 change ordering, or elect not to show columns.
179 Same as display_columns, only for listings. Defaults to display_columns
183 Return a hash mapping column names with human-readable equivalents.
191 $col =~ s/_+(\w)?/ \U$1/g;
198 should return true if a certain action is supported, or false otherwise.
199 Defaults to checking if the sub has the C<:Exported> attribute.
204 my ( $self, $action, $attrs ) = @_;
205 my $cv = $self->can($action);
206 warn "is_public failed . action is $action. self is $self" and return 0 unless $cv;
208 my %attrs = (ref $attrs) ? %$attrs : map {$_ => 1} $self->method_attrs($action,$cv) ;
211 warn "is_public failed. $action not exported. attributes are : ", %attrs;
213 } unless $attrs{Exported};
218 =head2 add_model_superclass
220 Adds model as superclass to model classes (if necessary)
224 sub add_model_superclass { return; }
228 Returns the list of attributes defined for a method. Maypole itself only
229 defines the C<Exported> attribute.
234 my ($class, $method, $cv) = @_;
236 $cv ||= $class->can($method);
240 my @attrs = attributes::get($cv);
247 This can go either in the master model class or in the individual
248 classes, and returns a list of has-many accessors. A brewery has many
249 beers, so C<BeerDB::Brewery> needs to return C<beers>.
261 L<Maypole>, L<Maypole::Model::CDBI>.
265 Maypole is currently maintained by Aaron Trevena.
267 =head1 AUTHOR EMERITUS
269 Simon Cozens, C<simon#cpan.org>
271 Simon Flack maintained Maypole from 2.05 to 2.09
273 Sebastian Riedel, C<sri#oook.de> maintained Maypole from 1.99_01 to 2.04
277 You may distribute this code under the same terms as Perl itself.