]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Model/Base.pm
Lots of documentation.
[maypole.git] / lib / Maypole / Model / Base.pm
1 package Maypole::Model::Base;
2 our %remember;
3 sub MODIFY_CODE_ATTRIBUTES { $remember{$_[1]} = $_[2]; () }
4
5 sub FETCH_CODE_ATTRIBUTES { $remember{$_[1]} } 
6 sub view :Exported { }
7 sub edit :Exported { }
8
9 sub process {
10     my ($class, $r) = @_;
11     $r->template( my $method = $r->action );
12     $r->objects([ $class->retrieve(shift @{$r->{args}}) ]);
13     $class->$method($r);
14 }
15
16 =head1 NAME
17
18 Maypole::Model::Base - Base class for model classes
19
20 =head1 DESCRIPTION
21
22 Anyone subclassing this for a different database abstraction mechanism
23 needs to provide the following methods:
24
25 =head2 do_edit
26
27 If there is an object in C<$r-E<gt>objects>, then it should be edited
28 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
29 be created with those parameters, and put back into C<$r-E<gt>objects>.
30 The template should be changed to C<view>, or C<edit> if there were any
31 errors. A hash of errors will be passed to the template.
32
33 =cut
34
35 sub do_edit { die "This is an abstract method" }
36
37 =head2 setup_database
38
39     $model->setup_database($config, $namespace, @data)
40
41 Uses the user-defined data in C<@data> to specify a database- for
42 example, by passing in a DSN. The model class should open the database,
43 and create a class for each table in the database. These classes will
44 then be C<adopt>ed. It should also populate C<< $config->{tables} >> and
45 C<< $config->{classes} >> with the names of the classes and tables
46 respectively. The classes should be placed under the specified
47 namespace. For instance, C<beer> should be mapped to the class
48 C<BeerDB::Beer>.
49
50 =head2 class_of
51
52     $model->class_of($r, $table)
53
54 This maps between a table name and its associated class.
55
56 =head2 retrieve
57
58 This turns an ID into an object of the appropriate class.
59
60 =head2 adopt
61
62 This is called on an model class representing a table and allows the
63 master model class to do any set-up required. 
64
65 =head2 related
66
67 This can go either in the master model class or in the individual
68 classes, and returns a list of has-many accessors. A brewery has many
69 beers, so C<BeerDB::Brewery> needs to return C<beers>.
70
71 =head2 columns
72
73 This is a list of the columns in a table.
74
75 =head2 table
76
77 This is the name of the table.
78
79 =head2 Commands
80
81 =over
82
83 =item list
84
85 The C<list> method should fill C<< $r-> objects >> with all of the
86 objects in the class. You may want to page this using C<Data::Page> or
87 similar.
88
89 =back
90
91 =cut
92
93 sub class_of       { die "This is an abstract method" }
94 sub setup_database { die "This is an abstract method" }
95 sub list :Exported { die "This is an abstract method" };
96
97 =pod
98
99 Also, see the exported commands in C<Maypole::Model::CDBI>.
100
101 =head1 Other overrides
102
103 Additionally, individual derived model classes may want to override the
104 following methods:
105
106 =head2 column_names
107
108 Return a hash mapping column names with human-readable equivalents.
109
110 =cut
111
112 sub column_names { my $class = shift; map { $_ => ucfirst $_ } $class->columns }
113
114 =head2 description
115
116 A description of the class to be passed to the template.
117
118 =cut
119
120 sub description { "A poorly defined class" }
121
122 1;
123