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             # ABSTRACT: class to represent any session object
2             $Dancer2::Core::Session::VERSION = '0.400000';
3             use Moo;
4 113     113   66185 use Dancer2::Core::Types;
  113         6621  
  113         797  
5 113     113   34803 use Dancer2::Core::Time;
  113         324  
  113         875  
6 113     113   920763  
  113         295  
  113         41802  
7             has id => (
8             # for some specific plugins this should be rw.
9             # refer to https://github.com/PerlDancer/Dancer2/issues/460
10             is => 'rw',
11             isa => Str,
12             required => 1,
13             );
14              
15             has data => (
16             is => 'ro',
17             lazy => 1,
18             default => sub { {} },
19             );
20              
21             has expires => (
22             is => 'rw',
23             isa => Str,
24             coerce => sub {
25             my $value = shift;
26             $value += time if $value =~ /^[\-\+]?\d+$/;
27             Dancer2::Core::Time->new( expression => $value )->epoch;
28             },
29             );
30              
31             has is_dirty => (
32             is => 'rw',
33             isa => Bool,
34             default => sub {0},
35             );
36              
37              
38             my ( $self, $key ) = @_;
39             return $self->data->{$key};
40 49     49 1 1126 }
41 49         767  
42              
43             my ( $self, $key, $value ) = @_;
44             $self->is_dirty(1);
45             $self->data->{$key} = $value;
46 55     55 1 1392 }
47 55         962  
48 55         2271 my ( $self, $key, $value ) = @_;
49             $self->is_dirty(1);
50             delete $self->data->{$key};
51             }
52 9     9 1 26  
53 9         151 1;
54 9         349  
55              
56             =pod
57              
58             =encoding UTF-8
59              
60             =head1 NAME
61              
62             Dancer2::Core::Session - class to represent any session object
63              
64             =head1 VERSION
65              
66             version 0.400000
67              
68             =head1 DESCRIPTION
69              
70             A session object encapsulates anything related to a specific session: its ID,
71             its data, and its expiration.
72              
73             It is completely agnostic of how it will be stored, this is the role of
74             a factory that consumes L<Dancer2::Core::Role::SessionFactory> to know about that.
75              
76             Generally, session objects should not be created directly. The correct way to
77             get a new session object is to call the C<create()> method on a session engine
78             that implements the SessionFactory role. This is done automatically by the
79             app object if a session engine is defined.
80              
81             =head1 ATTRIBUTES
82              
83             =head2 id
84              
85             The identifier of the session object. Required. By default,
86             L<Dancer2::Core::Role::SessionFactory> sets this to a randomly-generated,
87             guaranteed-unique string.
88              
89             This attribute can be modified if your Session implementation requires this.
90              
91             =head2 data
92              
93             Contains the data of the session (Hash).
94              
95             =head2 expires
96              
97             Number of seconds for the expiry of the session cookie. Don't add the current
98             timestamp to it, will be done automatically.
99              
100             Default is no expiry (session cookie will leave for the whole browser's
101             session).
102              
103             For a lifetime of one hour:
104              
105             expires => 3600
106              
107             =head2 is_dirty
108              
109             Boolean value for whether data in the session has been modified.
110              
111             =head1 METHODS
112              
113             =head2 read
114              
115             Reader on the session data
116              
117             my $value = $session->read('something');
118              
119             Returns C<undef> if the key does not exist in the session.
120              
121             =head2 write
122              
123             Writer on the session data
124              
125             $session->write('something', $value);
126              
127             Sets C<is_dirty> to true. Returns C<$value>.
128              
129             =head2 delete
130              
131             Deletes a key from session data
132              
133             $session->delete('something');
134              
135             Sets C<is_dirty> to true. Returns the value deleted from the session.
136              
137             =head1 AUTHOR
138              
139             Dancer Core Developers
140              
141             =head1 COPYRIGHT AND LICENSE
142              
143             This software is copyright (c) 2022 by Alexis Sukrieh.
144              
145             This is free software; you can redistribute it and/or modify it under
146             the same terms as the Perl 5 programming language system itself.
147              
148             =cut