Rewrote all the tests and now all tests pass!
[catagits/Web-Session.git] / lib / Plack / Session.pm
CommitLineData
06190e8b 1package Plack::Session;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
4ff41723 8use Plack::Util::Accessor qw( manager _data options );
06190e8b 9
10sub new {
11 my ($class, %params) = @_;
ac4892f4 12 bless { %params } => $class;
06190e8b 13}
14
85265792 15sub id {
16 my $self = shift;
17 $self->options->{id};
18}
19
06190e8b 20## Data Managment
21
4a0cb5a0 22sub dump {
23 my $self = shift;
4ff41723 24 $self->_data;
4a0cb5a0 25}
26
06190e8b 27sub get {
28 my ($self, $key) = @_;
4ff41723 29 $self->_data->{$key};
06190e8b 30}
31
32sub set {
33 my ($self, $key, $value) = @_;
4ff41723 34 delete $self->options->{no_commit};
35 $self->_data->{$key} = $value;
06190e8b 36}
37
38sub remove {
39 my ($self, $key) = @_;
4ff41723 40 delete $self->options->{no_commit};
41 delete $self->_data->{$key};
4a0cb5a0 42}
43
44sub keys {
45 my $self = shift;
4ff41723 46 keys %{$self->_data};
06190e8b 47}
48
49## Lifecycle Management
50
51sub expire {
52 my $self = shift;
85265792 53 for my $key ($self->keys) {
54 delete $self->_data->{$key};
55 }
4ff41723 56 $self->options->{expire} = 1;
caf3bd90 57}
58
59sub commit {
60 my $self = shift;
4ff41723 61 $self->options->{no_commit} = 1;
62 $self->manager->commit($self->_data, $self->options);
06190e8b 63}
64
ac4892f4 651;
66
67__END__
68
69=pod
70
71=head1 NAME
72
73Plack::Session - Middleware for session management
74
75=head1 SYNOPSIS
76
4ff41723 77 # Use with Middleware::Session
78 enable "Session", session_class => "Plack::Session";
ac4892f4 79
4ff41723 80 use Plack::Session;
3d92cf47 81
4ff41723 82 my $session =
3d92cf47 83
3d92cf47 84
ac4892f4 85=head1 DESCRIPTION
86
3d92cf47 87This is the core session object, you probably want to look
d5abacda 88at L<Plack::Middleware::Session>, unless you are writing your
3d92cf47 89own session middleware component.
90
ac4892f4 91=head1 METHODS
92
93=over 4
94
95=item B<new ( %params )>
96
43f34c01 97The constructor expects keys in C<%params> for I<state>,
98I<store> and I<request>. The I<request> param is expected to be
99a L<Plack::Request> instance or an object with an equivalent
100interface.
101
ac4892f4 102=item B<id>
103
43f34c01 104This is the accessor for the session id.
105
ac4892f4 106=item B<state>
107
43f34c01 108This is expected to be a L<Plack::Session::State> instance or
109an object with an equivalent interface.
110
ac4892f4 111=item B<store>
112
43f34c01 113This is expected to be a L<Plack::Session::Store> instance or
114an object with an equivalent interface.
115
ac4892f4 116=back
117
4a0cb5a0 118=head2 Session Data Management
43f34c01 119
4a0cb5a0 120These methods allows you to read and write the session data like
121Perl's normal hash. The operation is not synced to the storage until
122you call C<finalize> on it.
43f34c01 123
ac4892f4 124=over 4
125
126=item B<get ( $key )>
127
128=item B<set ( $key, $value )>
129
130=item B<remove ( $key )>
131
4a0cb5a0 132=item B<keys>
133
ac4892f4 134=back
135
43f34c01 136=head2 Session Lifecycle Management
137
ac4892f4 138=over 4
139
caf3bd90 140=item B<commit>
141
142This method synchronizes the session data to the data store, without
143waiting for the response final phase.
144
ac4892f4 145=item B<expire>
146
caf3bd90 147This method can be called to expire the current session id. It marks
148the session as expire and call the C<cleanup> method on the C<store>
149and the C<expire_session_id> method on the C<state>.
43f34c01 150
caf3bd90 151=item B<finalize ( $manager, $response )>
ac4892f4 152
caf3bd90 153This method should be called at the end of the response cycle. It will
154call the C<store> method on the C<store> and the C<expire_session_id>
155method on the C<state>. The C<$response> is expected to be a
156L<Plack::Response> instance or an object with an equivalent interface.
43f34c01 157
ac4892f4 158=back
159
160=head1 BUGS
161
162All complex software has bugs lurking in it, and this module is no
163exception. If you find a bug please either email me, or add the bug
164to cpan-RT.
165
166=head1 AUTHOR
167
168Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
169
170=head1 COPYRIGHT AND LICENSE
171
000c696e 172Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 173
174L<http://www.iinteractive.com>
175
176This library is free software; you can redistribute it and/or modify
177it under the same terms as Perl itself.
178
179=cut
180