CatalystX::Usul::PersistentState - Set/Get state information on/from the session store


CatalystX-Usul documentation Contained in the CatalystX-Usul distribution.

Index


Code Index:

Name

Top

CatalystX::Usul::PersistentState - Set/Get state information on/from the session store

Version

Top

0.3.$Revision: 576 $

Synopsis

Top

   use CatalystX::Usul::PersistentState;

Description

Top

Uses the session store to provide state information that is persistent across requests

Subroutines/Methods

Top

get_key

   my $value = $self->get_key( $c, $key_name );

Returns a value for a given key from stash which was populated by load_keys

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

reset_keys

   $self->reset_keys( $c );

Resets this requests keys in the stash

session_key

   $self->session_key( $c );

Returns the session store key for the current controller

set_key

   $self->set_key( $c, $key_name, $value );

Sets a key/value pair in the session store

Diagnostics

Top

None

Configuration and Environment

Top

None

Dependencies

Top

CatalystX::Usul::Base

Incompatibilities

Top

There are no known incompatibilities in this module

Bugs and Limitations

Top

There are no known bugs in this module. Please report problems to the address below. Patches are welcome

Author

Top

Peter Flanigan, <Support at RoxSoft.co.uk>

License and Copyright

Top


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: