File Coverage

blib/lib/Plack/Session/Store/File.pm
Criterion Covered Total %
statement 37 37 100.0
branch 3 4 75.0
condition 3 12 25.0
subroutine 13 13 100.0
pod 4 4 100.0
total 60 70 85.7


line stmt bran cond sub pod time code
1             package Plack::Session::Store::File;
2 4     4   3173 use strict;
  4         29  
  4         173  
3 4     4   22 use warnings;
  4         30  
  4         343  
4              
5             our $VERSION = '0.36';
6             our $AUTHORITY = 'cpan:STEVAN';
7              
8 4     4   576 use Storable ();
  4         3833  
  4         94  
9 4     4   29 use File::Spec ();
  4         12  
  4         98  
10              
11 4     4   18 use parent 'Plack::Session::Store';
  4         16  
  4         62  
12              
13 4         22 use Plack::Util::Accessor qw[
14             dir
15             serializer
16             deserializer
17 4     4   266 ];
  4         20  
18              
19             sub new {
20 3     3 1 836745 my ($class, %params) = @_;
21              
22 3   0     20 $params{'dir'} ||= $ENV{TMPDIR} || File::Spec->tmpdir;
      33        
23              
24             die "Storage directory (" . $params{'dir'} . ") is not writeable"
25 3 50       59 unless -w $params{'dir'};
26              
27 3   33 16   43 $params{'serializer'} ||= sub { Storable::lock_nstore( @_ ) };
  16         130  
28 3   33 12   27 $params{'deserializer'} ||= sub { Storable::lock_retrieve( @_ ) };
  12         146  
29              
30 3         46 bless { %params } => $class;
31             }
32              
33             sub fetch {
34 14     14 1 278307 my ($self, $session_id) = @_;
35              
36 14         42 my $file_path = $self->_get_session_file_path( $session_id );
37 14 100       554 return unless -f $file_path;
38              
39 12         74 $self->deserializer->( $file_path );
40             }
41              
42             sub store {
43 16     16 1 150 my ($self, $session_id, $session) = @_;
44 16         49 my $file_path = $self->_get_session_file_path( $session_id );
45 16         151 $self->serializer->( $session, $file_path );
46             }
47              
48             sub remove {
49 2     2 1 19 my ($self, $session_id) = @_;
50 2         8 unlink $self->_get_session_file_path( $session_id );
51             }
52              
53             sub _get_session_file_path {
54 32     32   126 my ($self, $session_id) = @_;
55 32         150 $self->dir . '/' . $session_id;
56             }
57              
58             1;
59              
60             __END__