File Coverage

blib/lib/Dancer2/Core/Session.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 3 3 100.0
total 26 26 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Core::Session;
2             # ABSTRACT: class to represent any session object
3             $Dancer2::Core::Session::VERSION = '2.0.1';
4 126     126   388162 use Moo;
  126         24431  
  126         1283  
5 126     126   62734 use Dancer2::Core::Types;
  126         326  
  126         1449  
6 126     126   1976619 use Dancer2::Core::Time;
  126         1628  
  126         60792  
7              
8             has id => (
9             # for some specific plugins this should be rw.
10             # refer to https://github.com/PerlDancer/Dancer2/issues/460
11             is => 'rw',
12             isa => Str,
13             required => 1,
14             );
15              
16             has data => (
17             is => 'ro',
18             lazy => 1,
19             default => sub { {} },
20             );
21              
22             has expires => (
23             is => 'rw',
24             isa => Str,
25             coerce => sub {
26             my $value = shift;
27             $value += time if $value =~ /^[\-\+]?\d+$/;
28             Dancer2::Core::Time->new( expression => $value )->epoch;
29             },
30             );
31              
32             has is_dirty => (
33             is => 'rw',
34             isa => Bool,
35             default => sub {0},
36             );
37              
38              
39             sub read {
40 49     49 1 1507 my ( $self, $key ) = @_;
41 49         1099 return $self->data->{$key};
42             }
43              
44              
45             sub write {
46 55     55 1 1063 my ( $self, $key, $value ) = @_;
47 55         1416 $self->is_dirty(1);
48 55         2878 $self->data->{$key} = $value;
49             }
50              
51             sub delete {
52 9     9 1 25 my ( $self, $key, $value ) = @_;
53 9         191 $self->is_dirty(1);
54 9         417 delete $self->data->{$key};
55             }
56              
57             1;
58              
59             __END__