[Dancer-users] Flash Message

damien krotkine dkrotkine at gmail.com
Tue Jan 11 17:48:20 CET 2011


I've re-implemented it to be more Rails-like, as sukria said.

https://github.com/dams/Dancer-Plugin-FlashMessage

and on CPAN, pending  mirrors refresh.

The funny part of the story ? the effective code is only 30 lines
long. Talking about Perl and Dancer expressiveness...

dams.

On 11 January 2011 14:40, Alexis Sukrieh <sukria at sukria.net> wrote:
> Hi list!
>
> Le 11/01/2011 14:29, damien krotkine a écrit :
>>
>> Hi,
>>
>> following previous thread, I've done a first implementation of
>> Dancer::Plugin::FlashMessage :
>>
>> https://github.com/dams/Dancer-Plugin-FlashMessage
>
> Great! Thanks a lot for your time dams, the myth is still alived! (Dancer's
> community)++
>
>
>
>> Some parts need to be improved, for instance :
>>
>> - it supports only one flash message
>> - the keywords are not short enough.
>>
>> So I think I'll change the implementation so that the template token is
>> simply called 'flash', and it'll be a hash, like in Rails. I'll also
>> change the registered method so that it's just flash() instead of
>> get_flash()
>
> I agree. I'd like to behave just like Rails' flash feature. The idea is
> pretty straight forward:
>
> "flash" is an accessor to a particular session hash table whose values can
> only be accessed once. Nothing more complicated than that.
>
> So to conclude, IMO, flash should be a wrapper like the following:
>
>    sub flash {
>        my ($key, $value) = @_;
>        my $flash = session('_flash');
>
>        # write
>        if (@_ == 2) {
>             $flash->{$key} = $value;
>             session('_flash' => $flash);
>        }
>
>        # read (+ delete)
>        else {
>            my $value = $flash->{$key};
>            delete $flash->{$key} if defined $value;
>            session('_flash' => $flash);
>        }
>
>        return $value;
>    }
>
> This is it, I think. This allows for the following code in a Dancer app:
>
>
>     get '/' => sub {
>         flash welcome => "This is a welcome message, only shown once";
>     }
>
> Then, as soon as the key 'welcome' is accesed via flash('welcome'), the
> entry will be purged.
>
> This will be very helpful for authentication stuff in before filters, error
> messages, notifications, ....
>
>
> Kudos to dams!
>
> (BTW I haven't read the code yet)
>
> --
> Alexis Sukrieh
> _______________________________________________
> Dancer-users mailing list
> Dancer-users at perldancer.org
> http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
>


More information about the Dancer-users mailing list