| CatalystX-Usul documentation | Contained in the CatalystX-Usul distribution. |
CatalystX::Usul::PersistentState - Set/Get state information on/from the session store
0.3.$Revision: 576 $
use CatalystX::Usul::PersistentState;
Uses the session store to provide state information that is persistent across requests
my $value = $self->get_key( $c, $key_name );
Returns a value for a given key from stash which was populated by load_keys
Recovers the key(s) for the current endpoint. First it will look at then request parameters, if they are not set it will look in the session store, if that is not set then it will use the configuration defaults if they exist, inflating values from the stash if necessary
$self->reset_keys( $c );
Resets this requests keys in the stash
$self->session_key( $c );
Returns the session store key for the current controller
$self->set_key( $c, $key_name, $value );
Sets a key/value pair in the session store
None
None
There are no known incompatibilities in this module
There are no known bugs in this module. Please report problems to the address below. Patches are welcome
Peter Flanigan, <Support at RoxSoft.co.uk>
Copyright (c) 2008 Peter Flanigan. All rights reserved
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic
This program is distributed in the hope that it will be useful, but WITHOUT WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
| CatalystX-Usul documentation | Contained in the CatalystX-Usul distribution. |
# @(#)$Id: PersistentState.pm 576 2009-06-09 23:23:46Z pjf $ package CatalystX::Usul::PersistentState; use strict; use warnings; use version; our $VERSION = qv( sprintf '0.3.%d', q$Rev: 576 $ =~ /\d+/gmx ); use parent qw(CatalystX::Usul::Base); my $NUL = q(); sub get_key { my ($self, $c, $name) = @_; my $ckeys = $c->stash->{ckeys}; return exists $ckeys->{ $name } ? $ckeys->{ $name } : $NUL; } sub load_keys { # Recover the previous key field values for the requested # controller. Select the first true value from; the request # parameters, the session store, a default value as defined # in the config and loaded into the stash my ($self, $c) = @_; my ($conf_keys, $val); my $s = $c->stash; my $model = $c->model( q(Base) ); if ($conf_keys = $s->{keys}->{ $s->{form}->{name} }) { my $session = $c->session->{ $self->session_key( $c ) }; $self->reset_keys( $c ); # Clear the per request keys while (my ($key, $conf) = each %{ $conf_keys->{vals} }) { unless (defined ($val = $model->query_value( $key ))) { unless (defined ($val = $session->{ $key })) { if (($val = $conf->{key}) && ($val =~ m{ \[% \s+ (.*) \s+ %\] }msx)) { $val = $s->{ $1 } if ($1); } $val ||= $NUL; } } $self->set_key( $c, $key, $val ); # This will persist across requests } } return; } sub reset_keys { my ($self, $c) = @_; $c->stash( ckeys => {} ); return; } sub session_key { my ($self, $c) = @_; return $c->action->namespace || q(root); } sub set_key { # Save the value in the session for this controller my ($self, $c, $name, $val) = @_; return $self->get_key( $c, $name ) unless (defined $val); my $skey = $self->session_key( $c ); $c->stash->{ckeys}->{ $name } = $c->session->{ $skey }->{ $name } = $val; return $val; } 1; __END__
# Local Variables: # mode: perl # tab-width: 3 # End: