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