Checking in changes prior to tagging of version 0.14.
[catagits/Web-Session.git] / lib / Plack / Session.pm
index 6473b27..dff6672 100644 (file)
@@ -2,48 +2,61 @@ package Plack::Session;
 use strict;
 use warnings;
 
-use Plack::Util::Accessor qw[
-    id
-    store
-    state
-];
+our $VERSION   = '0.14';
+our $AUTHORITY = 'cpan:STEVAN';
+
+use Plack::Util::Accessor qw( session options );
 
 sub new {
-    my ($class, %params) = @_;
-    my $request = delete $params{'request'};
-    $params{'id'} = $params{'state'}->get_session_id( $request );
-    bless { %params } => $class;
+    my ($class, $env) = @_;
+    bless {
+        session => $env->{'psgix.session'},
+        options => $env->{'psgix.session.options'},
+    }, $class;
+}
+
+sub id {
+    my $self = shift;
+    $self->options->{id};
 }
 
 ## Data Managment
 
+sub dump {
+    my $self = shift;
+    $self->session;
+}
+
 sub get {
     my ($self, $key) = @_;
-    $self->store->fetch( $self->id, $key )
+    $self->session->{$key};
 }
 
 sub set {
     my ($self, $key, $value) = @_;
-    $self->store->store( $self->id, $key, $value );
+    delete $self->options->{no_store};
+    $self->session->{$key} = $value;
 }
 
 sub remove {
     my ($self, $key) = @_;
-    $self->store->delete( $self->id, $key );
+    delete $self->options->{no_store};
+    delete $self->session->{$key};
+}
+
+sub keys {
+    my $self = shift;
+    keys %{$self->session};
 }
 
 ## Lifecycle Management
 
 sub expire {
     my $self = shift;
-    $self->store->cleanup( $self->id );
-    $self->state->expire_session_id( $self->id );
-}
-
-sub finalize {
-    my ($self, $response) = @_;
-    $self->store->persist( $self->id, $response );
-    $self->state->finalize( $self->id, $response );
+    for my $key ($self->keys) {
+        delete $self->session->{$key};
+    }
+    $self->options->{expire} = 1;
 }
 
 1;
@@ -58,40 +71,48 @@ Plack::Session - Middleware for session management
 
 =head1 SYNOPSIS
 
+  # Use with Middleware::Session
+  enable "Session";
+
+  # later in your app
   use Plack::Session;
+  my $app = sub {
+      my $env = shift;
+      my $session = Plack::Session->new($env);
+
+      $session->id;
+      $session->get($key);
+      $session->set($key, $value);
+      $session->remove($key);
+      $session->keys;
+
+      $session->expire;
+  };
 
 =head1 DESCRIPTION
 
+This is the core session object, you probably want to look
+at L<Plack::Middleware::Session>, unless you are writing your
+own session middleware component.
+
 =head1 METHODS
 
 =over 4
 
-=item B<new ( %params )>
+=item B<new ( $env )>
 
-The constructor expects keys in C<%params> for I<state>,
-I<store> and I<request>. The I<request> param is expected to be
-a L<Plack::Request> instance or an object with an equivalent
-interface.
+The constructor takes a PSGI request env hash reference.
 
 =item B<id>
 
 This is the accessor for the session id.
 
-=item B<state>
-
-This is expected to be a L<Plack::Session::State> instance or
-an object with an equivalent interface.
-
-=item B<store>
-
-This is expected to be a L<Plack::Session::Store> instance or
-an object with an equivalent interface.
-
 =back
 
-=head2 Session Data Storage
+=head2 Session Data Management
 
-These methods delegate to appropriate methods on the C<store>.
+These methods allows you to read and write the session data like
+Perl's normal hash.
 
 =over 4
 
@@ -101,6 +122,10 @@ These methods delegate to appropriate methods on the C<store>.
 
 =item B<remove ( $key )>
 
+=item B<keys>
+
+=item B<session>, B<dump>
+
 =back
 
 =head2 Session Lifecycle Management
@@ -109,18 +134,7 @@ These methods delegate to appropriate methods on the C<store>.
 
 =item B<expire>
 
-This method can be called to expire the current session id. It
-will call the C<cleanup> method on the C<store> and the C<finalize>
-method on the C<state>, passing both of them the session id and
-the C<$response>.
-
-=item B<finalize ( $response )>
-
-This method should be called at the end of the response cycle. It
-will call the C<persist> method on the C<store> and the
-C<expire_session_id> method on the C<state>, passing both of them
-the session id. The C<$response> is expected to be a L<Plack::Response>
-instance or an object with an equivalent interface.
+This method can be called to expire the current session id.
 
 =back
 
@@ -136,7 +150,7 @@ Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2009 Infinity Interactive, Inc.
+Copyright 2009, 2010 Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>