File Coverage

blib/lib/Plack/Session/Store/File.pm
Criterion Covered Total %
statement 34 34 100.0
branch 3 4 75.0
condition 3 9 33.3
subroutine 12 12 100.0
pod 4 4 100.0
total 56 63 88.8


line stmt bran cond sub pod time code
1             package Plack::Session::Store::File;
2 4     4   2528 use strict;
  4         7  
  4         156  
3 4     4   20 use warnings;
  4         5  
  4         265  
4              
5             our $VERSION = '0.23';
6             our $AUTHORITY = 'cpan:STEVAN';
7              
8 4     4   1651 use Storable ();
  4         6555  
  4         94  
9              
10 4     4   59 use parent 'Plack::Session::Store';
  4         6  
  4         30  
11              
12 4         18 use Plack::Util::Accessor qw[
13             dir
14             serializer
15             deserializer
16 4     4   234 ];
  4         7  
17              
18             sub new {
19 3     3 1 2187 my ($class, %params) = @_;
20              
21 3   0     17 $params{'dir'} ||= $ENV{TMPDIR} || '/tmp';
      33        
22              
23 3 50       44 die "Storage directory (" . $params{'dir'} . ") is not writeable"
24             unless -w $params{'dir'};
25              
26 3   50 16   46 $params{'serializer'} ||= sub { Storable::lock_nstore( @_ ) };
  16         111  
27 3   50 12   26 $params{'deserializer'} ||= sub { Storable::lock_retrieve( @_ ) };
  12         99  
28              
29 3         40 bless { %params } => $class;
30             }
31              
32             sub fetch {
33 14     14 1 195397 my ($self, $session_id) = @_;
34              
35 14         35 my $file_path = $self->_get_session_file_path( $session_id );
36 14 100       419 return unless -f $file_path;
37              
38 12         44 $self->deserializer->( $file_path );
39             }
40              
41             sub store {
42 16     16 1 122 my ($self, $session_id, $session) = @_;
43 16         55 my $file_path = $self->_get_session_file_path( $session_id );
44 16         139 $self->serializer->( $session, $file_path );
45             }
46              
47             sub remove {
48 2     2 1 17 my ($self, $session_id) = @_;
49 2         7 unlink $self->_get_session_file_path( $session_id );
50             }
51              
52             sub _get_session_file_path {
53 32     32   46 my ($self, $session_id) = @_;
54 32         86 $self->dir . '/' . $session_id;
55             }
56              
57             1;
58              
59             __END__