File Coverage

blib/lib/Dancer2/Plugin/Flash.pm
Criterion Covered Total %
statement 9 11 81.8
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 13 15 86.6


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Flash;
2              
3 1     1   13005 use 5.006;
  1         3  
4 1     1   3 use strict;
  1         2  
  1         17  
5 1     1   3 use warnings FATAL => 'all';
  1         4  
  1         35  
6              
7 1     1   220 use Dancer2::Plugin;
  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             our $VERSION = '0.01';
20              
21             =head1 CONFIGURATION
22              
23             Configuration requires a secret key at a minimum.
24              
25             Either put this in your F 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             default => sub {
48             my ( $self ) = @_;
49             return $self->config->{token_name} || 'flash';
50             }
51             );
52              
53             has session_hash_key => (
54             is => 'ro',
55             default => sub {
56             my ( $self ) = @_;
57             return $self->config->{session_hash_key} || '_flash';
58             }
59             );
60              
61             sub BUILD {
62             my $plugin = shift;
63              
64             $plugin->app->add_hook( Dancer2::Core::Hook->new(
65             name => 'before_template_render',
66             code => sub {
67             my $tokens = shift;
68             my $session = $plugin->app->session;
69             my $flash = $session->read( $plugin->session_hash_key ) || {};
70             # Assign it to template
71             $tokens->{ $plugin->token_name } = $flash;
72             # Remove from session
73             $session->write( $plugin->session_hash_key, {} );
74             }
75             ));
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 - Store the give key and valye and send it to template temporary.
107              
108             =cut
109              
110             plugin_keywords 'flash';
111              
112             sub flash {
113             my( $plugin, $key, $value ) = @_;
114              
115             return unless $key;
116              
117             if ( $plugin->app->request
118             && $plugin->app->session )
119             {
120             my $session = $plugin->app->session;
121             my $flash = $session->read( $plugin->session_hash_key ) || {};
122              
123             $flash->{$key} = $value || '';
124             $session->write( $plugin->session_hash_key, $flash );
125             return $value;
126             }
127             return;
128             }
129              
130             =head1 AUTHOR
131              
132             Rakesh Kumar Shardiwal, C<< >>
133              
134             =head1 BUGS
135              
136             Please report any bugs or feature requests to C, or through
137             the web interface at L. 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
157              
158             =item * AnnoCPAN: Annotated CPAN documentation
159              
160             L
161              
162             =item * CPAN Ratings
163              
164             L
165              
166             =item * Search CPAN
167              
168             L
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
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             1; # End of Dancer2::Plugin::Flash