[Jifty-commit] r3738 - in jifty/trunk: lib/Jifty/Web t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Jul 30 16:57:22 EDT 2007


Author: sartak
Date: Mon Jul 30 16:57:21 2007
New Revision: 3738

Added:
   jifty/trunk/t/13-sessions.t
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Web/Session.pm

Log:
 r29652 at caladan:  sartak | 2007-07-30 16:56:47 -0400
 Add a load_by_kv to Jifty::Web::Session


Modified: jifty/trunk/lib/Jifty/Web/Session.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/Session.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/Session.pm	Mon Jul 30 16:57:21 2007
@@ -76,6 +76,44 @@
     $self->{cache} = undef;
 }
 
+=head2 load_by_kv key => value OR key, default, callback
+
+Load up the current session from the given (key, value) pair. If no matching
+session could be found, it will create a new session with the key, value set.
+Be sure that what you're loading by is unique. If you're loading a session
+based on, say, a timestamp, then you're asking for trouble.
+
+The second form is used when you have a complex value. Each value for the
+given key is passed to the callback. The callback should return true if
+there's a match. The default value is used to create a new entry when no
+value was chosen.
+
+=cut
+
+sub load_by_kv {
+    my $self = shift;
+    my $k = shift;
+    my $v = shift;
+    my $callback = shift || sub { $_[0] eq $v };
+    my $session_id;
+
+    # tried doing this with load_by_cols but it never returned any rows
+    my $sessions = Jifty::Model::SessionCollection->new;
+    $sessions->limit(column => 'key_type', value => 'key');
+    $sessions->limit(column => 'data_key', value => $k );
+
+    while (my $row = $sessions->next) {
+        my $match = $callback->($row->value);
+        if ($match) {
+            $session_id = $row->session_id;
+            last;
+        }
+    }
+
+    $self->load($session_id);
+    $self->set($k => $v) if !$session_id;
+}
+
 sub _get_session_id_from_client {
         my $self = shift;
         my %cookies    = CGI::Cookie->fetch();

Added: jifty/trunk/t/13-sessions.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/13-sessions.t	Mon Jul 30 16:57:21 2007
@@ -0,0 +1,98 @@
+use strict;
+use warnings;
+
+=head1 DESCRIPTION
+
+Tests Jifty::Web::Session
+
+=cut
+
+use Jifty::Test tests => 31;
+
+my ($first_id, $second_id, $third_id);
+
+{
+    my $session = Jifty::Web::Session->new();
+    $session->load_by_kv(user => 'first');
+    ok($session->id, "got a session");
+    $first_id = $session->id;
+
+    $session->set(hello => 'world');
+    $session->set(number => '1st');
+
+    is($session->get('hello'),  'world', "immediate 'get' works");
+    is($session->get('number'), '1st',   "immediate 'get' works");
+
+    $session->load_by_kv(user => 'first');
+    is($session->id, $first_id, "same session as before");
+    is($session->get('hello'),  'world', "'get' before destroy works");
+    is($session->get('number'), '1st',   "'get' before destroy works");
+}
+
+{
+    my $session = Jifty::Web::Session->new();
+    $session->load_by_kv(user => 'first');
+    ok($session->id, "got a session");
+    is($session->id, $first_id, "same session as before");
+
+    is($session->get('hello'),  'world', "'set', destroy, 'get' works");
+    is($session->get('number'), '1st',   "'set', destroy, 'get' works");
+}
+
+{
+    my $session = Jifty::Web::Session->new();
+    $session->load_by_kv(user => 'second');
+    ok($session->id, "got a session");
+    isnt($session->id, $first_id, "NOT same session as before");
+    $second_id = $session->id;
+
+    is($session->get('hello'),  undef, "different value gives different session");
+    is($session->get('number'), undef, "different value gives different session");
+
+    $session->set(hello => 'world');
+    $session->set(number => '2nd');
+}
+
+{
+    my $session = Jifty::Web::Session->new();
+    $session->load_by_kv(user => 'first');
+    ok($session->id, "got a session");
+    is($session->id, $first_id, "first session again");
+
+    is($session->get('hello'), 'world');
+    is($session->get('number'), '1st', "even though the two sessions have some overlapping keys, the one that matters doesn't overlap");
+}
+
+# the three-arg form
+
+{
+    my $session = Jifty::Web::Session->new();
+    $session->load_by_kv('user', 'first', sub { $_[0] =~ /^f/ } );
+    ok($session->id, "got a session");
+    is($session->id, $first_id, "first session again");
+    is($session->get('number'), '1st');
+}
+
+{
+    my $session = Jifty::Web::Session->new();
+    $session->load_by_kv('user', 'third', sub { $_[0] =~ /\b(thi|3)rd\b/ } );
+    ok($session->id, "got a session");
+    $third_id = $session->id;
+
+    isnt($session->id, $first_id,  "not first session");
+    isnt($session->id, $second_id, "not second session");
+    is($session->get('number'), undef);
+    $session->set(number => '3rd');
+    is($session->get('number'), '3rd');
+}
+
+{
+    my $session = Jifty::Web::Session->new();
+    $session->load_by_kv('user', 'third', sub { $_[0] =~ /\b(thi|3)rd\b/ } );
+    ok($session->id, "got a session");
+    isnt($session->id, $first_id,  "not first session");
+    isnt($session->id, $second_id, "not second session");
+    is($session->id,   $third_id, "third session again");
+    is($session->get('number'), '3rd');
+}
+


More information about the Jifty-commit mailing list