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   2340 use strict;
  4         9  
  4         183  
3 4     4   22 use warnings;
  4         4  
  4         192  
4              
5             our $VERSION = '0.30';
6             our $AUTHORITY = 'cpan:STEVAN';
7              
8 4     4   1410 use Storable ();
  4         5207  
  4         73  
9              
10 4     4   18 use parent 'Plack::Session::Store';
  4         6  
  4         16  
11              
12 4         17 use Plack::Util::Accessor qw[
13             dir
14             serializer
15             deserializer
16 4     4   179 ];
  4         7  
17              
18             sub new {
19 3     3 1 1845 my ($class, %params) = @_;
20              
21 3   0     13 $params{'dir'} ||= $ENV{TMPDIR} || '/tmp';
      33        
22              
23 3 50       31 die "Storage directory (" . $params{'dir'} . ") is not writeable"
24             unless -w $params{'dir'};
25              
26 3   50 16   37 $params{'serializer'} ||= sub { Storable::lock_nstore( @_ ) };
  16         92  
27 3   50 12   23 $params{'deserializer'} ||= sub { Storable::lock_retrieve( @_ ) };
  12         89  
28              
29 3         30 bless { %params } => $class;
30             }
31              
32             sub fetch {
33 14     14 1 179114 my ($self, $session_id) = @_;
34              
35 14         34 my $file_path = $self->_get_session_file_path( $session_id );
36 14 100       371 return unless -f $file_path;
37              
38 12         37 $self->deserializer->( $file_path );
39             }
40              
41             sub store {
42 16     16 1 104 my ($self, $session_id, $session) = @_;
43 16         36 my $file_path = $self->_get_session_file_path( $session_id );
44 16         143 $self->serializer->( $session, $file_path );
45             }
46              
47             sub remove {
48 2     2 1 14 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   37 my ($self, $session_id) = @_;
54 32         81 $self->dir . '/' . $session_id;
55             }
56              
57             1;
58              
59             __END__