File Coverage

blib/lib/Kelp/Module/FlashMessage.pm
Criterion Covered Total %
statement 17 19 89.4
branch 3 4 75.0
condition 2 2 100.0
subroutine 5 6 83.3
pod 1 1 100.0
total 28 32 87.5


line stmt bran cond sub pod time code
1 2     2   31922 use strict;
  2         5  
  2         50  
2 2     2   10 use warnings;
  2         2  
  2         87  
3             package Kelp::Module::FlashMessage;
4              
5             our $VERSION = 0.05;
6              
7 2     2   10 use parent 'Kelp::Module';
  2         4  
  2         13  
8              
9             sub build {
10 2     2 1 142 my ( $self, %args ) = @_;
11 2   100     10 my $key = $args{'key'} || 'km::flash';
12              
13             $self->register(
14             flash_message => sub {
15 4     4   130027 my ($self, $value) = @_;
16 4 100       17 if( @_ == 1 ) {
    50          
17 2         7 $value = delete $self->req->env->{'psgix.session'}->{$key};
18             }
19             elsif( @_ >= 2 ) {
20 2         8 $self->req->env->{'psgix.session'}->{$key} = $value;
21             }
22 4         33 return $value;
23             },
24              
25             flash_message_present => sub {
26 0     0     my $self = shift;
27 0           return exists $self->req->env->{'psgix.session'}->{$key};
28             }
29 2         18 );
30             }
31              
32             1;
33              
34             __END__
35              
36             =pod
37              
38             =head1 NAME
39              
40             Kelp::Module::FlashMessage - Kelp flash message support
41              
42             =head1 VERSION
43              
44             version 0.01
45              
46             =head1 SYNOPSIS
47              
48             Module configuration:
49              
50             # conf/config.pl
51             {
52             modules => ['FlashMessage'],
53             modules_init => {
54             'FlashMessage' => {
55             key => 'flash_custom_key'
56             # optional custom key name, the default is 'km::flash'
57             # it will be used to store the flash message into the session
58             },
59             }
60             }
61              
62             Usage (from a route)
63              
64             package MyApp;
65             use Kelp::Base 'Kelp';
66              
67             sub some_route {
68             my $self = shift;
69             $self->flash_message('my flash message' );
70             return $self->template('my_template', {
71             'fmp' => sub { $self->flash_message_present },
72             'fm' => sub { $self->flash_message }
73             });
74             }
75              
76             And finally, the template that consumes the flash message (Text::Xslate example):
77              
78             : if $fmp() {
79             Message: <: $fm() :>
80             : }
81              
82             : if $fmp() {
83             Message (should you see this? - no!)
84             There isn't flash message after you consume it
85             : }
86              
87             =head1 REQUIREMENTS
88              
89             It needs the Plack::Middleware::Session to work properly. See the adding
90             middleware section in the main Kelp documentation.
91              
92              
93             =head1 REGISTERED METHODS
94              
95             This module registers two methods into the application:
96              
97             =over 3
98              
99             =item C<flash_message>
100              
101             get/sets the flash message. When you get the flash
102             message value, it is deleted.
103              
104             =item C<flash_message_present>
105              
106             returns a true value if the flash message
107             is present. It may be called many times without clear the message
108              
109             =back
110              
111              
112             =head1 AUTHOR
113              
114             Miguel Prz, <niceperl at gmail.com>
115              
116             =head1 BUGS
117              
118             Please report any bugs or feature requests through
119             the web interface at L<https://rt.cpan.org/Public/Dist/Display.html?Name=Kelp-Module-FlashMessage>.
120             I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
121              
122             =head1 SUPPORT
123              
124             You can find documentation for this module with the perldoc command.
125              
126             perldoc Kelp::Module::FlashMessage
127              
128              
129             You can also look for information at:
130              
131             =over 4
132              
133             =item * RT: CPAN's request tracker
134              
135             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Kelp-Module-FlashMessage>
136              
137             =item * AnnoCPAN: Annotated CPAN documentation
138              
139             L<http://annocpan.org/dist/Kelp-Module-FlashMessage>
140              
141             =item * CPAN Ratings
142              
143             L<http://cpanratings.perl.org/d/Kelp-Module-FlashMessage>
144              
145             =item * Search CPAN
146              
147             L<http://search.cpan.org/dist/Kelp-Module-FlashMessage/>
148              
149             =back
150              
151              
152             =head1 ACKNOWLEDGEMENTS
153              
154             To Stefan Geneshky, the creator of great Kelp web framework
155              
156             =head1 COPYRIGHT & LICENSE
157              
158             This program is free software; you can redistribute it and/or modify it
159             under the terms of either: the GNU General Public License as published
160             by the Free Software Foundation; or the Artistic License.
161              
162             See http://dev.perl.org/licenses/ for more information.
163              
164             =cut