1 package Maypole::Model::Base;
3 sub MODIFY_CODE_ATTRIBUTES { $remember{$_[1]} = $_[2]; () }
5 sub FETCH_CODE_ATTRIBUTES { $remember{$_[1]} }
11 my $method = $r->action;
12 return if $r->{template}; # Authentication has set this, we're done.
14 $r->{template} = $method;
16 my $obj = $class->retrieve( $r->{args}->[0] );
18 $r->objects([ $obj ]);
30 Maypole::Model::Base - Base class for model classes
34 Anyone subclassing this for a different database abstraction mechanism
35 needs to provide the following methods:
39 If there is an object in C<$r-E<gt>objects>, then it should be edited
40 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
41 be created with those parameters, and put back into C<$r-E<gt>objects>.
42 The template should be changed to C<view>, or C<edit> if there were any
43 errors. A hash of errors will be passed to the template.
47 sub do_edit { die "This is an abstract method" }
51 $model->setup_database($config, $namespace, @data)
53 Uses the user-defined data in C<@data> to specify a database- for
54 example, by passing in a DSN. The model class should open the database,
55 and create a class for each table in the database. These classes will
56 then be C<adopt>ed. It should also populate C<< $config->{tables} >> and
57 C<< $config->{classes} >> with the names of the classes and tables
58 respectively. The classes should be placed under the specified
59 namespace. For instance, C<beer> should be mapped to the class
64 $model->class_of($r, $table)
66 This maps between a table name and its associated class.
70 This turns an ID into an object of the appropriate class.
74 This is called on an model class representing a table and allows the
75 master model class to do any set-up required.
79 This can go either in the master model class or in the individual
80 classes, and returns a list of has-many accessors. A brewery has many
81 beers, so C<BeerDB::Brewery> needs to return C<beers>.
85 This is a list of all the columns in a table. You may also override
86 C<display_columns>, which is the list of columns you want to view, in
91 This is the name of the table.
99 The C<list> method should fill C<< $r-> objects >> with all of the
100 objects in the class. You may want to page this using C<Data::Page> or
107 sub class_of { die "This is an abstract method" }
108 sub setup_database { die "This is an abstract method" }
109 sub list :Exported { die "This is an abstract method" };
113 Also, see the exported commands in C<Maypole::Model::CDBI>.
115 =head1 Other overrides
117 Additionally, individual derived model classes may want to override the
122 Return a hash mapping column names with human-readable equivalents.
126 sub column_names { my $class = shift; map {
128 $col =~ s/_+(\w)?/ \U$1/g;
129 $_ => ucfirst $col } $class->columns }
133 A description of the class to be passed to the template.
137 sub description { "A poorly defined class" }