blib/lib/Dancer2/Plugin/Flash.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 50.0% 2 / 4 Conditioncond 53.8% 7 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 9 / 9 Podpod 50.0% 1 / 2 totaltotal 77.4% 55 / 71 CC 9 SCAR 13.0
CC
9
Cov
83%
flash
7.5
__ANON__
2.1
__ANON__
2.1
CRAP 3.7
↑ Summary
1 package Dancer2::Plugin::Flash;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Dancer2::Plugin::Flash - flash message for Dancer2
12  
13 =head1 VERSION
14  
15 Version 0.01
16  
17 =cut
18  
19 2 our $VERSION = '0.03';
20  
21 =head1 CONFIGURATION
22  
23 Configuration requires a secret key at a minimum.
24  
25 Either put this in your F<config.yml> file:
26  
27 plugins:
28 Flash:
29 token_name: 'flash'
30 session_hash_key: '_flash'
31  
32 Or set the secret key at run time, with:
33  
34 BEGIN {
35 set plugins => {
36 Flash => {
37 token_name => 'flash',
38 session_hash_key => '_flash'
39 },
40 };
41 }
42  
43 =cut
44  
45 has token_name => (
46 is => 'ro',
47 1 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
48 1 my ( $self ) = @_;
49 1 ▶ return $self->config->{token_name} || 'flash';
Condition50%1/2
$self->config->{'token_name'} || 'flash'
l!l
01
MC/DC0%0/1
$self->config->{'token_name'} || 'flash'
$self->config->{'token_name'}
Truth table50%1/2
A
A= $self->config->{'token_name'}
Aresult
11
00
50 }
51 2 );
52  
53 has session_hash_key => (
54 is => 'ro',
55 1 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
56 1 my ( $self ) = @_;
57 1 ▶ return $self->config->{session_hash_key} || '_flash';
Condition50%1/2
$self->config->{'session_hash_key'} || '_flash'
l!l
01
MC/DC0%0/1
$self->config->{'session_hash_key'} || '_flash'
$self->config->{'session_hash_key'}
Truth table50%1/2
A
A= $self->config->{'session_hash_key'}
Aresult
11
00
58 }
59 2 );
60  
61 1 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
62 1 my $plugin = shift;
63  
64 $plugin->app->add_hook( Dancer2::Core::Hook->new(
65 name => 'before_template_render',
66 3 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
67 3 my $tokens = shift;
68 3 my $session = $plugin->app->session;
69 3 ▶ my $flash = $session->read( $plugin->session_hash_key ) || {};
Condition100%2/2
$session->read($plugin->session_hash_key) || {}
l!l
12
MC/DC100%1/1
$session->read($plugin->session_hash_key) || {}
$session->read($plugin->session_hash_key)
Truth table100%2/2
A
A= $session->read($plugin->session_hash_key)
Aresult
11
00
70 # Assign it to template
71 3 $tokens->{ $plugin->token_name } = $flash;
72 # Remove from session
73 3 $session->write( $plugin->session_hash_key, {} );
74 }
75 1 ));
76 }
77  
78 =head1 DESCRIPTION
79  
80 A flash is session data with a specific life cycle. When you put something into the flash it stays then until the end of the next request. This allows you to use it for storing messages that can be accessed after a redirect, but then are automatically cleaned up.
81  
82 =head1 SYNOPSIS
83  
84 package TestApp;
85 use Dancer2;
86 use Dancer2::Plugin::Flash;
87  
88 BEGIN {
89 set plugins => {
90 Flash => {
91 token_name => 'flash',
92 session_hash_key => '_flash'
93 },
94 };
95 }
96  
97 get '/different' => sub {
98 flash(error => 'plop');
99 template 'index', { foo => 'bar' };
100 };
101  
102 =head1 SUBROUTINES/METHODS
103  
104 =head2 flash
105  
106 C<flash(KEY, VALUE)> - Store the give key and valye and send it to template temporary.
107  
108 =cut
109  
110 2 plugin_keywords 'flash';
111  
112 1 ▶sub flash {
Subroutine flash: called CC=6 SCAR=20.2
Pod documented
113 1 my( $plugin, $key, $value ) = @_;
114  
115 1 ▶ return unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 1 1
116  
117 1 ▶ if ( $plugin->app->request
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->app->request and $plugin->app->session) 1 0 1
Condition33%1/3
$plugin->app->request and $plugin->app->session
!ll&&!rl&&r
001
MC/DC0%0/2
$plugin->app->request and $plugin->app->session
$plugin->app->request $plugin->app->session
Truth table33%1/3
A and B
A= $plugin->app->requestB= $plugin->app->session
ABresult
0X0
100
111
118 && $plugin->app->session )
119 {
120 1 my $session = $plugin->app->session;
121 1 ▶ my $flash = $session->read( $plugin->session_hash_key ) || {};
Condition50%1/2
$session->read($plugin->session_hash_key) || {}
l!l
01
MC/DC0%0/1
$session->read($plugin->session_hash_key) || {}
$session->read($plugin->session_hash_key)
Truth table50%1/2
A
A= $session->read($plugin->session_hash_key)
Aresult
11
00
122  
123 1 ▶ $flash->{$key} = $value || '';
Condition50%1/2
$value || ''
l!l
10
MC/DC0%0/1
$value || ''
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
124 1 $session->write( $plugin->session_hash_key, $flash );
125 1 return $value;
126 }
127 0 return;
128 }
129  
130 =head1 AUTHOR
131  
132 Rakesh Kumar Shardiwal, C<< <rakesh.shardiwal at gmail.com> >>
133  
134 =head1 BUGS
135  
136 Please report any bugs or feature requests to C<bug-dancer2-plugin-flash at rt.cpan.org>, or through
137 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer2-Plugin-Flash>. I will be notified, and then you'll
138 automatically be notified of progress on your bug as I make changes.
139  
140  
141  
142  
143 =head1 SUPPORT
144  
145 You can find documentation for this module with the perldoc command.
146  
147 perldoc Dancer2::Plugin::Flash
148  
149  
150 You can also look for information at:
151  
152 =over 4
153  
154 =item * RT: CPAN's request tracker (report bugs here)
155  
156 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer2-Plugin-Flash>
157  
158 =item * AnnoCPAN: Annotated CPAN documentation
159  
160 L<http://annocpan.org/dist/Dancer2-Plugin-Flash>
161  
162 =item * CPAN Ratings
163  
164 L<http://cpanratings.perl.org/d/Dancer2-Plugin-Flash>
165  
166 =item * Search CPAN
167  
168 L<http://search.cpan.org/dist/Dancer2-Plugin-Flash/>
169  
170 =back
171  
172  
173 =head1 ACKNOWLEDGEMENTS
174  
175  
176 =head1 LICENSE AND COPYRIGHT
177  
178 Copyright 2016 Rakesh Kumar Shardiwal.
179  
180 This program is free software; you can redistribute it and/or modify it
181 under the terms of the the Artistic License (2.0). You may obtain a
182 copy of the full license at:
183  
184 L<http://www.perlfoundation.org/artistic_license_2_0>
185  
186 Any use, modification, and distribution of the Standard or Modified
187 Versions is governed by this Artistic License. By using, modifying or
188 distributing the Package, you accept this license. Do not use, modify,
189 or distribute the Package, if you do not accept this license.
190  
191 If your Modified Version has been derived from a Modified Version made
192 by someone other than you, you are nevertheless required to ensure that
193 your Modified Version complies with the requirements of this license.
194  
195 This license does not grant you the right to use any trademark, service
196 mark, tradename, or logo of the Copyright Holder.
197  
198 This license includes the non-exclusive, worldwide, free-of-charge
199 patent license to make, have made, use, offer to sell, sell, import and
200 otherwise transfer the Package with respect to any patent claims
201 licensable by the Copyright Holder that are necessarily infringed by the
202 Package. If you institute patent litigation (including a cross-claim or
203 counterclaim) against any party alleging that the Package constitutes
204 direct or contributory patent infringement, then this Artistic License
205 to you shall terminate on the date that such litigation is filed.
206  
207 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
208 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
209 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
210 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
211 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
212 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
213 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
214 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
215  
216  
217 =cut
218  
219 2 1; # End of Dancer2::Plugin::Flash
↑ Summary