File Coverage

blib/lib/Dancer/Session/Storable.pm
Criterion Covered Total %
statement 21 43 48.8
branch 0 12 0.0
condition n/a
subroutine 7 13 53.8
pod 5 6 83.3
total 33 74 44.5


line stmt bran cond sub pod time code
1             package Dancer::Session::Storable;
2              
3 1     1   37613 use strict;
  1         2  
  1         38  
4 1     1   5 use warnings;
  1         2  
  1         33  
5 1     1   6 use base 'Dancer::Session::Abstract';
  1         5  
  1         2945  
6 1     1   291649 use vars qw($VERSION);
  1         4  
  1         51  
7              
8 1     1   6 use Dancer::ModuleLoader;
  1         2  
  1         23  
9 1     1   6 use Dancer::Config 'setting';
  1         2  
  1         49  
10 1     1   7 use Dancer::FileUtils 'path';
  1         2  
  1         504  
11              
12             $VERSION = '0.06';
13              
14             # static
15              
16             sub init {
17 0     0 1   my ($class) = @_;
18              
19 0           $class->SUPER::init(@_);
20              
21 0 0         die "Storable is needed and is not installed"
22             unless Dancer::ModuleLoader->load('Storable');
23              
24             # default value for session_dir
25 0 0         setting('session_dir' => path(setting('appdir'), 'sessions'))
26             if not defined setting('session_dir');
27              
28             # make sure session_dir exists
29 0           my $session_dir = setting('session_dir');
30 0 0         if (!-d $session_dir) {
31 0 0         mkdir $session_dir
32             or die "session_dir $session_dir cannot be created";
33             }
34             Dancer::Logger::core(
35 0           __PACKAGE__ . " using session_dir : $session_dir"
36             );
37             }
38              
39             # create a new session and return the newborn object
40             # representing that session
41             sub create {
42 0     0 1   my ($class) = @_;
43              
44 0           my $self = Dancer::Session::Storable->new;
45 0           $self->flush;
46 0           return $self;
47             }
48              
49             # Return the session object corresponding to the given id
50             sub retrieve {
51 0     0 1   my ($class, $id) = @_;
52              
53 0 0         return undef unless -f $class->session_file($id);
54 0           return Storable::lock_retrieve($class->session_file($id));
55             }
56              
57             # instance
58              
59             sub session_file {
60 0     0 0   my ($class,$id) = @_;
61 0           return path(
62             setting('session_dir'),
63             $class->session_name . "_$id.stor"
64             );
65             }
66              
67             sub destroy {
68 0     0 1   my ($self) = @_;
69 0 0         unlink $self->session_file($self->id)
70             if -f $self->session_file($self->id);
71             }
72              
73             sub flush {
74 0     0 1   my $self = shift;
75 0           Storable::lock_nstore($self, $self->session_file($self->id));
76 0           return $self;
77             }
78              
79             1;
80             __END__