File Coverage

blib/lib/Dancer/Plugin/WindowSession.pm
Criterion Covered Total %
statement 18 41 43.9
branch 0 14 0.0
condition n/a
subroutine 6 9 66.6
pod n/a
total 24 64 37.5


line stmt bran cond sub pod time code
1             package Dancer::Plugin::WindowSession;
2              
3 1     1   29608 use strict;
  1         3  
  1         39  
4 1     1   5 use warnings;
  1         2  
  1         27  
5 1     1   799 use Clone;
  1         3393  
  1         51  
6 1     1   8 use Carp;
  1         2  
  1         75  
7              
8             our $VERSION = '0.02';
9              
10 1     1   1049 use Dancer ':syntax';
  1         360399  
  1         6  
11 1     1   1304 use Dancer::Plugin;
  1         1632  
  1         639  
12              
13             sub _get_window_session_object
14             {
15 0     0     my $win_sess_id ; ## The window session ID, from the HTTP request.
16             my $win_sess_obj ; ## The Dancer::Session::Abstract object
17              
18             ## Is there a cached version of the window-session object ?
19 0 0         if (defined(var 'window_session_object')) {
20 0           $win_sess_obj = var 'window_session_object';
21 0           $win_sess_id = $win_sess_obj->id;
22 0           return $win_sess_obj;
23             }
24              
25              
26             # This is the first time 'window_session' is request during this request handling,
27             # check if we have one in the HTTP Request
28 0           $win_sess_id = param 'winsid';
29              
30 0 0         my $session_engine = engine 'session'
31             or die "Can't find session engine";
32              
33             # If we have a Window-session-Id, try to retrieve the corresponding hash.
34 0 0         if ($win_sess_id) {
35 0           $win_sess_obj = $session_engine->retrieve($win_sess_id);
36              
37             ## If we failed to retrieve existing window-session, force a new one
38 0 0         $win_sess_id = undef unless $win_sess_obj;
39             }
40              
41             # If anything failed along the way, just create a new window session
42 0 0         if (!$win_sess_id) {
43 0           $win_sess_obj = $session_engine->create();
44 0           $win_sess_id = $win_sess_obj->id;
45              
46             }
47             ## Cache the new window-session object,
48             ## for other route handlers downstream in the current request handling.
49 0           var 'window_session_object' => $win_sess_obj;
50              
51 0           return $win_sess_obj;
52             }
53              
54             hook before => sub {
55             my $window_session = _get_window_session_object;
56             my $window_session_id = $window_session->id;
57             };
58              
59             hook before_template_render => sub {
60             my $tokens = shift ;
61              
62             my $window_session = _get_window_session_object;
63              
64             $tokens->{winsid} = $window_session->id;
65             $tokens->{window_session} = Clone::clone($window_session);
66             };
67              
68             hook after => sub {
69             my $window_session = _get_window_session_object;
70             my $window_session_id = $window_session->id;
71             $window_session->flush();
72             };
73              
74             register window_session_id => sub {
75 0     0     my $window_session = _get_window_session_object;
76 0           return $window_session->id;
77             };
78              
79              
80             register window_session => sub {
81 0     0     my $window_session = _get_window_session_object;
82 0           my $window_session_id = $window_session->id;
83              
84 0           my ($key,$value) = @_;
85 0 0         $key eq 'id' and croak 'Can\'t store to window_session key with name "id"';
86              
87 0 0         $window_session->{$key} = $value if (@_==2);
88 0           return $window_session->{$key};
89             };
90              
91             register_plugin;
92              
93             # ABSTRACT: A Dancer plugin for managing Browser Window Sessions
94              
95             1;
96             __END__