]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Model/Base.pm
Ensure objects can always be safely dereferenced
[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     my $method = $r->action;
12     return if $r->{template}; # Authentication has set this, we're done.
13
14     $r->{template} = $method;
15     $r->objects([]);
16     my $obj = $class->retrieve( $r->{args}->[0] );
17     if ($obj) {
18         $r->objects([ $obj ]);
19         shift @{$r->{args}};
20     }
21     $class->$method($r);
22 }
23
24 sub display_columns { 
25     sort shift->columns;
26 }
27
28 =head1 NAME
29
30 Maypole::Model::Base - Base class for model classes
31
32 =head1 DESCRIPTION
33
34 Anyone subclassing this for a different database abstraction mechanism
35 needs to provide the following methods:
36
37 =head2 do_edit
38
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.
44
45 =cut
46
47 sub do_edit { die "This is an abstract method" }
48
49 =head2 setup_database
50
51     $model->setup_database($config, $namespace, @data)
52
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
60 C<BeerDB::Beer>.
61
62 =head2 class_of
63
64     $model->class_of($r, $table)
65
66 This maps between a table name and its associated class.
67
68 =head2 retrieve
69
70 This turns an ID into an object of the appropriate class.
71
72 =head2 adopt
73
74 This is called on an model class representing a table and allows the
75 master model class to do any set-up required. 
76
77 =head2 related
78
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>.
82
83 =head2 columns
84
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
87 the right order.
88
89 =head2 table
90
91 This is the name of the table.
92
93 =head2 Commands
94
95 =over
96
97 =item list
98
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
101 similar.
102
103 =back
104
105 =cut
106
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" };
110
111 =pod
112
113 Also, see the exported commands in C<Maypole::Model::CDBI>.
114
115 =head1 Other overrides
116
117 Additionally, individual derived model classes may want to override the
118 following methods:
119
120 =head2 column_names
121
122 Return a hash mapping column names with human-readable equivalents.
123
124 =cut
125
126 sub column_names { my $class = shift; map { 
127         my $col = $_;
128         $col =~ s/_+(\w)?/ \U\1/g;
129         $_ => ucfirst $col } $class->columns }
130
131 =head2 description
132
133 A description of the class to be passed to the template.
134
135 =cut
136
137 sub description { "A poorly defined class" }
138
139 1;
140