[Jifty-commit] r3761 - in jifty/trunk: lib/Jifty/Web t
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Wed Aug 1 15:15:32 EDT 2007
Author: jesse
Date: Wed Aug 1 15:15:32 2007
New Revision: 3761
Modified:
jifty/trunk/ (props changed)
jifty/trunk/lib/Jifty/Web/Session.pm
jifty/trunk/t/13-sessions.t
Log:
r65037 at pinglin: jesse | 2007-08-01 15:11:00 -0400
* clean up load_by_kv
Modified: jifty/trunk/lib/Jifty/Web/Session.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/Session.pm (original)
+++ jifty/trunk/lib/Jifty/Web/Session.pm Wed Aug 1 15:15:32 2007
@@ -5,6 +5,7 @@
use base qw/Jifty::Object/;
use CGI::Cookie ();
use DateTime ();
+use Storable ();
=head1 NAME
@@ -76,42 +77,39 @@
$self->{cache} = undef;
}
-=head2 load_by_kv key => value OR key, default, callback
+=head2 load_by_kv key => value
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 $k = shift;
+ my $v = shift;
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 );
+ $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;
- }
+ # XXX TODO: we store this data in a storable. so we now want to match on the storable version
+ # It would be so nice if Jifty::DBI could do this for us.
+ $Storable::Deparse = 1;
+ my $value = Storable::nfreeze(\$v);
+
+ $sessions->limit( column => 'value' => value => $value );
+
+ while ( my $row = $sessions->next ) {
+ $session_id = $row->session_id;
+ last;
}
-
$self->load($session_id);
- $self->set($k => $v) if !$session_id;
+ $self->set( $k => $v ) if !$session_id;
}
sub _get_session_id_from_client {
Modified: jifty/trunk/t/13-sessions.t
==============================================================================
--- jifty/trunk/t/13-sessions.t (original)
+++ jifty/trunk/t/13-sessions.t Wed Aug 1 15:15:32 2007
@@ -7,7 +7,7 @@
=cut
-use Jifty::Test tests => 31;
+use Jifty::Test tests => 18;
my ($first_id, $second_id, $third_id);
@@ -63,36 +63,3 @@
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