Amon2-Plugin-Web-Flash

 view release on metacpan or  search on metacpan

lib/Amon2/Plugin/Web/Flash.pm  view on Meta::CPAN

package Amon2::Plugin::Web::Flash;
use strict;
use warnings;
use utf8;

our $VERSION = '0.05';

use Amon2::Util;

sub init {
    my ($class, $c, $conf) = @_;
    my $webpkg = ref $c || $c;

    my $key = $conf->{session_key} || 'flash';
    my $new_key = $key . "_new";
    my $flash;
    my $new_flash;

    Amon2::Util::add_method($webpkg, flash => sub {
        my ($self, $flash_key, $value) = @_;

        # getter
        return $flash unless $flash_key;
        return $flash->{$flash_key} unless $value;

        # setter
        $new_flash->{$flash_key} = $value;
        return $value;
    });

    Amon2::Util::add_method($webpkg, flash_now => sub {
        my ($self, $flash_key, $value) = @_;
        # getter. same as flash
        return $self->flash($flash_key) unless $value;

        # setter
        $flash->{$flash_key} = $value;
        return $value;
    });

    Amon2::Util::add_method($webpkg, flash_keep => sub {
        my ($self, $flash_key) = @_;
        unless ($flash_key) {
            for my $k (keys %$flash) {
                $self->flash($k => $self->flash($k));
            }
            return;
        }
        $self->flash($flash_key => $self->flash($flash_key));
    });

    Amon2::Util::add_method($webpkg, flash_discard => sub {
        my ($self, $flash_key, $value) = @_;
        unless ($flash_key) {
            $flash = {};
            return;
        }
        undef $flash->{$flash_key};
    });

    $c->add_trigger(BEFORE_DISPATCH => sub {
        my $c = shift;
        $c->session->remove($key);
        $flash = $c->session->get($new_key) || {};



( run in 0.532 second using v1.01-cache-2.11-cpan-39bf76dae61 )