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