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.1.0';
4 129     129   472903 use Moo;
  129         27448  
  129         1370  
5 129     129   63407 use Dancer2::Core::Types;
  129         962  
  129         1837  
6 129     129   2035639 use Dancer2::Core::Time;
  129         352  
  129         64722  
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 1862 my ( $self, $key ) = @_;
41 49         884 return $self->data->{$key};
42             }
43              
44              
45             sub write {
46 55     55 1 1440 my ( $self, $key, $value ) = @_;
47 55         1205 $self->is_dirty(1);
48 55         2573 $self->data->{$key} = $value;
49             }
50              
51             sub delete {
52 9     9 1 21 my ( $self, $key, $value ) = @_;
53 9         148 $self->is_dirty(1);
54 9         297 delete $self->data->{$key};
55             }
56              
57             1;
58              
59             __END__