blib/lib/Backed_Objects.pm

filter: Statementstmt 13.7% 8 / 58 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 8.0% 2 / 25 Podpod 95.6% 22 / 23 totaltotal 27.1% 32 / 118 SCAR 39.6
CC
8
Cov
11%
select
6.0
insert
6.0
update
6.0
CRAP 52.5
↑ Summary
1 package Backed_Objects;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Backed_Objects - Create static files from a database.
9  
10 =head1 VERSION
11  
12 Version 1.14
13  
14 =cut
15  
16 2 our $VERSION = '1.14';
17  
18 =head1 SYNOPSIS
19  
20 Create static files from a database.
21 Update the files every time when you update the database.
22  
23 It can be used with any kind of database, for example SQL databases,
24 Berkeley databases, data stored in .ini files, etc.
25  
26 =head1 USAGE OF THE CLASS
27  
28 The class C<Backed_Objects> is an abstract base class and you need to
29 derive your class from it. For further suppose you developed a class
30 C<HTML_DB> derived from C<Backed_Objects>. We will call HTML (for example)
31 files which are updated by this module I<the view>.
32  
33 All methods can be called either as object methods or as class methods.
34  
35 Calling these as class methods may be convienient when you do not need to
36 specify additional parameters to be put into an object.
37  
38 Thus the following alternatives are possible:
39  
40 HTML_DB->output_all_and_order;
41  
42 or
43  
44 my $obj = HTML_DB->new;
45 $obj->output_all_and_order;
46  
47 Examples below assume that you are familiar with SQL and C<DBI> module.
48  
49 =head1 Database update methods
50  
51 The class C<Backed_Objects> offers you flexibility on the way how you
52 update your database.
53  
54 One variant is to override C<do_insert>, C<do_update>, C<do_delete>, C<post_process> methods,
55 so that they will update your database when C<insert>, C<update>, C<delete>
56 methods are called.
57  
58 The other variant is to update the database yourself and I<afterward>
59 to call C<insert>, C<update>, C<delete> which will call the default
60 do-nothing C<do_insert>, C<do_update>, C<do_delete>, C<post_process> methods.
61  
62 =head1 The object and the ID
63  
64 A database stores objects, every object stored in a database has an ID.
65 An object may be without an ID when it is not yet stored into the DB, but
66 you must assign an ID to an object when you store it in the DB, either
67 by overriding C<do_insert> method which should set the object ID or yourself
68 in your own code (without overriding C<do_insert>). C<HTML_DB> must have
69 C<id> method which receives an object and return its ID.
70  
71 The interface of this module does not specify what objects are. Objects may
72 be hashes or any other data structures.
73  
74 Every object inserted into the database has ID (which may be a natural number,
75 but is not required to be a number).
76  
77 Sometimes you may want the objects and IDs to be the same. For example, it is
78 often OK for an object and an ID to be a row ID in a SQL database. Or you may
79 want an object to be a hash representing a row in an SQL DB.
80  
81 Sometimes a middle solution is fit: Store an object as a hash with some
82 values from the database and read the rest values from the DB when needed,
83 using the ID stored in the object.
84  
85 An other possibililty for an object is to be a hash based on user input in
86 a HTML form.
87  
88 =head1 METHODS
89  
90 =head2 id
91  
92 This abstract (not defined in C<Backed_Objects> method) must be defined to
93 return the ID of an object.
94  
95 Examples:
96  
97 sub id {
98 my ($self, $obj) = @_;
99 return $obj->{id};
100 }
101  
102 or
103  
104 # Objects are simple IDs
105 sub id {
106 my ($self, $obj) = @_;
107 return $obj;
108 }
109  
110 =head2 all_ids
111  
112 This abstract (not defined in C<Backed_Objects> method) must return a list of
113 all IDs in the database.
114  
115 sub all_ids {
116 my ($self) = @_;
117 return @{ $dbh->selectcol_arrayref("SELECT id FROM table") };
118 }
119  
120 =cut
121  
122 =head2 do_select
123  
124 This abstract method should return an object from the DB having a given ID.
125  
126 sub do_select {
127 my ($self, $id) = @_;
128 return $dbh->selectrow_hashref("SELECT * FROM table WHERE id=?", undef, $id);
129 }
130  
131 or
132  
133 # Objects are simple IDs
134 sub do_select {
135 my ($self, $id) = @_;
136 return $id;
137 }
138  
139 =head2 select
140  
141 This method returns an object from the DB or C<undef> if the ID is absent
142 (undefined or zero).
143  
144 See its implementation:
145  
146 sub select {
147 my ($self, $id) = @_;
148 return undef unless $id;
149 return $self->do_select($id);
150 }
151  
152 =cut
153  
154 sub select {
155 0 ▶ my ($self, $id) = @_;
Subroutine select: not called CC=2 SCAR=17.9
Pod documented
156 0 ▶ return undef unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
157 0 return $self->do_select($id);
158 }
159  
160 =head2 do_insert, do_update, do_delete, post_process
161  
162 By default these methods do nothing. (In this case you need to update database
163 yourself, before calling C<insert>, C<update>, or C<delete> methods.)
164  
165 You may override these methods to do database updates:
166  
167 sub do_insert {
168 my ($self, $obj) = @_;
169 my @keys = keys %$obj;
170 my @values = values %$obj;
171 my $set = join ', ', map { "$_=?" } @keys;
172 $dbh->do("INSERT table SET $set", undef, @values);
173 $obj->{id} = $dbh->last_insert_id(undef, undef, undef, undef);
174 }
175  
176 sub do_update {
177 my ($self, $obj) = @_;
178 my @keys = keys %$obj;
179 my @values = values %$obj;
180 my $set = join ', ', map { "$_=?" } @keys;
181 $dbh->do("UPDATE table SET $set WHERE id=?", undef, @values, $obj->{id});
182 }
183  
184 sub do_delete {
185 my ($self, $id) = @_;
186 $dbh->do("DELETE FROM table WHERE id=?", undef, $id);
187 }
188  
189 sub post_process {
190 my ($self, $obj) = @_;
191 ...
192 }
193  
194 C<do_insert> should set object ID after it is saved into the database.
195  
196 C<post_process> is called by C<insert> after the object is inserted into
197 the database (and the object ID is set). It can be used for amending the
198 object with operations which require some object ID, for example for
199 uploading files into a folder with name being based on the ID.
200  
201 C<post_process> is also called by C<update>.
202  
203 =cut
204  
205 ▶sub do_insert { }
Subroutine do_insert: not called CC=1 SCAR=0.0
Pod documented
206 ▶sub do_update { }
Subroutine do_update: not called CC=1 SCAR=0.0
Pod documented
207 ▶sub do_delete { }
Subroutine do_delete: not called CC=1 SCAR=0.0
Pod documented
208 ▶sub post_process { }
Subroutine post_process: not called CC=1 SCAR=0.0
Pod documented
209  
210 =head2 outputter
211  
212 This method should return a value used to output a view of the DB
213 (for example it may be used to output HTML files and be a hash whose values
214 are HTML templates).
215  
216 Example:
217  
218 use File::Slurp;
219  
220 sub outputter {
221 my ($self) = @_;
222 my $template_dir = "$ENV{DOCUMENT_ROOT}/templates";
223 return { main_tmpl => read_file("$template_dir/main.html"),
224 announce_tmpl => read_file("$template_dir/announce.html") };
225 }
226  
227 The default implementation returns C<undef>.
228  
229 =cut
230  
231 ▶sub outputter { }
Subroutine outputter: not called CC=1 SCAR=0.0
Pod documented
232  
233 =head2 insert, update, delete
234  
235 HTML_DB->insert($obj);
236 HTML_DB->update($obj);
237 HTML_DB->delete($id);
238  
239 These functions update the view based on the value C<$obj> from the DB.
240 They are to be called when an object is inserted, updated, or deleted in
241 the DB.
242  
243 If you've overridden the C<do_insert>, C<do_update>, or C<do_delete> methods,
244 then C<insert>, C<update>, or C<delete> methods update the database before
245 updating the view.
246  
247 Note that C<insert> methods calls both C<on_update> and C<on_insert> methods
248 (as well as some other methods, see the source).
249  
250 C<insert> and C<update> also call C<post_process>.
251  
252 C<update> also calls C<before_update> before calling C<do_update>.
253 The C<before_update> method can be used to update the data based on
254 old data in the DB, before the DB is updated by C<do_update>.
255  
256 =cut
257  
258 # Calls both on_update() and on_insert()
259 sub insert {
260 0 ▶ my ($self, $obj) = @_;
Subroutine insert: not called CC=2 SCAR=17.9
Pod documented
261 0 ▶ die "Inserting an object into DB second time!" if $self->id($obj);
Branch: 1 branch
BranchTrueFalseTotal
if $self->id($obj) 0 0 0
262 0 $self->do_insert($obj);
263 0 $self->post_process($obj);
264 0 $self->on_insert($obj);
265 0 $self->on_update($obj);
266 0 $self->on_order_change;
267 0 $self->on_any_change;
268 }
269  
270 sub update {
271 0 ▶ my ($self, $obj) = @_;
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
272 0 ▶ die "Updating an object not in DB!" unless $self->id($obj);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->id($obj) 0 0 0
273 0 $self->before_update($obj);
274 0 $self->do_update($obj);
275 0 $self->post_process($obj);
276 0 $self->on_update($obj);
277 0 $self->on_any_change;
278 }
279  
280 sub delete {
281 0 ▶ my ($self, $id) = @_;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
282 0 $self->do_delete($id);
283 0 $self->on_order_change;
284 0 $self->on_delete($id);
285 0 $self->on_any_change;
286 }
287  
288 =head2 on_update, on_update_one
289  
290 C<on_update> method it called when an object in the database is updated or after
291 a new object is inserted.
292  
293 C<on_update_one> is the method called by C<on_update>. The C<on_update_one>
294 method is meant to update view of one object. Contrary to this, C<on_update>
295 may be overridden to update several objects by calling C<on_update_one> several
296 times. For example, when updating title of a HTML file, we may want to update
297 two more HTML files with titles of prev/next links dependent on the title of
298 this object.
299  
300 By default C<on_update_one> calls the C<output> method to update the view of the
301 object.
302  
303 =cut
304  
305 sub on_update {
306 0 ▶ my ($self, $obj) = @_;
Subroutine on_update: not called CC=1 SCAR=6.9
Pod documented
307 0 $self->on_update_one($obj);
308 }
309  
310 sub on_update_one {
311 0 ▶ my ($self, $obj) = @_;
Subroutine on_update_one: not called CC=1 SCAR=6.9
Pod documented
312 0 $self->output(scalar($self->outputter), $obj);
313 }
314  
315 =head2 on_insert, on_delete, on_order_change, before_update
316  
317 sub on_insert {
318 my ($self, $obj) = @_;
319 ...
320 }
321  
322 sub on_delete {
323 my ($self, $id) = @_;
324 ...
325 }
326  
327 sub on_order_change {
328 my ($self) = @_;
329 ...
330 }
331  
332 sub before_update {
333 my ($self, $obj) = @_;
334 ...
335 }
336  
337 These methods (doing nothing by default) are called correspondingly when:
338  
339 =over
340  
341 =item inserting a new object into the database;
342  
343 =item deleting an object from the database;
344  
345 =item changing order of objects in the database (including the case of inserting a new object).
346  
347 =item before calling C<do_update> to update the database.
348  
349 =back
350  
351 By default these methods do nothing.
352  
353 You may update your view in your overrides of these methods.
354  
355 C<before_update> is called by C<update> (but not by C<insert>) before updating
356 the DB with C<do_update>.
357  
358 =cut
359  
360 ▶sub on_insert { }
Subroutine on_insert: not called CC=1 SCAR=0.0
Pod documented
361 ▶sub on_delete { }
Subroutine on_delete: not called CC=1 SCAR=0.0
Pod documented
362 ▶sub on_order_change { }
Subroutine on_order_change: not called CC=1 SCAR=0.0
Pod documented
363 ▶sub before_update { }
Subroutine before_update: not called CC=1 SCAR=0.0
Pod documented
364  
365 =head2 on_any_change
366  
367 sub on_any_change {
368 my ($self) = @_;
369 ...
370 }
371  
372 This method is called after every change of the database, for example,
373 after insertion, deletion, update, etc.
374  
375 =cut
376  
377 ▶sub on_any_change { }
Subroutine on_any_change: not called CC=1 SCAR=0.0
Pod documented
378  
379 =head2 do_output
380  
381 sub do_output {
382 my ($self, $outputter, $obj) = @_;
383 ...
384 }
385  
386 This is the main method to output your files (the view).
387  
388 It receives the object and the outputter returned by the C<outputter> method.
389  
390 =cut
391  
392 ▶sub do_output { }
Subroutine do_output: not called CC=1 SCAR=0.0
Pod documented
393  
394 =head2 order_change
395  
396 HTML_DB->order_change;
397  
398 Call C<order_change> after you changed the order of objects in the
399 database (but not after calling C<insert> or C<delete> method which call
400 C<order_change> automatically).
401  
402 =cut
403  
404 sub order_change {
405 0 ▶ my ($self) = @_;
Subroutine order_change: not called CC=1 SCAR=6.9
Pod documented
406 0 $self->on_order_change;
407 0 $self->on_any_change;
408 }
409  
410 =head2 output_by_id
411  
412 An internal function.
413  
414 =cut
415  
416 sub output_by_id {
417 0 ▶ my ($self, $id, $outputter) = @_;
Subroutine output_by_id: not called CC=2 SCAR=17.9
Pod documented
418 0 ▶ $outputter = $self->outputter unless $outputter;
Branch: 1 branch
BranchTrueFalseTotal
unless $outputter 0 0 0
419 0 my $obj = $self->select($id);
420 0 $self->output($outputter, $obj);
421 }
422  
423 =head2 output_all
424  
425 HTML_DB->output_all;
426 HTML_DB->output_all_and_order;
427  
428 C<output_all> updates the entire set of your files based on the
429 data in the DB.
430  
431 C<output_all_and_order> additionally updates data dependent on the order
432 of objects in the DB.
433  
434 Use these methods to update your all files (for example, after your template
435 changed).
436  
437 =cut
438  
439 sub output_all {
440 0 ▶ my ($self) = @_;
Subroutine output_all: not called CC=2 SCAR=17.9
Pod documented
441 0 my @ids = $self->all_ids;
442 0 my $outputter = $self->outputter;
443 0 for my $id (@ids) {
444 0 $self->output_by_id($id, $outputter);
445 }
446 0 $self->on_any_change;
447 }
448  
449 # Is it better to first call output_all and then on_order_change, or the reverse
450 sub output_all_and_order {
451 0 ▶ my ($self) = @_;
Subroutine output_all_and_order: not called CC=1 SCAR=6.9
Pod undocumented
452 0 $self->output_all;
453 0 $self->on_order_change;
454 }
455  
456 =head2 save
457  
458 HTML_DB->save($obj);
459  
460 This saves an object into the DB: updates it if it is already in the DB
461 (has an ID) or inserts it into the DB if it has an undefined ID.
462  
463 The actual code:
464  
465 sub save {
466 my ($self, $obj) = @_;
467 if($self->id($obj)) {
468 $self->update($obj);
469 } else {
470 $self->insert($obj);
471 }
472 }
473  
474 =cut
475  
476 sub save {
477 0 ▶ my ($self, $obj) = @_;
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
478 0 ▶ if($self->id($obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id($obj)) { } 0 0 0
479 0 $self->update($obj);
480 } else {
481 0 $self->insert($obj);
482 }
483 }
484  
485 =head2 output
486  
487 An internal function.
488  
489 =cut
490  
491 sub output {
492 0 ▶ my ($self, $outputter, $obj) = @_;
Subroutine output: not called CC=2 SCAR=17.9
Pod documented
493 0 ▶ die "Object has no ID!" unless $self->id($obj);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->id($obj) 0 0 0
494 0 $self->do_output(scalar($outputter), $obj);
495 }
496  
497 =head1 AUTHOR
498  
499 Victor Porton, C<< <porton@narod.ru> >>
500  
501 =head1 BUGS
502  
503 Please report any bugs or feature requests to C<bug-backed_objects at rt.cpan.org>, or through
504 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Backed_Objects>. I will be notified, and then you'll
505 automatically be notified of progress on your bug as I make changes.
506  
507 In the current version of C<Backed_Objects> there are no provision for passing
508 file handles for example got from a HTML form with a C<file> control.
509 A complexity is that usually to upload a file we need to already know the
510 ID of a row in a database what is possible only I<after> inserting into the DB.
511 Your suggestions how to deal with this problem are welcome.
512  
513 =head1 SUPPORT
514  
515 You can find documentation for this module with the perldoc command.
516  
517 perldoc Backed_Objects
518  
519  
520 You can also look for information at:
521  
522 =over 4
523  
524 =item * RT: CPAN's request tracker
525  
526 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Backed_Objects>
527  
528 =item * AnnoCPAN: Annotated CPAN documentation
529  
530 L<http://annocpan.org/dist/Backed_Objects>
531  
532 =item * CPAN Ratings
533  
534 L<http://cpanratings.perl.org/d/Backed_Objects>
535  
536 =item * Search CPAN
537  
538 L<http://search.cpan.org/dist/Backed_Objects/>
539  
540 =back
541  
542 =head1 LICENSE AND COPYRIGHT
543  
544 Copyright 2011 Victor Porton.
545  
546 This program is free software; you can redistribute it and/or modify it
547 under the terms of either: the GNU General Public License as published
548 by the Free Software Foundation; or the Artistic License.
549  
550 See http://dev.perl.org/licenses/ for more information.
551  
552  
553 =cut
554  
555 2 1; # End of Backed_Objects
↑ Summary