File Coverage

blib/lib/Catalyst/Plugin/Flash.pm
Criterion Covered Total %
statement 17 40 42.5
branch 0 8 0.0
condition 0 3 0.0
subroutine 6 12 50.0
pod 0 6 0.0
total 23 69 33.3


line stmt bran cond sub pod time code
1 1     1   378386 use 5.008001; use strict; use warnings;
  1     1   5  
  1     1   6  
  1         1  
  1         27  
  1         6  
  1         6  
  1         118  
2              
3             package Catalyst::Plugin::Flash;
4              
5             our $VERSION = '0.002';
6              
7 1     1   793 use URI ();
  1         9012  
  1         603  
8              
9             sub flash {
10 0     0 0   my ( $c, $uri, %var ) = ( shift, @_ );
11 0           ( my $location = URI->new_abs( $uri, $c->request->uri )->as_string ) =~ s!#.*!!s;
12 0           @{ $c->{(__PACKAGE__)}{ $location } }{ keys %var } = values %var;
  0            
13 0           $uri;
14             }
15              
16 0     0 0   sub clear_flash { %{ $_[0]{(__PACKAGE__)} } = () }
  0            
17              
18             ########################################################################
19              
20 0     0 0   sub flash_to_cookie { shift; @_ }
  0            
21 0     0 0   sub flash_from_cookie { shift; @_ }
  0            
22              
23             ########################################################################
24              
25             sub prepare_flash {
26 0     0 0   my $c = shift;
27 0           ( my $location = $c->request->uri->as_string ) =~ s!#.*!!s;
28 0 0         my $cookie = $c->request->cookie( $location ) or return;
29 0           $c->stash( $c->flash_from_cookie( $cookie->value ) );
30 0           $c->response->cookies->{ $cookie->name } = { expires => '-1y', value => '' };
31             }
32              
33             sub finalize_flash {
34 0     0 0   my $c = shift;
35 0 0         my $flash = $c->{(__PACKAGE__)} or return;
36 0 0 0       my $uri = $c->response->status =~ /\A(?:30[12378]|201)\z/ && $c->response->location or return;
37 0           ( my $location = URI->new_abs( $uri, $c->request->uri )->as_string ) =~ s!#.*!!s;
38 0 0         my @value = $c->flash_to_cookie( %{ $flash->{ $location } or return } );
  0            
39 0           $c->response->cookies->{ $location } = { expires => '+1m', value => \@value };
40             }
41              
42             ########################################################################
43              
44             # use Catalyst 5.80004 ();
45 1     1   737 use Moose::Role;
  1         991527  
  1         5  
46             after prepare_path => sub { shift->prepare_flash };
47             before finalize_cookies => sub { shift->finalize_flash };
48 1     1   8308 no Moose::Role;
  1         3  
  1         7  
49              
50             1;
51              
52             __END__
53              
54             =pod
55              
56             =encoding UTF-8
57              
58             =head1 NAME
59              
60             Catalyst::Plugin::Flash - put values on the stash of the next request
61              
62             =head1 DESCRIPTION
63              
64             This plugin will allow providing values that will automatically be placed
65             on the stash during a subsequent request.
66              
67             =head1 AUTHOR
68              
69             Aristotle Pagaltzis <pagaltzis@gmx.de>
70              
71             =head1 COPYRIGHT AND LICENSE
72              
73             This software is copyright (c) 2024 by Aristotle Pagaltzis.
74              
75             This is free software; you can redistribute it and/or modify it under
76             the same terms as the Perl 5 programming language system itself.
77              
78             =cut