blib/lib/Catalyst/Plugin/Message.pm

filter: Statementstmt 30.7% 8 / 26 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.0% 2 / 5 Podpod 100% 3 / 3 totaltotal 25.0% 13 / 52 CC 7 SCAR 27.0
CC
7
Cov
20%
errmsg
20.0
tipmsg
20.0
diemsg
2.0
CRAP 14.9
↑ Summary
1 package Catalyst::Plugin::Message;
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 2 our $VERSION = '0.03';
6  
7 0 ▶sub errmsg {
Subroutine errmsg: not called CC=4 SCAR=30.0
Pod documented
8 0 my $c = shift;
9 0 my ( $key, $msg ) = @_;
10 0 ▶ if ( $key and $msg ){
Branch: 1 branch
BranchTrueFalseTotal
if ($key and $msg) { } 0 0 0
Condition0%0/3
$key and $msg
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $msg
$key $msg
Truth table0%0/3
A and B
A= $keyB= $msg
ABresult
0X0
100
111
11 0 ▶ $c->stash->{errmsg}{$key} = $msg if ( not defined $c->stash->{errmsg}{$key} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $c->stash->{"errmsg"}{$key} 0 0 0
12 }else{
13 0 return scalar keys %{$c->stash->{errmsg}};
14 }
15 }
16  
17 0 ▶sub tipmsg {
Subroutine tipmsg: not called CC=4 SCAR=30.0
Pod documented
18 0 my $c = shift;
19 0 my ( $key, $msg ) = @_;
20 0 ▶ if ( $key and $msg ){
Branch: 1 branch
BranchTrueFalseTotal
if ($key and $msg) { } 0 0 0
Condition0%0/3
$key and $msg
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $msg
$key $msg
Truth table0%0/3
A and B
A= $keyB= $msg
ABresult
0X0
100
111
21 0 ▶ $c->stash->{tipmsg}{$key} = $msg if ( not defined $c->stash->{tipmsg}{$key} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $c->stash->{"tipmsg"}{$key} 0 0 0
22 }else{
23 0 return scalar keys %{$c->stash->{tipmsg}};
24 }
25 }
26  
27 0 ▶sub diemsg {
Subroutine diemsg: not called CC=1 SCAR=6.9
Pod documented
28 0 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 2 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<< <chunzi at cpan.org> >>
109  
110 =head1 BUGS
111  
112 Please report any bugs or feature requests to
113 C<bug-catalyst-plugin-message at rt.cpan.org>, or through the web interface at
114 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-Message>.
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<http://annocpan.org/dist/Catalyst-Plugin-Message>
131  
132 =item * CPAN Ratings
133  
134 L<http://cpanratings.perl.org/d/Catalyst-Plugin-Message>
135  
136 =item * RT: CPAN's request tracker
137  
138 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-Message>
139  
140 =item * Search CPAN
141  
142 L<http://search.cpan.org/dist/Catalyst-Plugin-Message>
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
↑ Summary