File Coverage

blib/lib/Catalyst/Plugin/Message.pm
Criterion Covered Total %
statement 6 24 25.0
branch 0 8 0.0
condition 0 6 0.0
subroutine 2 5 40.0
pod 3 3 100.0
total 11 46 23.9


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::Message;
2              
3 1     1   23861 use warnings;
  1         3  
  1         28  
4 1     1   5 use strict;
  1         2  
  1         360  
5             our $VERSION = '0.03';
6              
7             sub errmsg {
8 0     0 1   my $c = shift;
9 0           my ( $key, $msg ) = @_;
10 0 0 0       if ( $key and $msg ){
11 0 0         $c->stash->{errmsg}{$key} = $msg if ( not defined $c->stash->{errmsg}{$key} );
12             }else{
13 0           return scalar keys %{$c->stash->{errmsg}};
  0            
14             }
15             }
16              
17             sub tipmsg {
18 0     0 1   my $c = shift;
19 0           my ( $key, $msg ) = @_;
20 0 0 0       if ( $key and $msg ){
21 0 0         $c->stash->{tipmsg}{$key} = $msg if ( not defined $c->stash->{tipmsg}{$key} );
22             }else{
23 0           return scalar keys %{$c->stash->{tipmsg}};
  0            
24             }
25             }
26              
27             sub diemsg {
28 0     0 1   my $c = shift;
29 0           my ( $msg, @string ) = @_;
30 0           $c->stash->{diemsg} = sprintf( $msg, @string );
31 0           $c->stash->{template} = 'error.tpl';
32 0           $c->res->status(500);
33 0           die $c->error(0);
34             }
35              
36             1;
37              
38             =pod
39              
40             =head1 NAME
41              
42             Catalyst::Plugin::Message - The great new Catalyst::Plugin::Message!
43              
44             =head1 VERSION
45              
46             Version 0.03
47              
48             =head1 SYNOPSIS
49              
50             # in your controller
51             use Catalyst qw/Message/;
52             sub register : Local {
53             my ( $self, $c ) = @_;
54             if ( $c->req->method eq 'POST' ){
55             my $email = $c->req->param('email');
56             $c->errmsg( email => 'email can not be empty.' ) unless defined $email;
57             $c->errmsg( email => 'email invalid.' ) unless $email =~ /\@/;
58             if ( not $c->errmsg ){
59             # save data
60             }
61             }
62             $c->stash->{'template'} = 'register.tpl';
63             }
64              
65             # register.tpl
66             [% errmsg.email %]
67              
68             =head2 errmsg
69              
70             pass some error message return to the previous page, every message has a key to indicate which aspect.
71              
72             you can make more error messages relate to a key, only the first message will save into stash.
73              
74             =head2 tipmsg
75              
76             same as errmsg, just make some tips message return.
77              
78             =head3 diemsg
79              
80             # in your controller
81             use Catalyst qw/Message/;
82             sub edit : Local {
83             my ( $self, $c ) = @_;
84             my $client_id = $c->req->param('client_id');
85             my $client = $c->model('DBIC::Client')->find( $client_id );
86             $c->diemsg( "client not found with id: %s", $client_id ) unless $client;
87              
88             # continue when $client object is valid
89             # ...
90            
91             $c->stash->{'template'} = 'edit.tpl';
92             }
93              
94             # error.tpl
95             [% diemsg %]
96              
97             Sometimes the fatal error occurs, which means we cannot continue to execute the rest code,
98             and we wanner just raise the error to the client. For example as above, if the $client object
99             not exists, maybe the parameter client_id invalid or the client has been deleted, then we need
100             tell that what happened, but the internal $c->error simple save the error informations into
101             stash and continue rest codes - of course we can use if-else to let it work, but why should we
102             make it simple, just like native die() did? Here diemsg() comes, and the error information is a
103             business logic, but not code logic, so we need not show the req-res-stash to user. Just tell your
104             user what happend, and make them understand what's the point. :)
105              
106             =head1 AUTHOR
107              
108             Chunzi, C<< >>
109              
110             =head1 BUGS
111              
112             Please report any bugs or feature requests to
113             C, or through the web interface at
114             L.
115             I will be notified, and then you'll automatically be notified of progress on
116             your bug as I make changes.
117              
118             =head1 SUPPORT
119              
120             You can find documentation for this module with the perldoc command.
121              
122             perldoc Catalyst::Plugin::Message
123              
124             You can also look for information at:
125              
126             =over 4
127              
128             =item * AnnoCPAN: Annotated CPAN documentation
129              
130             L
131              
132             =item * CPAN Ratings
133              
134             L
135              
136             =item * RT: CPAN's request tracker
137              
138             L
139              
140             =item * Search CPAN
141              
142             L
143              
144             =back
145              
146             =head1 ACKNOWLEDGEMENTS
147              
148             =head1 COPYRIGHT & LICENSE
149              
150             Copyright 2006 Chunzi, all rights reserved.
151              
152             This program is free software; you can redistribute it and/or modify it
153             under the same terms as Perl itself.
154              
155             =cut